【问题标题】:JSON data array queryJSON数据数组查询
【发布时间】:2013-11-29 23:45:45
【问题描述】:

我正在使用 jsonTable 将数据解析到 Google 表中,并且工作正常。现在我有一个问题要同时添加多个查询并仅在已定义的数组的两列中显示数据。这是我的代码:

$data = mysql_query("SELECT reg.`oilchange`-SUM(gs.`Distance`) AS Nextoilchange FROM gs INNER JOIN reg ON (gs.`DeviceId`=25) AND (reg.`DeviceId`=25) INNER JOIN LOG ON TIME BETWEEN DATE(log.`lastoilchange`) AND CURDATE()")
or die(mysql_error()); 

$table = array();
$table['cols'] = array(
 array('label' => 'Vehicle', 'type' => 'number'),
array('label' => 'Distance Left', 'type' => 'number')

);
$rows = array();
while ($nt = mysql_fetch_array($data))

{

$temp = array();

$temp[] = array('v' => 'Nextoilchange');
$temp[] = array('v' =>$nt['Nextoilchange']);

// insert the temp array into $rows
$rows[]['c'] = $temp;

}
$table['rows'] = $rows;
$jsonTable = json_encode($table); 

我的第一个问题是我想在 $data 中添加多个查询,每个查询给出一个结果。我的下一个问题是我想在上面定义为距离左的列中显示多个查询的数据。在车辆列中,我想在 $temp(第一行)中添加像上面这样的静态数据。我搜索了很多,我很困惑如何做到这一点。请帮助我,我想这样显示表格:

Vehicle            Distance Left

nextoilchange      500
nextfilter         300
nextcheckup        400

【问题讨论】:

    标签: mysql sql json


    【解决方案1】:

    我没有对此进行测试。如果不起作用,请尝试这样的操作。

    <?php 
    
    $data[1] = mysql_query("SELECT reg.`oilchange`-SUM(gs.`Distance`) AS Nextoilchange FROM gs INNER JOIN reg ON (gs.`DeviceId`=25) AND (reg.`DeviceId`=25) INNER JOIN LOG ON TIME BETWEEN DATE(log.`lastoilchange`) AND CURDATE()")
    or die(mysql_error());
    $data[2] = mysql_query("SELECT reg.`oilchange`-SUM(gs.`Distance`) AS Nextoilchange FROM gs INNER JOIN reg ON (gs.`DeviceId`=25) AND (reg.`DeviceId`=25) INNER JOIN LOG ON TIME BETWEEN DATE(log.`lastoilchange`) AND CURDATE()")
    or die(mysql_error());
    
    $table[1]['cols'] = array(array('label' => 'Vehicle', 'type' => 'number'),array('label' => 'Distance Left', 'type' => 'number'));
    $table[2]['cols'] =  array(array('label' => 'Vehicle', 'type' => 'number'),array('label' => 'Distance Left', 'type' => 'number'));
    
    foreach($table as $key=>$eachtable)
    {
        $cols = $eachtable['cols'];
    
        while ($nt = mysql_fetch_array($newdata))
        {
    
            foreach($cols as  $key1=>$each_col)
            {
                if($each_col['label'] == $nt)  //you have match the conditions
                {
                    $row[$key1] = $nt['yourvalue'];
                }
                else
                {
                    $row[$key1] = $nt['yourvalue1'];
                }
                $rows[] = $row;
            }
        }
        $table[$key]['rows'] = $rows;
    }
    $jsonTable = json_encode($table); 
    

    【讨论】:

    • 感谢您的回复。我已经编辑了我的帖子,现在我已经提到了表格的外观。您的代码很有帮助,但我很困惑,因为在 Vehicle 列中我想显示未存储在数据库中的字符串值,并且查询结果将显示在距离左列中。请告诉我该怎么做?
    猜你喜欢
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 2021-07-15
    • 1970-01-01
    • 2021-01-26
    相关资源
    最近更新 更多