Wondering if you can help me. I am querying a table to populate a graph with a H axis of time and V axis of count and im trying to dynamically add filters to the graph based on servers in the database table. At this moment its static.
想知道你是否可以幫助我。我正在查詢一個表來填充一個H軸為時間軸和V軸計數的圖形,我試圖根據數據庫表中的服務器動態地將過濾器添加到圖形中。此刻它的靜止。
i use the following code to query database and add the series to the graph
我使用以下代碼查詢數據庫並將該系列添加到圖表中
$results = array();
foreach( \IPS\Db::i()->select( '*', 'stats', $where, 'time ASC' ) as $row )
{
$value = '_'.$row['server'];
if( !isset( $results[ $row['time'] ] ) )
{
$results[ $row['time'] ] = array(
'time' => $row['time'],
'_64' => 0,
'_66' => 0
);
}
//$results[ $row['time'] ][$value] = 0;
if ($value == '_64')
{
$results[ $row['time'] ]['_64'] = $row['value_1'];
}
elseif($value == '_66')
{
$results[ $row['time'] ][ '_66' ] = $row['value_1'];
}
}
return $results;
and add the filters
並添加過濾器
$chart->addSeries('_66', 'number');
$chart->addSeries('_64', 'number');
I have tried to make it dynamic by doing
我試圖讓它變得動態
foreach(\IPS\Db::i()->query( "SELECT DISTINCT server FROM stats ORDER BY server" ) as $row)
{
$value = '_'.$row['server'];
$chart->addSeries($value, 'number');
}
and
和
$value = '_'.$row['server'];
if( !isset( $results[ $row['time'] ] ) )
{
$results[ $row['time'] ] = array(
'time' => $row['time'],
$value => 0,
);
}
$results[ $row['time'] ][$value] = $row['value_1'];
But this did not work in any way. I think im going about adding to the array completely wrong. Any suggestions please?
但這沒有任何作用。我認為即時添加到數組完全錯誤。有什么建議嗎?
array should look like this
數組應該是這樣的
array (size=8)
1504025011 =>
array (size=3)
'time' => int 1504025011
'_64' => int 2
'_66' => int 0
1504094803 =>
array (size=3)
'time' => int 1504094803
'_64' => int 0
'_66' => int 14
but instead
但反而
array (size=8)
1504025011 =>
array (size=2)
'time' => int 1504025011
'_64' => int 2
1504094803 =>
array (size=2)
'time' => int 1504094803
'_66' => int 14
1 个解决方案
#1
1
I managed to make it work in the end. Was a simple in the end. (Normally the case) Sure i tried this before and didn't work. Must have missed something.
我設法最終使它工作。最后很簡單。 (通常情況下)當然我以前試過這個並且沒有用。一定錯過了什么。
I used this code to add keys/value to the array.
我使用此代碼向數組添加鍵/值。
foreach(\IPS\Db::i()->query( "SELECT DISTINCT(server) AS Server FROM stats ORDER BY Server DESC;" ) as $rows)
{
$value = '_' .$rows['Server'];
$results[ $row['time'] ][$value] = 0;
}