Jump to content

Group by date and display all related entries below???


StefanRSA

Recommended Posts

I have a datetime field in MySql and the entries are put is as 2010-06-08 17:12:50.

Its for a message box... So when I display the conversation, do I want the specific date on top of those days coversations.... And the time at every message sent...

 

I just cannot get my mind around to do this....

 

At this stage the results look like this:

User 1    ---- User 1 message A --- Time Sent

User 2    ---- User 2 message A --- Time Sent

User 1    ---- User 1 message B --- Time Sent

User 2    ---- User 2 message B --- Time Sent

 

I know how to get the date and time out by its own but how can I display it as follow:

4 Januray 2011

User 1    ---- User 1 message A --- Time Sent

User 2    ---- User 2 message A --- Time Sent

5 January 2011

User 1    ---- User 1 message B --- Time Sent

User 2    ---- User 2 message B --- Time Sent

 

My script so far:

<? 
$allmq=mysql_query("SELECT * FROM messages WHERE `from`='$myid' AND `to`='$hisid' AND `from_deleted`!='1' OR `from`='$hisid' AND `to`='$myid' AND `to_deleted`!='1' ORDER BY `created` DESC") 
or die (mysql_error());



echo '<div style="width:646px;height:150px;overflow-x:hidden; overflow-y:auto;float:left;">';
while($allmr=mysql_fetch_array($allmq)){
$created=$allmr['created'];
$arrtime=explode(" ", $created);
$thisadid=$allmr['id'];
$from=$allmr['from'];
$to=$allmr['to'];
if($aduserid==$from){
$nfrom=$receivedun;
$nto=$myun;
$viewer="`to_viewed` = '1', `to_vdate` = NOW()";
$message=$allmr['message'];
$fcol='color:#829A64;';
}
else {
$nfrom=$myun;
$viewer="`from_viewed` = '1', `from_vdate` = NOW()";
$nto=$receivedun;
$message=$allmr['message'];
$fcol='color:#464646;';
}
$sql1 = mysql_query("UPDATE messages SET $viewer WHERE `id` = '".$thisadid."' LIMIT 1");
$title=$allmr['title'];

$created=$allmr['created'];
echo '<div style="width:631px;height:auto;clear:both;">';
echo '<div style="width:86px;height:20px;float:left;text-align:right;padding-right:14px;'.$fcol.'">'.$nfrom.'</div>';
echo '<div style="width:427px;height:auto;float:left;padding-right:14px;'.$fcol.'">'.$message.'</div>';
echo '<div style="width:90px;height:25px;float:left;clear:right;color:#999999;">'.$created.'</div>';
echo '</div><div style="width:631px;height:14px;clear:both;"> </div>';
}
echo '</div>';
?>
<br>
<div style="padding-bottom:5px;margin-left:140px;padding-top:10px;height:22px;width:724px;clear:both;">
<?
include 'probars.php';
?>
</div>
</div>

 

Link to comment
Share on other sites

use a "flag" when looping through your results.

if the flag is set to the current date in the loop, don't show the div again.  Otherwise, show the div and update the flag for the next time through the loop.

 

<?php
// ...all the stuff in your while loop

if($last_date_displayed != date("d F Y",strtotime($created)) ){
     $last_date_displayed = date("d F Y",strtotime($created));
    echo '<div>'.$last_date_displayed.'</div>';
} 
echo '<div style="width:631px;height:auto;clear:both;">';
echo '<div style="width:86px;height:20px;float:left;text-align:right;padding-right:14px;'.$fcol.'">'.$nfrom.'</div>';
echo '<div style="width:427px;height:auto;float:left;padding-right:14px;'.$fcol.'">'.$message.'</div>';
echo '<div style="width:90px;height:25px;float:left;clear:right;color:#999999;">'.$created.'</div>';
echo '</div><div style="width:631px;height:14px;clear:both;"> </div>';
}
?>

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.