New Coder Posted January 6, 2010 Share Posted January 6, 2010 Hello All, I have a comments table: ID int autoincrement, Name varchar(50), Comments varchar(2000), Safe char(1) default (N). Select * from comments; ID Name Comment Safe 1 Joe Bloggs Comment 1 Y 2 John Doe Comment 2 N 3 Jane Doe Comment 3 Y 4 Joe Bloggs Comment 4 Y 5 John Doe Comment 5 Y 6 Joe Bloggs Comment 6 Y I am using the following query to output 4 random site comments that has been approved and set as safe. SELECT * FROM tbl_dyk where safe=’Y’ ORDER BY RAND() limit 4 ID Name Comment Safe 1 Joe Bloggs Comment 1 Y 3 Jane Doe Comment 3 Y 4 Joe Bloggs Comment 4 Y 6 Joe Bloggs Comment 6 Y Which works a treat. What I also need is to assign the rows a new number from 1-4 like so: ID Name Comment Safe Row_num 1 Joe Bloggs Comment 1 Y 1 3 Jane Doe Comment 3 Y 2 4 Joe Bloggs Comment 4 Y 3 6 Joe Bloggs Comment 6 Y 4 Is this possible? Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187394-row-numbering-help/ Share on other sites More sharing options...
trq Posted January 6, 2010 Share Posted January 6, 2010 Yes its possible, but when you say assign do you mean you want to save these numbers back into the database or just use them for display reasons? Quote Link to comment https://forums.phpfreaks.com/topic/187394-row-numbering-help/#findComment-989537 Share on other sites More sharing options...
New Coder Posted January 6, 2010 Author Share Posted January 6, 2010 Sorry, Yes I just want to use them for display reasons. Quote Link to comment https://forums.phpfreaks.com/topic/187394-row-numbering-help/#findComment-989538 Share on other sites More sharing options...
trq Posted January 6, 2010 Share Posted January 6, 2010 Simple then, just increment a number as you iterate through your loop. eg; $i = 1; while ($row = mysql_fetch_assoc($result)) { echo $row['ID'] . ' ' . $row['Name'] . ' ' . $row['Comment'] . ' ' . $row['Safe'] . $i . '<br />'; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/187394-row-numbering-help/#findComment-989541 Share on other sites More sharing options...
New Coder Posted January 6, 2010 Author Share Posted January 6, 2010 Spot on, many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/187394-row-numbering-help/#findComment-989562 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.