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?
Jaynesh
February 10, 2012, 9:17pm
5
Hi, it is a very basic while loop.
$loop = mysql_query(“SELECT * FROM info”);
while ( $row = mysql_fetch_array($loop) {
echo $row[‘result’];
}
Cups
February 11, 2012, 9:50pm
7
Show us what the column names are (rather than *) and how you are formatting your timestamp to show “day1”.
Jaynesh
February 12, 2012, 1:37pm
8
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’];
}
Cups
February 12, 2012, 2:06pm
9
So how are you currently turning this:
echo $row[‘result’];
into lines like:
result 1 - day1
Jaynesh
February 12, 2012, 2:14pm
10
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>
Cups
February 13, 2012, 4:40pm
11
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
Jaynesh
February 16, 2012, 12:58pm
12
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>
}