Automatically Numbering the populated result

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!

Moderator: General Moderators

Post Reply
sree78
Forum Commoner
Posts: 39
Joined: Tue May 20, 2003 11:38 am

Automatically Numbering the populated result

Post by sree78 »

Hi,

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!! :roll:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

html already defines a numbered list so you don't have to worry about it

Code: Select all

<html>
	<head>
		<title>an ordered list</title>
	</head>
	<body>
		<ol type="1">
			<li>first</li>
			<li>second</li>
			<li>third</li>
		</ol>
	</body>
</html>
The bbcode for
  1. uses (more or less) the same code
    1. first
    2. second
    3. third
added
Last edited by volka on Wed Oct 01, 2003 12:15 pm, edited 1 time in total.
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

Grr, and while Volka typed that, I wrote the following code:

Code: Select all

<?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);

?>
:twisted:

But it was fun, though!!

LOL
sree78
Forum Commoner
Posts: 39
Joined: Tue May 20, 2003 11:38 am

Post by sree78 »

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.. !!
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Derfel Cadarn wrote:Grr, and while Volka typed that, I wrote the following code.....
heh.........looks like me and Volka are doubling on you! :twisted:

-Nay
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

heh.........looks like me and Volka are doubling on you!
instant team work :]

and sree78: of course it does

Code: Select all

<?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);
?>
Post Reply