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