我正在建立一个数组的特定客户端从最近30天的运行列表上的MySQL数据库中调用的数量。我迄今为止的代码用于添加调用数组的日子,但我需要在没有调用的日子(db中没有条目)显示'0'。这里是我到目前为止的代码:Echo 0为Mysql查询没有结果
$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];
}
我只是需要一种方式来显示,如果今天我有30个电话和我昨天有0,我需要它来显示[30,0]。我只会显示[30]。
编辑*我有一个MySQL数据库将列client_phone,calldate。我正在寻找使用数组中的数据构建图。图表中的每个点都表示当天以及该客户在该日的呼叫次数。我建立上述查询来填充该数组。我试图向后倒数30天,并将每天的总呼叫数量输入到数组中。
编辑2 *我几乎在那里。我在'foreach'区域遇到问题。下面是用两个print_r()来转储数组的代码。第一个看起来不错,但第二个显示了一些数组项获得过写这不应该是:
$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);
我在浏览器中运行这个,我得到这个:
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)
的顶部输出看起来不错,只是遗漏了分配给data []数组中日期的零。第二个数组在缺失的日期中有零,但是其他的应该不是这样。
感谢您的帮助!
我不知道你的数据库结构是如何从我的上下文我不知道你的意思是每天的查询......是表中的变量之一?请进一步阐述。你的意思是说,不在数组中的每个日期都没有调用,并且你想为这些日子中的每一天插入一个0到数组中? –
增加了更多细节 – Milksnake12
你能显示'my_db'结构和条目吗? –