Jump to content

Retrive from mysql by date


LanceyDavid

Recommended Posts

Hello,

this is my first time on this forum and also a modest newb on php, but i heard alot of people i know visit this site to get help on php. So i have a question, i know how to submit a form to mysql, the question is how do i recieve each indivual data i submit with the form by date and display it in order by date. i would like to submit kind of a status up everyday similar to twitter but order it by date and have it look kinda like:

 

July 5, 2006

 

This is a status example text that i will sumbit via form, links can be clicked and etc.

This is a status example text that i will sumbit via form, links can be clicked and etc.

This is a status example text that i will sumbit via form, links can be clicked and etc.

 

July 4, 2006

 

This is a status example text that i will sumbit via form, links can be clicked and etc.

This is a status example text that i will sumbit via form, links can be clicked and etc.

Link to comment
Share on other sites

You order the results by the date (so that all rows having the same date will be together in the result set.) Then in your presentation code you 'remember' what the last date is and when the date changes value you output the new date heading. Then you output the data. This results in a date heading followed by the data for that date.

 

Pseudo code -

$query = " your query string here.. ORDER BY date";  // assumes that your date column is a DATE data type so that ordering by it will sort the dates
$result = mysql_query($query);

// check if the query executed without error and if it returned any rows here...

// retrieve and process the data from the query
$last_date = ''; // initialize to a value that will never exist as data
while($row = mysql_fetch_assoc($result)){
    // test for a new date and output the date heading
    if($last_date != $row['date']){
        echo $row['date'] . "<br />";
        $last_date = $row['date']; // remember the new date
    }
    // output the data (under each date heading)
    echo $row['content'] . "<br />";
}

 

Link to comment
Share on other sites

sorry, reading back i did sound kind of rude... but i understand fully on how to output it just dont understand how to read it and display it using a html table, im a noob when it comes to coding so i read through these forums as well as tutorials to get a full understanding. i guess u can say what i dont get is how to display it correctly for it to look as how i explained it prior.. thanks again guys, trully appreciate it (sorry im such a rookie in this)

Link to comment
Share on other sites

So, did you use the pseudo code that someone posted to retrieve the rows from your database and output the date headings followed by the data under each date? That would be the first step.

 

Outputting the necessary HTML tags to start/close the table and to put table headers <tr><th>...</th></tr> around the date headers and table rows <tr><td>...</td></tr> around the data would then only involve minor changes to the code to produce the following -

 

<table>

<tr><th>Header row1</th></tr>
<tr><td>data row 1</td></tr>
<tr><td>data row 2</td></tr>
...
<tr><th>Header row2</th></tr>
<tr><td>data row 1</td></tr>
<tr><td>data row 2</td></tr>
...

</table>

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.