Reputation: 199
I want to display today's data on my website, but if today no data has been added yet, I want to show yesterday's data.
This is part of the code that I am using, and now it shows today AND yesterday if both data is available, but I want to show only today if both available.
I don't know, how to separate them. Can anybody point me into the right direction?
date_default_timezone_set('America/Los_Angeles');
$today = date('Y-m-d');
$yesterday = date('Y-m-d',strtotime("-1 days"));
$result = mysqli_query($bd, "SELECT * FROM apps WHERE datum = '".$today."' OR datum = '".$yesterday."' AND price > 0 ") or die(mysqli_error());
$count = mysqli_num_rows($result);
$cc = 0;
.....
$cc++;
if ($cc < $count) {
echo "\n";
Upvotes: 2
Views: 1857
Reputation: 817
You will need to add ORDER BY datum DESC LIMIT 1
to the end of your query and read a single row.
$query = "SELECT *
FROM apps
WHERE (datum = '".$today."'
OR datum = '".$yesterday."')
AND price > 0
ORDER BY datum DESC
LIMIT 1";
$result = mysqli_query($bd, $query) or die(mysqli_error());
if ($row = mysqli_fetch_row($result)) {
// You have the data here
}
Upvotes: 2
Reputation: 54
Try this:
date_default_timezone_set('America/Los_Angeles');
$today = date('Y-m-d');
$yesterday = date('Y-m-d',strtotime("-1 days"));
$result = mysqli_query($bd, "SELECT * FROM apps WHERE datum = '".$today."' AND price > 0 ") or die(mysqli_error());
$count = mysqli_num_rows($result);
if($count == 0) {
$result = mysqli_query($bd, "SELECT * FROM apps WHERE datum = '".$yesterday."' AND price > 0 ") or die(mysqli_error());
$count = mysqli_num_rows($result);
}
$cc = 0;
.....
$cc++;
if ($cc < $count) {
echo "\n";
Upvotes: 3