Jump to content

Archived

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

Aftermath

Organizing Data From Database

Recommended Posts

Hello all. What i'm trying to do here is make it so that when it reads the info from the database it will orgranize it date.

EX:

June 28 2006
*Whatever title*
*Second title if there is one*
June 27 2006
*Another title*

Below is my code of what i have so far... Any help would be much appreciated.

[code]    <?php
mysql_connect ('localhost', ' ', ' ');
mysql_select_db (' ');

$result = mysql_query("SELECT * FROM post_table ORDER BY id DESC LIMIT 10");

while ($row = mysql_fetch_array($result)) {
       $id = $row['id'];
        $title = $row['title'];
        $user = $row['username'];
    $date = date("d/n/y", $row['timestamp']);
                      
        echo "<a href='entries.php?id=$id'>($date) $title - $user</a><br />";
}

                ?> [/code]

Share this post


Link to post
Share on other sites
easy, convert your dates to Unix timestamp format using strtotime(), load all of your relative data into a temp assoc. array and then sort the array based on the timestamp element.

Share this post


Link to post
Share on other sites
good that's what we like, people who are willing to try to do it themselves instead of asking for code handed to them. if you can't figure it out or have problems along the way, please feel free to ask and we can elaborate on the process but as long as you understand the logic and the ways to get there, you should be able to fight your way through it. again, please feel free to post again for further troubles. =D

Share this post


Link to post
Share on other sites
I can't for the life of my figure out how to put it into an array. I have gone to the php.net site and read through several pages of about array's and read the strtotime page. I understand the logic i need to make this page, but I just don't have the expereince or knowledge to do it.
Does anyone have a link to a tutorial or something similar to this?

Share this post


Link to post
Share on other sites
[code]$tempArray = array();
while ($row = mysql_fetch_assoc($result)) {
  # Here we write all of your associated row values to a multidimensional array with the timestamp being the parent element in it's own dimension so we can run a sort based off of the integer value of the unix timestamp represented by converting your date to secs.
  foreach ($row as $key => $val) {
    $timestamp = strtotime($row['timestamp']);
    $tempArray[$timestamp]][$row[$key]] = $val;
  }

}[/code]

now that i've showed you how to convert your data into a unix timestamp and add your data into the multi-dimensional array, now just figure out how to sort that array based off of the 1st dimension element.

Share this post


Link to post
Share on other sites
I don't suppose you can post that link again. It seems it is gone and I neglected to bookmark it last nite.

Share this post


Link to post
Share on other sites

×

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.