Jump to content

Simple Sorting?


Branden Wagner

Recommended Posts

Ok, so i am making a page for System status updates(generated by my server).... i am having a small problem figuring out the logic...
so i have in the mysql db the id,note,author,timestamp. i want everything sorted by day, and then time as far as the html displays it


i know i can do a query to "order by timestamp desc" and that puts them  into the right order, but doesnt display them the way i want, because i want to break up the individual days. like i have here: [url=http://dev.pureintellect.com/status/index.html]http://dev.pureintellect.com/status/index.html[/url]

so how do i do this? any suggestions would be helpful. its not so much the php thats the problem, just the logic atleast i think thats my only problem.
Link to comment
https://forums.phpfreaks.com/topic/27305-simple-sorting/
Share on other sites

You can fetch all the data sorted by timestamp, and then have a loop which extracts the day and time, and then displays according to those values.  By keeping the last displayed day in a variable, you will know when to start displaying a new day (it will be whenever the current day is not equal to the last displayed day).

[code=php:0]$last_day = null;
foreach ($data as $d) {
  list($day, time) = extract_day_time($d['timestamp']);
  if ($day !== $last_day) {
    # New day
  } else {
    # Continue current day
  }
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/27305-simple-sorting/#findComment-124867
Share on other sites

Thank you....that cleared up the logic error... and worked... but not really... lol its a small problem, i made a minor change, because i hate foreach loops..
[code]
mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name);
$result =mysql_query("SELECT UNIX_TIMESTAMP(date) as date FROM status WHERE resolved=0 ORDER BY date DESC");
$data = mysql_fetch_assoc($result);
while($row = mysql_fetch_assoc($result))
{
        $day = $row['date'];
        echo date("Y/m/j g:i:s",$day) ."<br />";
}
[/code]

i know there are 4 results.. mysql_num_rows confirms ( also checked via sql console).
but yet only 3 show up... am i missing something?
Link to comment
https://forums.phpfreaks.com/topic/27305-simple-sorting/#findComment-125861
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.