DanielHardy Posted January 21, 2011 Share Posted January 21, 2011 Hi all, I have a script that is essentially a log file of all preious versions of an article (versioning) You can see the script I am talking about in action at: http://danielrhyshardy.com/AWT/forumadmin.php What I would like to do is, instead of displaying the title of each instance (you will see there are two instances named "apple", as they are two previous versions of an article), I would like to display that title once, and then all of the instances of that title be displayed below. I hope that makes sense. Here is the code <? $sql = "SELECT id,title,message,date_added FROM messages2 ORDER BY id "; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); while(list($id,$title,$message,$date_added)=mysql_fetch_row($result)){ echo '<div style="color:#bb0000;width:250px;text-align:center;float:left;margin-left:15px;margin-bottom:15px;padding-bottom:20px;"><b>'.$title.'<br>'.$date_added.'</b> <input id="'.$message.'" type="radio" name="admin[]" value="'.$id.'" onclick="javascript:document.form22.car.value=this.id" > </font></div>'."\n"; } ?> I would use something like if ($title = "apple") { but this would obviously not work for new posts, unless I wanted to constantly update my code. I am sure there is a simple way to achieve this. Perhaps someone can tell me!? Thanks in advance Dan Quote Link to comment https://forums.phpfreaks.com/topic/225167-order-bygroup-by/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2011 Share Posted January 21, 2011 See this link - http://www.phpfreaks.com/forums/mysql-help/need-some-array-help-bigtime!!!/msg1513324/#msg1513324 Obviously, you would need to change any usage of 'date' to 'title' to match what you are doing. Quote Link to comment https://forums.phpfreaks.com/topic/225167-order-bygroup-by/#findComment-1162910 Share on other sites More sharing options...
DanielHardy Posted January 21, 2011 Author Share Posted January 21, 2011 EXACTLY what I wanted! Updated code if you are interested: <?php $booked = "<b>Seat Booked</b>"; $sql = "SELECT id,title,message,date_added FROM messages2 ORDER BY id "; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); $last_title = ''; while($row = mysql_fetch_assoc($result)){ $test = $row['message']; if($last_title != $row['title']){ echo "<h2>".$row['title'] . "</h2>"; $last_title = $row['title']; } echo $row['date_added']; echo '<input id="'.$test.'" type="radio" name="admin[]" value="'.$id.'" onclick="javascript:document.form22.car.value=this.id" >'."\n"; } ?> Feel free to view it on the site too! Thanks again, can see why you are recommended. Quote Link to comment https://forums.phpfreaks.com/topic/225167-order-bygroup-by/#findComment-1162923 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.