0

我有以下代码:

$rows = array(); 
$table = array();
$table['cols'] = array(

    array('label' => 'Date', 'type' => 'string'),
    array('label' => 'Charging Current', 'type' => 'number'),
    array('label' => 'Battery Voltage', 'type' => 'number'),    
    array('label' => 'Charging Power', 'type' => 'number'),
    array('label' => 'Rotation', 'type' => 'number'),
);

并想知道如何更改它,以便能够更改列数。显示列,取决于变量(类似这样):

$rows = array(); 
$table = array();
$table['cols'] = array(
   array('label' => 'Date', 'type' => 'string'),
if ($current == "true") {**
    array('label' => 'Charging Current', 'type' => 'number'),
}
if ($voltage == "true") {
    array('label' => 'Battery Voltage', 'type' => 'number'),    
}
if ($power == "true") {
    array('label' => 'Charging Power', 'type' => 'number'),
}
if ($rotation == "true") {
    array('label' => 'Rotation', 'type' => 'number'),
}
);

$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $temp = array();
    // the following line will be used to slice the chart
    $temp[] = array('v' => (string) $r['TimeStamp']); 

    // Values of each slice
    if ($current == "true") {
        $temp[] = array('v' => (int) $r['ChargingCurrent']); 
        $rows[] = array('c' => $temp);
    }
    if ($voltage == "true") {
        $temp[] = array('v' => (int) $r['BatteryVoltage']); 
        $rows[] = array('c' => $temp);  
    }
    if ($power == "true") {
        $temp[] = array('v' => (int) $r['ChargingPower']); 
        $rows[] = array('c' => $temp);
    }
    if ($rotation == "true") {
        $temp[] = array('v' => (int) $r['Rotation']); 
        $rows[] = array('c' => $temp);  
    }
}

我希望很清楚我要做什么。请帮我。

4

1 回答 1

0

因此,您可以轻松地将项目添加到$tables['col']数组中。要将某些内容添加到数组中,请执行以下操作:

$table['cols'][] = 'something';

在您的情况下,您的代码将如下所示:

$rows = array();
$table = array();
$table['cols'] = array(
    array('label' => 'Date', 'type' => 'string')
);

if ($current == "true") {
    $table['cols'][] = array('label' => 'Charging Current', 'type' => 'number');
}
if ($voltage == "true") {
    $table['cols'][] = array('label' => 'Battery Voltage', 'type' => 'number');
}
if ($power == "true") {
    $table['cols'][] = array('label' => 'Charging Power', 'type' => 'number');
}
if ($rotation == "true") {
    $table['cols'][] = array('label' => 'Rotation', 'type' => 'number');
}
);
于 2016-05-28T22:18:32.330 回答