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
Share on other sites

[code]php<?
$sql = mysql_query("SELECT * FROM name") or die(mysql_error());
$i = 0;
while($row = mysql_fetch_array($sql)) {
    $id = $row['id']; // who cares?
    $i++;
    echo $i. " ". $row['names']. "<br>";
}[/code]
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.