I need help with this code all i need is to display a string "0" to the empty hit data so what i have in mind is day 1 has 3 hits and day 2 has 0 hits, i really have no idea how to display the 0 please help me. thank you
days || Hits
1 - | | |- 3
2 - | | |-
3 - | | |-
4 - | | |-
5 - | | |- 5
here's the code:
$year = date("Y");
$month = $_POST['selected_date'];
if(!isset($month) or $_POST['selected_date'] == 0)
{
$month = date("n");
}
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for($i = 1; $i <= $num; $i++)
{
echo "<p >$i - ";
$query = mysql_query("SELECT DATE_FORMAT(`LookbookLogDate`, '%m') as 'month', DATE_FORMAT(`LookbookLogDate`, '%d') as 'day',
COUNT(`LookbookLogID`) as 'total'
FROM LookbookLog
where Lookbook_LookbookID = $LookbookID and LookbookLogDate like '%2013-03%'
GROUP BY DATE_FORMAT(`LookbookLogDate`, '%m'), DATE_FORMAT(`LookbookLogDate`, '%d')");
while($row = mysql_fetch_array($query))
{
$day = $row['day'];
$total = $row['total'];
if($day == $i) echo $total."</p>";
}
}
Try using the empty() function, the empty() function considers many things as a true value, for a list of these values have a look here http://php.net/manual/en/function.empty.php
$total = (empty($row['total'])?0:$row['total']);
Update:
You should only need to do the below to get the results you are looking for.
$total = ($row['total']?$row['total']:0);
if you do echo 0
it will display 0 on the screen, however if you do echo false
it will not display it. Now the problem that you have is not with displaying 0 but its in what value $total holds. If your query returns empty set then your $row['total']
is either false
or null
and therefore echo false
or echo null
will not display anything.
check to make sure your query returns something and that $total
holds the desired value using var_dump
to see the type of it.
You would need something like
$total = ! empty($row['total']) ? $row['total'] : 0;
Edit: Why not just make the field an int
and set the default value to 0
?
You don't show any code of how you build your table, but you would do:
$total = ((!$row['total'] ? 0 : $row['total']);
This code basically says "if $row['total']
is blank, make it zero. Otherwise, make the $total
equal to whatever was returned.
Your problem is that COUNT(...)
returns NULL instead of 0 if there are no results, which presumably PHP treats as an empty string when you output it. You can fix that by either adding an COALESCE
expression to your SQL (in my opinion, this is the better solution):
SELECT ... COALESCE(COUNT(`LookbookLogID`), 0) ...
Or check it in PHP:
echo $row['total'] ? $row['total'] : 0;
Or the multi-line version:
if($row['total'])
echo $row['total'];
else
echo 0;
Note that you don't need to do any comparisons or call empty()
, because PHP's type casting treats null, empty string and 0 as false.
I would get the zero from the database, since there is a universal SQL function which achieves a good solution:
SELECT
DATE_FORMAT(`LookbookLogDate`, '%m') as 'month',
DATE_FORMAT(`LookbookLogDate`, '%d') as 'day',
COALESCE(COUNT(`LookbookLogID`),0) as 'total'
FROM LookbookLog
{snip}
This means if there's no value in the results for COUNT() it will select 0 instead. Then your echo statements will proceed to work fine without checking especially for the NULL case in your application.