DrFishNips Posted June 24, 2009 Share Posted June 24, 2009 This is a simple question but I couldn't think of what to put into google to get info on it. I need to dynamically number database results starting from 1. For example lets say I have a database with 3 entries "monkey", "badger", "armadillo" when I display the contents I need it to be like 1.) Armadillo 2.) Badger 3.) Monkey Quote Link to comment https://forums.phpfreaks.com/topic/163522-solved-php-increasing-numbers/ Share on other sites More sharing options...
Maq Posted June 24, 2009 Share Posted June 24, 2009 What do you mean dynamically? You should already have some sort of id or auto-increment values when inserting in the database. Quote Link to comment https://forums.phpfreaks.com/topic/163522-solved-php-increasing-numbers/#findComment-862777 Share on other sites More sharing options...
MadTechie Posted June 24, 2009 Share Posted June 24, 2009 Or just add a counter to the loop $i++ Quote Link to comment https://forums.phpfreaks.com/topic/163522-solved-php-increasing-numbers/#findComment-862783 Share on other sites More sharing options...
DrFishNips Posted June 24, 2009 Author Share Posted June 24, 2009 Or just add a counter to the loop $i++ Yeah thats what I'm talking about. I tried that and it works but the number starts at 0 for some reason. Do you know how to make it start at 1? Quote Link to comment https://forums.phpfreaks.com/topic/163522-solved-php-increasing-numbers/#findComment-862796 Share on other sites More sharing options...
Maq Posted June 24, 2009 Share Posted June 24, 2009 Or just add a counter to the loop $i++ Yeah thats what I'm talking about. I tried that and it works but the number starts at 0 for some reason. Do you know how to make it start at 1? Put $i=1 before the loop. If you post the relevant code it will be easier to help. Quote Link to comment https://forums.phpfreaks.com/topic/163522-solved-php-increasing-numbers/#findComment-862798 Share on other sites More sharing options...
DrFishNips Posted June 24, 2009 Author Share Posted June 24, 2009 Sorry heres the code $query = "SELECT * FROM potg_memb_pm WHERE memb_username = '$pm_login' ORDER BY 'msg_date' LIMIT 10"; $result = mysql_query($query); $num = mysql_num_rows($result); $i=0; while ($i < $num) { $msg_id = mysql_result($result, $i, "id"); $memb_id = mysql_result($result, $i, "memb_id"); $msg_sender = mysql_result($result, $i, "msg_sender"); $msg_subject = mysql_result($result, $i, "msg_subject"); $msg_body = mysql_result($result, $i, "msg_body"); $msg_date = mysql_result($result, $i, "msg_date"); $msg_time = mysql_result($result, $i, "msg_time"); echo " <tr>\n"; echo " <td><a class=\"gb_link\" href=\"?get=members&page=pm&action=show&msg=$msg_id\">"; echo $i; echo "</a></td>\n"; echo " <td><a class=\"gb_link\" href=\"?get=members&page=pm&action=show&msg=$msg_id\">$msg_subject</a></td>\n"; echo " <td><a class=\"gb_link\" href=\"?get=members&member=$memb_id\">$msg_sender</a></td>\n"; echo " <td>$msg_date</td>\n"; echo " <td>$msg_time</td>\n"; echo " <td><input type=\"checkbox\" name=\"1\" value=\"1\"></td>\n"; echo " </tr>\n"; echo " <br>\n"; ++$i; It lists members PM's and I'm trying to number the PM's from 1 up. Quote Link to comment https://forums.phpfreaks.com/topic/163522-solved-php-increasing-numbers/#findComment-862800 Share on other sites More sharing options...
DrFishNips Posted June 24, 2009 Author Share Posted June 24, 2009 I got it to work. I just typed echo $i + 1 and now it starts from 1 instead of 0. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/163522-solved-php-increasing-numbers/#findComment-862802 Share on other sites More sharing options...
Maq Posted June 24, 2009 Share Posted June 24, 2009 Or you could have changed $i=0 to $i=1. Quote Link to comment https://forums.phpfreaks.com/topic/163522-solved-php-increasing-numbers/#findComment-862806 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.