-3

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>";
        }


    }
Louie Jay Jayme
  • 169
  • 3
  • 13
  • 3
    [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Mar 26 '13 at 16:06

5 Answers5

1

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.

deed02392
  • 4,799
  • 2
  • 31
  • 48
0

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);
llanato
  • 2,508
  • 6
  • 37
  • 59
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;
GGio
  • 7,563
  • 11
  • 44
  • 81
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.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
What have you tried
  • 11,018
  • 4
  • 31
  • 45
0

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.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188