Next record. PHP + Mysql

3

I'm a beginner in programming and would like to know how I would make my $ id = 1 , turn 2, 3 and so on. The code at the end means that it needs to compare that value and only then $id++ occurs. I know I need to insert within while , but I'm not sure how to put it.

It would be so, every $ row would be a letter, I put 9 just to test. It is a game in the super trump style, that is, at first it will only appear 1 (one card), and the person will select his attribute, this is done through $_POST("Submit") . After the person clicks, this attribute will compare with another attribute, where I put that attribute ( $select > 10 ) as an example only. Then, $id=1 needs to become 2, so the game continues until $id=9 is reached.

Yes! It's a card game, type Super Trump 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] . So after doing this comparison, which is only for testing (because compare with another letter I've already managed to apply), will go to the next letter, which would be $row[2] . And this occurs until you reach $row[9] .

Have you understood better?

I do not want to play the whole select at once on the screen, but only one appears, and then after that button-like comparison ("Submit"), $id=2 , and so on. p>

In short, what I need is to start with 1, and as soon as I make the comparison and click 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"; 
            }
        }
    }
}

Thank you in advance.

    
asked by anonymous 12.08.2015 / 00:22

4 answers

3

If I understand correctly, you need to look up several different ids in a table, so you need a loop of repetition (do not necessarily have to be a while), with a for example would look 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 beware , this is a super simple example with security holes. I only meant you to understand the necessary logic. I recommend you give a read in the php documentation that tells how to make queries in the bank in the best way. I hope I have helped.

    
12.08.2015 / 01:32
2

As you've been told in the previous quote, these are not the best ways to do this. The above example does what you asked in addition to it, as you commented on 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++;
}
    
12.08.2015 / 02:47
2

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

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

That way you get N records with sequential IDs, notice that ID-3 and ID-6 have been removed and even then returns 5 IDs , in the case of the query within a loop 3 records would be returned:

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 all records with the IDs 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
        )
)
    
12.08.2015 / 05:39
0

Good morning, try it this way:

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

In this way the variable $id , will receive all the ID's of the table while there is data

    
01.03.2016 / 11:43