Guest convention Posted October 15, 2006 Share Posted October 15, 2006 I made a while loop that displays all of the entries in the specified table (shown below), and I need to set up a for loop that assigns an incrementing number to each database entry. How could I do this? I tried using this for loop:[code]dbconnect();$query = mysql_query("SELECT * FROM table_name ORDER BY id DESC");while ($row = mysql_fetch_array($query)) { for ($i=1; $i<count(mysql_num_rows($query)); $i++) { echo $i."<br>"; echo $row['field_name']; // Other statements... }}[/code]but the whole thing disappears. And if I assign 0 to $i, then the number 0 is just displayed for every table entry. Any way to fix this?Thank you. :) Quote Link to comment Share on other sites More sharing options...
dymon Posted October 15, 2006 Share Posted October 15, 2006 [color=blue]mysql_num_rows($query)[/color] returns an int value, that contains the number of rows. You don't need to use count() function.while ($row = mysql_fetch_array($query)) { for ($i=1; $i<[color=blue]mysql_num_rows($query)[/color]; $i++) { echo $i."<br>"; echo $row['field_name']; // Other statements... }} Quote Link to comment Share on other sites More sharing options...
Guest convention Posted October 15, 2006 Share Posted October 15, 2006 Thank you for your help, dymon. However, now every single database entry loops 26 times each! (There are 26 entries in the database.) Here's what the output now looks like:1. Entry One2. Entry One3. Entry One...1. Entry Two2. Entry Two3. Entry Two...1. Entry Three2. Entry Three3. Entry Three... Quote Link to comment Share on other sites More sharing options...
dymon Posted October 15, 2006 Share Posted October 15, 2006 You would better do like this, just a simple inc variable:$count = 1;while ($row = mysql_fetch_array($query)) { echo $count." "; echo $row['field_name']; $count++; // Other statements...}This should work. Quote Link to comment Share on other sites More sharing options...
Guest convention Posted October 15, 2006 Share Posted October 15, 2006 Ah ha! Thank you so much! ;D ;D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.