I'm trying to prepare code which will run every hour and retrieve some DB data. I would like to store it as csv file and use it to create chart from highcharts. Problem: I'm unable to create csv file which will be updated every hour according to this model:
01:00, 02:00, 03:00
dataA-01, dataA-02, dataA-03
dataB-01, dataB-02, dataB-03
So next column '04:00' with rows dataA-04 and dataB-04 will be added on 4am.
What I got:
class liveData
{
public $newCsvData = array();
/*some sql here*/
public function getHour()
{
$hour = date('H:i');
array_push($this->newCsvData, $hour);
}
public function getDataA()
{
/*...*/
array_push($this->newCsvData, $dataA);
}
public function getDataB()
{
/*...*/
array_push($this->newCsvData, $dataB);
}
public function saveCsv()
{
print_r($this->newCsvData);
$fp = fopen('./tmp/test.csv', 'a+');
fputcsv($fp, $this->newCsvData, ',');
fclose($fp);
}
}
The result of print_r($this->newCsvData) is:
Array
(
[0] => 21:00
[1] => 12
[2] => 40
)
The code works, but created test.csv looks of course like this:
21:00,12,40
21:01,15,45
21:03,18,62
and I need to get:
21:00,21:01,21:03
12,15,18
40,45,62
My problem might seems trivial, but really - and unfortunately - I've spent hours on this. I just can't imagine the proper way. Even if I'm able to get something like
21:00
12
40
Data on next hour is added to new line like this.
21:00
12
40
21:01
15
45
Should I use multidimensional array? Should I change fopen() from a+ to other option?
Can you please give me a hint?
In my opinion, from a logical perspective, I would use per row the output you have on this moment: dd-mm;data,data,... Is it logical to have the times on the X-axis? This would also mean you can use the append modus.
If you really want to keep the desired output then I would create an array per row something like:
$arr[0][] = '21:00';
$arr[1][] = 'data_b';
$arr[0][] = 'data_b';
$new_data = '';
foreach($arr as $row)
{
$new_data .= implode(',', $row);
}
Note that you cannot use the append modus in this last situation that easily and that this might result in the need to retrieve more data from the database...
print_r($this->newCsvData) shoud be look like this:
$data = array(
array( '21:00' ),
array( '12' ),
array( '40' ),
);
and in second step, shoud be look like this:
$data = array(
array( '21:00', '21:01' ),
array( '12', '15' ),
array( '40', '40' ),
);
and third step, same:
so who you can do this. Each time you import all data from CSV file and push each cell to the corresponding array and look like this
$newArray = array(array(), array(), array());
foreach($dataFromCSV as $data)
{
array_push($newArray[0][], $data[0]);
array_push($newArray[1][], $data[1]);
array_push($newArray[2][], $data[2]);
}
and then append the new record data in $newArray. delete the old data from CSV file and write on it with new data from $newArray.
:) enjoy
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.