soltek Posted January 3, 2011 Share Posted January 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/223231-ordinal-numeration-after-sql-result/ Share on other sites More sharing options...
OldWest Posted January 3, 2011 Share Posted January 3, 2011 So you want to print the results of the query data in the same order as the table holds the data? Can you clarify? Quote Link to comment https://forums.phpfreaks.com/topic/223231-ordinal-numeration-after-sql-result/#findComment-1154078 Share on other sites More sharing options...
soltek Posted January 3, 2011 Author Share Posted January 3, 2011 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 ( ) 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 Quote Link to comment https://forums.phpfreaks.com/topic/223231-ordinal-numeration-after-sql-result/#findComment-1154080 Share on other sites More sharing options...
soltek Posted January 4, 2011 Author Share Posted January 4, 2011 Anybody? Quote Link to comment https://forums.phpfreaks.com/topic/223231-ordinal-numeration-after-sql-result/#findComment-1154604 Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 start with $value = 1. echo $value. on each loop, add 1 to the $value. Quote Link to comment https://forums.phpfreaks.com/topic/223231-ordinal-numeration-after-sql-result/#findComment-1154654 Share on other sites More sharing options...
soltek Posted January 4, 2011 Author Share Posted January 4, 2011 How do I do that exactly? I tried, but I ended up having the number 1 on all rows Quote Link to comment https://forums.phpfreaks.com/topic/223231-ordinal-numeration-after-sql-result/#findComment-1154688 Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 please post your code. Quote Link to comment https://forums.phpfreaks.com/topic/223231-ordinal-numeration-after-sql-result/#findComment-1154694 Share on other sites More sharing options...
Psycho Posted January 4, 2011 Share Posted January 4, 2011 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/223231-ordinal-numeration-after-sql-result/#findComment-1154701 Share on other sites More sharing options...
soltek Posted January 4, 2011 Author Share Posted January 4, 2011 Damn, I had the $value = 1 after the begining of the loop. It works like a charm now, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/223231-ordinal-numeration-after-sql-result/#findComment-1154752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.