Jump to content

problem with query loop


joecooper

Recommended Posts

I was the code to skip the first 2 entrys returned, and then loop though the other entrys in the table and display the html code (unfinished)

 

<?php

include("db.php");
$query = mysql_query("SELECT * FROM games ORDER BY id DESC");

$i = 1;
while ($row = mysql_fetch_assoc($query)){
   if ($i == 1 || $i == 2){
   }else{
   echo "test";
?>


  <tr>
    <td width="50" height="100" bgcolor="#666666"> </td>
    <td width="102" height="100" bgcolor="#999999"style="border-bottom:1px solid #FFFFFF; border-left:1px solid #FFFFFF; border-right:1px solid #FFFFFF;> </td>
    <td bgcolor="#666666"> </td>
    <td width="10" bgcolor="#666666"> </td>
    <td width="638" bgcolor="#666666"><span class="style3">This is the game description. This will give all the interesting info about the game and why the user should play it </span></td>
  </tr>
  
<?php    
   }

$i++;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/121762-problem-with-query-loop/
Share on other sites

I was the code to skip the first 2 entrys returned, and then loop though the other entrys in the table and display the html code (unfinished)

Why do you want to skip the first two entries from the result set? Do you not want the query the return the first two rows from the games table?

 

However your code as it is not recommended, but it should work.

Try doing something along these lines:

 

<?php

$num_rows = mysql_num_rows($query) - 2;//return the number of results excluding the first two

$query123 = mysql_query($query. "LIMIT 2,".$num_rows);//return all rows from the third result on

?>

 

This logic works in the following sense:

 

If you have 10 results, ordered from 0,1,2,3,4,5,6,7,8,9 [for instance].

 

$num_rows = 10 - 2 = 8.

 

The query will then fetch 8 rows starting from record #2: 2,3,4,5,6,7,8,9.

<?php

include("db.php");
$query = mysql_query("SELECT * FROM games ORDER BY id DESC");

echo "<table>";//start the table tag

while ($row = mysql_fetch_assoc($query)){
?>


  <tr>
    <td width="50" height="100" bgcolor="#666666"> </td>
    <td width="102" height="100" bgcolor="#999999"style="border-bottom:1px solid #FFFFFF; border-left:1px solid #FFFFFF; border-right:1px solid #FFFFFF;> </td>
    <td bgcolor="#666666"> </td>
    <td width="10" bgcolor="#666666"> </td>
    <td width="638" bgcolor="#666666"><span class="style3">This is the game description. This will give all the interesting info about the game and why the user should play it </span></td>
  </tr>
  
<?php    
}
echo "</table>";//end the table tag
?>

 

I modified the above code provided just for your simple request of having it create a new row for each entry. Please note that your html has errors in it (missing quotations, incorrect CSS, etc..) and needs to be fixed before you attempt to echo it out with PHP [the above will echo out the html just fine, but it's not the best way to echo it out if you want to include information from the database].

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.