Organising by day

Hello

I got a select query that outputs a bunch of results. All the results have a timestamp column.

How can I group these results by days and wrap each day into a div?

e.g

<div>
result 1 - day1
result 2 - day1
result 3 - day1
</div>

<div>
result 4 - day2
result 5 - day2
</div>

Order them by the timestamp column in your query. Then loop through the result set in PHP and create your output.

Hi I have already ordered them by timestamp and created my output. Currently my output is this:

result 1 - day1
result 2 - day1
result 3 - day1
result 4 - day2
result 5 - day2

But I want to groups the days into divs like this:

<div>
result 1 - day1
result 2 - day1
result 3 - day1
</div>

<div>
result 4 - day2
result 5 - day2
</div>

Could you post your code please?

Hi, it is a very basic while loop.

$loop = mysql_query(“SELECT * FROM info”);

while ( $row = mysql_fetch_array($loop) {
echo $row[‘result’];
}

anyone?

Show us what the column names are (rather than *) and how you are formatting your timestamp to show “day1”.

Hello

This is my timestamp format: 2012-02-05 12:12:58

this is my query, with the column name.

$loop = mysql_query(“SELECT result FROM info”);

while ( $row = mysql_fetch_array($loop) {
echo $row[‘result’];
}

So how are you currently turning this:

echo $row[‘result’];

into lines like:

result 1 - day1

Hi,

I used that as just an example.

I have a one colum that needs to be outputted. The date wont be outputted.
I want to wrap all the divs that come under one day into a div.

So for example,

if
result 1
result 2
result 3
are on 11th may 2011

and
result 4
result 5
result 6 are on 13th may 2011

the result would be:

<div>
result 1
result 2
result 3
</div>

<div>
result 4
result 5
result 6
</div>

Well if you want PHP to be able to format the results in terms of days you need to collect both the timestamp and the result so show us the output of putting this sql statement into your database management tool (PhpMyAdmin or whatever you use) where info is the name of your table.

SHOW CREATE TABLE info

or

DESCRIBE info

hello

my timestamp is stored in a ‘timestamp’ column. So the query would be…

$loop = mysql_query(“SELECT result, timestamp FROM info”);

while ( $row = mysql_fetch_array($loop) {
echo $row[‘result’];
}

Pseudocoding it.

$curdate = ‘’
if num_rows > 0 {
echo <div>
while row = fetch_array {
$date = date(‘dmY’,result)
if ($date != $curdate && $curdate != ‘’) { echo </div><div> }
$curdate = $date;
}
echo </div>
}