Next record. PHP + Mysql

Asked

Viewed 1,123 times

3

I am a beginner in programming and would like to know how I would make mine $id = 1, turn 2, 3 and so on. The code at the end, means that it needs to compare that value and only then occurs a $id++. I know I need to insert into one while, but I don’t know how to put.

It would be like, every $Row would be a letter, I put nine just to test. It is a game in super trump style, IE, at the beginning will appear only 1 (one card), and the person will select his attribute, this is done through the $_POST("Submit"). After the person clicks, this attribute will compare to another attribute, where I put that ($select > 10) just as an example. Next, the $id=1 needs to turn 2, for the game to continue until arriving $id=9.

Yes! It’s a card game, like Super Trump you know?! The rule is very simple, because the player even winning or losing, his card has to change. What happens is that each letter, I inserted there by $row[1]. Then after making this comparison, which is only for testing (Why compare with another card I already managed to apply), will go to the next card, which would be $row[2]. And that happens until you get to $row[9].

It gave to understand better?

I do not want to play all select at once on the screen, but rather appear only one, and then, after this comparison made by button("Submit"), appear the $id=2 and so on.

In short, what I need is for you to start with 1, and once you make the comparison and click on the button, the next "screen" is 2.

include ("connect.php");  //Conexão Banco

$sql = mysql_query("SELECT * FROM card");

$id = 1;

$row[1] = mysql_fetch_array($sql);
$row[2] = mysql_fetch_array($sql);
$row[3] = mysql_fetch_array($sql);
$row[4] = mysql_fetch_array($sql);
$row[5] = mysql_fetch_array($sql);
$row[6] = mysql_fetch_array($sql);
$row[7] = mysql_fetch_array($sql);
$row[8] = mysql_fetch_array($sql);
$row[9] = mysql_fetch_array($sql);

$pic = $row[$id]["Photo"]; 
$rk = $row[$id]["Ranking"]; 
$tt = $row[$id]["Tittles"];
$st = $row[$id]["Started"];
$ya = $row[$id]["Years Active"];
if (isset($_POST["Submit"])){       
    if (!empty($_POST["game"])){ 
        foreach ($_POST ["game"] as $selected) { 
            if ($selected > 10) { 
                echo "win"; 
            }
        }
    }
}

Thanks in advance.

  • You can do a for().. would have a link that will click to go to the next page?

  • I can’t understand where you’re going.... Can you explain the situation better?

  • Explain better what is the purpose of this code ... it looks like some kind of card game ... what is the rule? the player gets 9 cards and what happens?

  • Remember that mysql_* functions are obsolete since PHP 5.5 and will be removed from PHP soon. Prefer to use Mysqli or PDO. See more here: http://www.ultimatephp.com.br/php-por-que-nao-utilizar-funcoes-mysql

  • @Philip added text of the answer you had put. I deleted because it was not an answer. Can you [Dit] the question and see if the text is right? So we can help better.

  • Problem-free!!!

Show 1 more comment

4 answers

3

If I understood correctly, you need to search for several different ids in a table, for that, you need a repeat loop (it doesn’t need to be necessarily a while), with a for, for example, it would be more or less like this:

for ($i=0; $i<=9; $i++){
  $sql = mysql_query("SELECT * FROM card WHERE id=".$id);
  $row = mysql_fetch_array($sql);

  //aqui vai toda a sua logica com o $row
}

But be careful, This is a super simple and flawed example of security. I only did it to understand the necessary logic. I recommend you take a look at the php documentation that tells you how to make queries in the database in the best way. I hope I’ve helped.

2

As already reported in the previous quote, these are not the best ways to do this. The above example does what you asked for, besides it, as you commented on the while:

$i = 1;
while ($i <= 10){
  $sql = mysql_query("SELECT * FROM card WHERE id=".$id.");
  $row = mysql_fetch_array($sql);

  //aqui vai toda a sua logica com o $row
  $i++;
}

2

Note that a loop from 1 to 10 using SELECT * FROM card WHERE id = {$id++} does not guarantee to return 10 records! If between the limit of 10 there is 1 deleted record, then you will have lower records than you want.

As an example I will use a smaller amount to not lengthen the answer. My suggestion is based on the use of only 2 queries with the Ids available sequentially.:


This way you get N records with Ids sequential, note that the ID-3 and the ID-6 have been removed and still returns 5 Ids, in case the query inside a loop would be returned 3 records:

SQL:select id from tabela limit 5

Array
(
    [0] => Array
            [id] => 1

    [1] => Array
            [id] => 2

    [2] => Array
            [id] => 4

    [3] => Array
            [id] => 5

    [4] => Array
            [id] => 7

)

Using a foreach to create a list of Ids found:

foreach( mysql_fetch_array($sql) as $rows )
{
    $ids[] = $rows['id'];
}

Now using implode to mount a query with where id in to return the records with the Ids that were found above: where id in( 1,2,4,5,7)

SQL:select id , nome from tabela where id in( ' . implode( ',' , $ids ) . ' )


Final result

Array
(
    [0] => Array
        (
            [id] => 1
            [nome] => nome 1
        )

    [1] => Array
        (
            [id] => 2
            [nome] => nome 2
        )

    [2] => Array
        (
            [id] => 4
            [nome] => nome 4
        )

    [3] => Array
        (
            [id] => 5
            [nome] => nome 5
        )

    [4] => Array
        (
            [id] => 7
            [nome] => nome 7
        )
)
  • Boy, I haven’t been able to enter the code yet! The question has been modified! See if you can understand it better? Thanks for the help already!

0

Good morning, try this way:

$sql = mysql_query("SELECT * FROM card");
while($dado = mysql_fetch_array($sql)){
   $id = $dado['id'];
}

Thus the variable $id, will receive all table ID’s while there is data

Browser other questions tagged

You are not signed in. Login or sign up in order to post.