简体   繁体   中英

Echo 0 for Mysql query with no results

I'm building an array of the number of calls I have for a particular client from a mysql db on a running list of the last 30 days. The code I have so far works for adding the days that have calls to the array but I need a show a '0' for the days that have no calls (no entries in the db). Here is me code so far:

$query="SELECT COUNT(*) FROM my_db WHERE client_phone='clint_phone#' GROUP BY calldate"; 
$result = mysql_query($query);
    $data = array();
    while ($row = mysql_fetch_row($result)) {
       $data[] = $row[0];
    }

I just need a way to show if today I had 30 calls and yesterday I had 0, I need it to show [30,0]. What I have only would show [30].

EDIT * I have a mysql db will columns client_phone, calldate. Im looking to build a graph using the data in an array. Each point of the graph will represent a day and the number of calls for that client on that day. Im building the above query to populate that array. I'm trying to count backwards thirty days and feed the total calls for each day into the array.

EDIT 2* I've got it almost there. I'm getting a problem in the 'foreach' area. Below is the code with two print_r()'s to dump the array. The first one looks good, but the second one shows some array entries getting over-written that shouldn't be:

$query="SELECT calldate, COUNT(*) FROM my_db WHERE client_phone='phone#' and calldate>='20130101' AND calldate<='20130107' GROUP BY calldate ORDER BY calldate"; 
$result = mysql_query($query);
    $data = array();
    while ($row = mysql_fetch_array($result)) {
      $data[$row['calldate']] = $row[1];
    }

$startDate = '20130101'; 
    $endDate = '20130107'; 
    $dates = array(); 

    for($current = $startDate; $current != $endDate; $current = date('Ymd', strtotime("$current +1 day"))) {
        $dates[] = $current; 
    }
    $dates[] = $endDate;  

print_r ($data);
echo "<br />";

foreach($dates as $date){
if (in_array($date, $data)) {
    // that date was found in your db_date array(therefore had queries)
}else{
$data[$date] = 0; //date was not found in your db_array so we set that date with no   queries to zero
}
}

print_r ($data);

I run this in a browser and I get this:

Array ( [20130101] => 1 [20130104] => 6 [20130105] => 2 [20130106] => 1 [20130107] => 3 ) 
Array ( [20130101] => 0 [20130104] => 0 [20130105] => 0 [20130106] => 0 [20130107] => 0 [20130102] => 0 [20130103] => 0 )

The top output looks good, just missing a zero assigned to dates not in the data[] array. The second array has zero's in the missing dates, but other overwrited that shouldn't have been.

Thanks for any help!

Finding every date that is in that timespan and doing a task for that does not really that standard of a solution. But depending if your host allows cron-tasks; if you were able to use cron tasks to automatically insert a 0 into your database at 11:59pm for that date if no querys were made that day; you could simply extract all dates.

If you do not want to do it with cron tasks I think you can manage it this way...

      $startDate = '2009-01-28'; 
        $endDate = '2009-02-04'; 
        $dates = array(); 

        for($current = $startDate; $current != $endDate; $current = date('Y-m-d', strtotime("$current +1 day"))) {
            $dates[] = $current; 

        $dates[] = $endDate;  
    }

then do this

foreach($dates as $date){
if (array_key_exists($date, $data)) {
    // that date was found in your db_date array(therefore had queries)
}else{
$data[$date] = 0; //date was not found in your db_array so we set that date with no queries to zero
}
}

This may need some minor adjustments because I have not tested it; but this should work; if not something very close to it should. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM