Jump to content


Photo

Organizing Data From Database


  • Please log in to reply
6 replies to this topic

#1 Aftermath

Aftermath
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 29 June 2006 - 08:34 PM

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.

    <?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 />";
}

                ?>


#2 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 29 June 2006 - 08:37 PM

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.

#3 Aftermath

Aftermath
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 29 June 2006 - 08:45 PM

ok. I think I will be able to figure it out. thanks ^_^

#4 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 29 June 2006 - 08:48 PM

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

#5 Aftermath

Aftermath
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 29 June 2006 - 09:43 PM

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?

#6 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 29 June 2006 - 10:32 PM

$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;
  }

}

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.

#7 Aftermath

Aftermath
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 30 June 2006 - 06:30 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users