Jump to content

Ordinal numeration after sql result


Recommended Posts

Hello, wrote some Top 10 thingy, with data retrieved from a sql database.

Everything is fine, I managed to display the data ordered as I wanted and stuff, but what I'm really looking to is for a way to have one row on that table that prints the ordinal numeration (1º, 2º...and so on).


I think it's done with an array, but I'm having a hard time to figure that out.

Can you please enlight me?


Thank you.

Link to comment
Share on other sites

For example, I want to print a table with the 5 more popular posts on a blog:


SELECT title, views FROM posts ORDER BY views DSC Limit 0,5


Then I'd mysql_fetch_array the crap out of it ( ;D) to have an html table with the 5 Most Popular Posts.

Whan im looking for is a way to have that html table not only sorted, but the with an extra column conteining their ordinal numeration.


ie:      1º |  How to eat pudin tutorial  |  194 views

          2º | ........

          and so on

Link to comment
Share on other sites

Example script

$query = "SELECT post_name, post_id
          FROM blogs
          ORDER BY views DESC
          LIMIT 10";
$result = mysql_query($query);

$position = 1;
while($row = mysql_fetch_assoc())
    echo "{$position}. <a href=\"show_post.php?id={$row['post_id']}\">{$row['post_name']}</a><br />\n"

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

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

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.