Jump to content

Sort by date


cordoprod

Recommended Posts

Hi. I want to create a feed.

I know how to list all entries from a DB, but the date will be a bit corny.

 

Now its like this:

 

1 july

-------------

entry

 

1. july

--------

entry

 

2. july

------

entry

 

What i want to do is to join those who have the same date. Like the to 1 july to be like this:

 

1.july

----------

entry

entry

Link to comment
https://forums.phpfreaks.com/topic/114509-sort-by-date/
Share on other sites

Ive been playin around with the code a bit... take a look.. see if you can help

 

<?
          $db = new DbConnector();
	  $db->connect();
	  
	  $sql = "SELECT * FROM travek_feed ORDER BY time DESC";
	  $result = $db->query($sql);
	  
	  $day = "";
	  
	  while($fetch = mysql_fetch_array($result)) {
	      if($day == "" and $current_day == "") $day = norsk_dato($fetch['time'], false);
		  elseif($day == $current_day)  $day = ""; ?>

            <table width="100%" border="0" cellspacing="0" cellpadding="2">
              <tr>
              <td><? echo $day; ?></td>
                <td><? echo date("H:i", $fetch['time']); ?></td>
                <td><? echo "<a href=\"?s=user&nick={$fetch['nick']}\">".$fetch['nick']."</a> ".$fetch['action']; ?></td>
              </tr>
            </table>
            
          <?
	  $current_day = norsk_dato($fetch['time'], false);
	  }
	  
	  ?>

 

Note that norsk_dato is a function to output this format in norwegian: Sunday 20. july 2008

 

Output:

Søndag 20. juli 2008  13:59  demo lastet opp videoen Roy Abelseth (gone too soon).

17:00 glenn blabla

23:14 hei iiejijji

23:14 glenn glenn ble venn med cairoli

 

It only outputs the first..

Link to comment
https://forums.phpfreaks.com/topic/114509-sort-by-date/#findComment-594784
Share on other sites

I think am getting closer to it now.. just a few bugs..

 

heres the code:

<?
          $db = new DbConnector();
	  $db->connect();
	  
	  $sql = "SELECT * FROM travek_feed ORDER BY time DESC";
	  $result = $db->query($sql);
	  
	  $day = "";
	  
	  echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
	  while($fetch = mysql_fetch_array($result)) {
	      if($day == "" and $current_day == "") $day = norsk_dato($fetch['time'], false);
		  elseif($day != $current_day)  $day = norsk_dato($fetch['time'], false); 
		  else $day = "";
		  ?>

            
              <? if($day != "") { echo "<tr><td colspan=2 style=\"border-bottom: 1px solid #000000;\"><br>$day</td></tr>"; } ?>
              <tr>
                <td><? echo date("H:i", $fetch['time']); ?></td>
                <td><? echo "<a href=\"?s=user&nick={$fetch['nick']}\">".$fetch['nick']."</a> ".$fetch['action']; ?></td>
              </tr>
          <?
	  $current_day = norsk_dato($fetch['time'], false);
	  }
	  
	  ?></table>

 

What's in the table: (id | nick | action | time)

1      glenn  glenn ble venn med cairoli                                      1211750067

2 hei iiejijji                                                                          1211750069

3 glenn blabla                                                                 1211814009

4 demo lastet opp videoen <a href="?s=video&nick=demo&fol... 1216555164

5 cordoprod har blitt kul.                                                         1211814009

 

Output:

Søndag 20. juli 2008

13:59 demo lastet opp videoen Roy Abelseth (gone too soon).

17:00 glenn blabla

 

Mandag 26. mai 2008

17:00 cordoprod har blitt kul.

23:14 hei iiejijji

 

Søndag 25. mai 2008

23:14 glenn glenn ble venn med cairoli

 

 

 

Some of them is not included and some of them does not match the date...

Link to comment
https://forums.phpfreaks.com/topic/114509-sort-by-date/#findComment-594791
Share on other sites

You might find it easier to pull just the date part as a separate column

 

SELECT *, DATE(FROM_UNIXTIME(time)) as datepart ...

 

