Jump to content

[SOLVED] Numbers next to results


CanMan2004

Recommended Posts

Hi all

I 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 like

Dave
Sarah
John
Harry
Vicky

How can I place a number next to each one, counting from 1 upwards, so the results would look like

1 Dave
2 Sarah
3 John
4 Harry
5 Vicky

Is this possible?

Thanks in advance

Ed
Link to comment
https://forums.phpfreaks.com/topic/32551-solved-numbers-next-to-results/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.