CanMan2004 Posted January 2, 2007 Share Posted January 2, 2007 Hi allI have a simply query which looks like[code]<?$sql = mysql_query("SELECT * FROM name")or die(mysql_error());while($row = mysql_fetch_array($sql)){$id = $row['id'];$names = $row['names'];?><? print $names; ?><br><?}?>[/code]it returns a list of names, this looks likeDaveSarahJohnHarryVickyHow can I place a number next to each one, counting from 1 upwards, so the results would look like1 Dave2 Sarah3 John4 Harry5 VickyIs this possible?Thanks in advanceEd Quote Link to comment Share on other sites More sharing options...
AndyB Posted January 2, 2007 Share Posted January 2, 2007 [code]php<?$sql = mysql_query("SELECT * FROM name") or die(mysql_error());$i = 0;while($row = mysql_fetch_array($sql)) { $id = $row['id']; // who cares? $i++; echo $i. " ". $row['names']. "<br>";}[/code] Quote Link to comment Share on other sites More sharing options...
taith Posted January 2, 2007 Share Posted January 2, 2007 [code]<?$sql = mysql_query("SELECT * FROM name")or die(mysql_error());while($row = mysql_fetch_array($sql)){ echo $row['id'].' '.$row['names'].'<br>';}?>[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 2, 2007 Share Posted January 2, 2007 Yes, this is simple PHP. Do you want to print out the $id, which you are retrieving but not using, or just a simple counter?Echoing the $id:[code]<?php$q = "SELECT * FROM name";$sql = mysql_query($q)or die("Problem with the query: $q<br>" . mysql_error());while($row = mysql_fetch_array($sql)){ $id = $row['id']; $names = $row['names']; echo $id . ' ' . $names . '<br>';}?>[/code]A simple counter:[code]<?php$q = "SELECT names FROM name";$sql = mysql_query($q)or die("Problem with the query: $q<br>" . mysql_error());$i = 1;while($row = mysql_fetch_array($sql)){ $names = $row['names']; echo $i++ . ' ' . $names . '<br>';}?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted January 2, 2007 Author Share Posted January 2, 2007 thanks everyone 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.