numbering sql returns

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
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

numbering sql returns

Post by hward »

this returns all my records in my data base and just gives the names but I want to number the returns using the $num that is in the display block

Code: Select all

<?php
$db = @mysql_select_db($db_name, $connection)
	or die("Couldn't select database.");


$sql = "SELECT fname, lname
	FROM $table_name
    ORDER BY lname
	";

$result = @mysql_query($sql,$connection)
	or die("Couldn't execute query.");

while ($row = mysql_fetch_array($result)) {
	$fname = $row['fname'];
	$lname = $row['lname'];
	
	
	$display_block .= "
	<P><h2>($num)</h2> 
	<strong>$lname, $fname</strong><hr>
	";
}


?>

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>


<? echo "$display_block"; ?>

</BODY>
</HTML>
?>
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

You could just use a simple counter or something. That would work. Just have it increment for each new row.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$num = 1;
while(......)
{
//....
$display_block ..........
$num++;
}
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

thanks feyd

had to change the $num = 1 to 0 it was starting at 2
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it shouldn't have unless you put the $num++ before the $display_block setting..
Post Reply