then (pseudocode)

lastdate=''
while fetch next row
   if datepart != lastdate
        output date
        lastdate = datepart
   end if
   output detail line
end while

Link to comment
https://forums.phpfreaks.com/topic/114509-sort-by-date/#findComment-594897
Share on other sites

You might find it easier to pull just the date part as a separate column

 

SELECT *, DATE(FROM_UNIXTIME(time)) as datepart ...

 

then (pseudocode)

lastdate=''
while fetch next row
   if datepart != lastdate
        output date
        lastdate = datepart
   end if
   output detail line
end while

 

What do you mean? Can you show a bit more detailed?

Link to comment
https://forums.phpfreaks.com/topic/114509-sort-by-date/#findComment-594901
Share on other sites

I think i'm on the wrong track here but something like this?

 

<?
          $db = new DbConnector();
	  $db->connect();
	  
	  $sql = "SELECT *, DATE(FROM_UNIXTIME(time)) as datepart FROM travek_feed";
	  $result = $db->query($sql);
	  
	  
	  $lastdate='';
	  while($fetch = mysql_fetch_array($result)) {
	     if ($datepart != $lastdate) { $datepart = norsk_dato($fetch['time'], false); $lastdate = $datepart; }
		  ?>

            <table width="100%" border="0" cellspacing="0" cellpadding="2">
              <? echo "<tr><td style=\"border-bottom: 1px solid #000000;\">$datepart</td></tr>"; ?>
              <tr>
                <td><? echo date("H:i", $fetch['time']); ?></td>
                <td><? echo "<a href=\"?s=user&nick={$fetch['nick']}\">".$fetch['nick']."</a> ".$fetch['action']; ?></td>
              </tr>
            </table>
            
          <?
	  }
	  
	  ?>

Link to comment
https://forums.phpfreaks.com/topic/114509-sort-by-date/#findComment-594912
Share on other sites

You might find it easier to pull just the date part as a separate column

 

SELECT *, DATE(FROM_UNIXTIME(time)) as datepart ...

 

then (pseudocode)

lastdate=''
while fetch next row
   if datepart != lastdate
        output date
        lastdate = datepart
   end if
   output detail line
end while

 

Should i use that code? I mean that isn't php? right?

Link to comment
https://forums.phpfreaks.com/topic/114509-sort-by-date/#findComment-595407
Share on other sites

I think i'm on the wrong track here but something like this?

 

<?
          $db = new DbConnector();
	  $db->connect();
	  
	  $sql = "SELECT *, DATE(FROM_UNIXTIME(time)) as datepart FROM travek_feed";
	  $result = $db->query($sql);
	  
	  
	  $lastdate='';
	  while($fetch = mysql_fetch_array($result)) {
	     if ($datepart != $lastdate) { $datepart = norsk_dato($fetch['time'], false); $lastdate = $datepart; }
		  ?>

            <table width="100%" border="0" cellspacing="0" cellpadding="2">
              <? echo "<tr><td style=\"border-bottom: 1px solid #000000;\">$datepart</td></tr>"; ?>
              <tr>
                <td><? echo date("H:i", $fetch['time']); ?></td>
                <td><? echo "<a href=\"?s=user&nick={$fetch['nick']}\">".$fetch['nick']."</a> ".$fetch['action']; ?></td>
              </tr>
            </table>
            
          <?
	  }
	  
	  ?>

 

I tried something here. no success

Link to comment
https://forums.phpfreaks.com/topic/114509-sort-by-date/#findComment-595426
Share on other sites

What errors are you getting?

 

$sql = "SELECT *, DATE(FROM_UNIXTIME(time)) as datepart FROM travek_feed";

 

i do not think you need the comma after *

 

Output:

23:14  glenn glenn ble venn med cairoli

23:14 hei iiejijji

17:00 glenn blabla

13:59 demo lastet opp videoen Roy Abelseth (gone too soon).

17:00 cordoprod har blitt kul.

 

It does not join the days, and the $datepart is empty.

Link to comment
https://forums.phpfreaks.com/topic/114509-sort-by-date/#findComment-596416
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.