Jump to content

ORDER BY/GROUP BY


DanielHardy

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.