PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am wondering if it is possible to automatically assign numbers like for an example :-
1. XYX
2. VBG
3. GFT
in a populated result from a database which in this case would be XYX, VBG, GFT. I am fairly new to PHP.. I would really appreciate if you could provide some explanation if you have the answer for my question. In that way I will be able to understand what I am doing. Thanks a lot in advance!!
<?php
/*
* Name: test
* Author: Derfel
* Created on: 01.10.2003 19:01:34
*/
// Taken from the PHP-Manual
// Connect to the MySQL-database
mysql_connect($host, $user, $password);
// Select the database
mysql_select_db("database");
// Do the query
$result = mysql_query("select user_id from table");
// Set a counter to 1
$i="1";
while ($row = mysql_fetch_array($result)) {
// For each result, present tha counter and the result
echo $i.": ".$row["user_id"]."<br />\n";
// Don't forget to add 1 to the counter for the next result
$i++;
}
// Free the $result (make it empty)
mysql_free_result($result);
?>
Thank you Derfel Cadarn .. your explanation was very clear and it worked... and volka.. I really appreciate your response.... I tried similar code before but since I needed auto numbering for dynamically populated results it did not work.. Once gaain thanks a lot guys.. !!
<?php
/*
* Name: test
* Author: Derfel
* Created on: 01.10.2003 19:01:34
* Changed by: Volker
*/
// Taken from the PHP-Manual
// Connect to the MySQL-database
mysql_connect($host, $user, $password);
// Select the database
mysql_select_db("database");
// Do the query
$result = mysql_query("select user_id from table");
echo '<ol type="1">';
while ($row = mysql_fetch_array($result))
echo ' <li>', $row['user_id'], '</li>';
echo '</ol>';
mysql_free_result($result);
?>