Reputation: 161
I have this simple code that collects data from my mysql database:
$get = mysql_query("SELECT * FROM table ORDER BY dato ASC") or die(mysql_error());
if(mysql_num_rows($get)) {
while($show = mysql_fetch_array($get)) {
}
} else {
echo "No results";
}
But the list is now over 200 items, and i would like to sort them on dates, så that i have forexample.
26/08-2015
1. line
2. line
3. line
4. line
27/08-2015
5. line
6. line
7. line
Instead of
1. line
2. line
3. line
4. line
5. line
6. line
7. line
Which i get now
The date i sort after is a mktime date.
$dato = mktime(0, 0, 0, date('m'), date('d'), date('y'));
Upvotes: 1
Views: 43
Reputation: 1594
Use this,
$get = mysql_query("SELECT DISTINCT(DATE(`date`)) as dt, t.* FROM table t ORDER BY 1") or die(mysql_error());
if(mysql_num_rows($get)) {
$prevDT = "";
while($show = mysql_fetch_array($get)) {
if (strlen($prevDT) < 0 || strcmp($prevDT, $show['dt']) != 0){
echo("<br />");
echo($show['dt']."<br />");
$prevDT = $show['dt'];
}
echo($show['field']."<br />");
}
} else {
echo "No results";
}
Upvotes: 1
Reputation: 5157
Use this
$currentDate = null;
$get = mysql_query("SELECT * FROM table ORDER BY dato ASC") or die(mysql_error());
if(mysql_num_rows($get)) {
while($show = mysql_fetch_array($get)) {
if($currentDate != $show['dato'])
echo "<BR><BR>";
$currentDate = $show['dato'];
}
} else {
echo "No results";
}
Upvotes: 1
Reputation: 6755
try this with where condition
$dato = mktime(0, 0, 0, date('m'), date('d'), date('y'));
$get = mysql_query("SELECT * FROM table WHERE date > $dato ORDER BY date ASC") or die(mysql_error());
if(mysql_num_rows($get)) {
while($show = mysql_fetch_array($get)) {
}
} else {
echo "No results";
}
Upvotes: 1