Jump to content

Ordinal numeration after sql result


soltek

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
https://forums.phpfreaks.com/topic/223231-ordinal-numeration-after-sql-result/
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

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"
    $position++;
}

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.