Page 1 of 1

numbering sql returns

Posted: Fri Jul 30, 2004 3:01 pm
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>
?>

Posted: Fri Jul 30, 2004 3:14 pm
by coreycollins
You could just use a simple counter or something. That would work. Just have it increment for each new row.

Posted: Fri Jul 30, 2004 3:14 pm
by feyd

Code: Select all

$num = 1;
while(......)
{
//....
$display_block ..........
$num++;
}

Posted: Fri Jul 30, 2004 3:29 pm
by hward
thanks feyd

had to change the $num = 1 to 0 it was starting at 2

Posted: Fri Jul 30, 2004 3:41 pm
by feyd
it shouldn't have unless you put the $num++ before the $display_block setting..