0

how can i generate something like this in a loop?:

$data = array(
        'cols' => array(
                array('id' => '', 'label' => 'Year', 'type' => 'string'),
                array('id' => '', 'label' => 'diastolisch', 'type' => 'number'),
                array('id' => '', 'label' => 'systolisch', 'type' => 'number')
        ),
        'rows' => array(
                array('c' => array(array('v' => '1990'), array('v' => 150), array('v' => 100))),
                array('c' => array(array('v' => '1995'), array('v' => 300), array('v' => 50))),
                array('c' => array(array('v' => '2000'), array('v' => 180), array('v' => 200))),
                array('c' => array(array('v' => '2005'), array('v' => 400), array('v' => 100))),
                array('c' => array(array('v' => '2010'), array('v' => 300), array('v' => 600))),
                array('c' => array(array('v' => '2015'), array('v' => 350), array('v' => 400)))
        )
);
$chart->load(json_encode($data));

im already rendering a table with the same data like this:

 <?php foreach ($dataSets as $dataSet): ?>
    <tr>
        <td><?php echo $dataSet['DataSet']['id']; ?></td>
        <td>
            <?php echo $this->Html->link($dataSet['DataSet']['user_id'],
array('controller' => 'dataSets', 'action' => 'view', $dataSet['DataSet']['id'])); ?>
        </td>
        <td><?php echo $dataSet['DataSet']['created']; ?></td>
 <td>  <p><small><?php echo $dataSet['DataSet']['diastolisch']; ?></small></p></td>
       <td>  <p><small><?php echo $dataSet['DataSet']['systolisch']; ?></small></p></td>
         <td>  <p><small> <?php echo $dataSet['DataSet']['puls']; ?></small></p></td>
           <td>  <p><small><?php echo $dataSet['DataSet']['gewicht']; ?></small></p></td>
           <td>  <p><small><?php echo $dataSet['DataSet']['comment']; ?></small></p></td>

    </tr>
    <?php endforeach; ?>
    <?php unset($dataSet); ?>

update:this is the loop for now, still not working

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

        // Labels for your chart, these represent the column titles.
        /* 
            note that one column is in "string" format and another one is in "number" format 
            as pie chart only required "numbers" for calculating percentage 
            and string will be used for Slice title
        */

                array('id' => '', 'label' => 'Zeitpunkt', 'type' => 'string'),
                array('id' => '', 'label' => 'diastolisch', 'type' => 'number'),
                array('id' => '', 'label' => 'systolisch', 'type' => 'number')
        );

        /* Extract the information from $result */
        foreach($dataSets as $r) {
            $tempCreated[] = array('v' => $r['DataSet']['created']); 
            $tempSystolisch[] = array('v' => $r['DataSet']['systolisch']); 
            $tempDiastolisch[] = array('v' => $r['DataSet']['systolisch']); 
            $rows[] = array('c' => array(array('v' => $tempCreated), array('v' => $tempSystolisch), array('v' => $tempDiastolisch)));

        }

    $table['rows'] = $rows;

    // convert data into JSON format
    $chart->load(json_encode($table));
thegrunt
  • 1,054
  • 1
  • 11
  • 22
  • foreach($result as $r) { $temp = array(); // the following line will be used to slice the Pie chart $temp[] = array('v' => (string) $r['weekly_task']); // Values of each slice $temp[] = array('v' => (int) $r['percentage']); $rows[] = array('c' => $temp); } $table['rows'] = $rows; // convert data into JSON format $jsonTable = json_encode($table); – thegrunt Oct 29 '13 at 19:49
  • 2
    What is this? Please edit into your question if it's part of the problem, or add it as an answer if it's a solution. It's OK to answer your own question. –  Oct 29 '13 at 19:52
  • http://stackoverflow.com/questions/12994282/php-mysql-google-chart-json-complete-example?rq=1 this is a good start – thegrunt Oct 29 '13 at 20:13

1 Answers1

0

somehow, this does the trick:

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

        /* 
            note that one column is in "string" format and another one is in "number" format 
            as pie chart only required "numbers" for calculating percentage 
            and string will be used for Slice title
        */

                array('id' => '', 'label' => 'Zeitpunkt', 'type' => 'string'),
                array('id' => '', 'label' => 'diastolisch', 'type' => 'number'),
                array('id' => '', 'label' => 'systolisch', 'type' => 'number'),
                 array('id' => '', 'label' => 'gewicht', 'type' => 'number'),
                 array('id' => '', 'label' => 'puls', 'type' => 'number')
        );

        foreach($dataSets as $r) {
            $tempCreated = (string)$r['DataSet']['created']; 
            $tempSystolisch = (int)$r['DataSet']['systolisch']; 
            $tempDiastolisch = (int)$r['DataSet']['diastolisch']; 
            $tempGewicht = (int)$r['DataSet']['gewicht']; 
            $tempPuls = (int)$r['DataSet']['puls']; 
            $rows[] = array('c' => array(array('v' => $tempCreated), array('v' => $tempSystolisch), array('v' => $tempDiastolisch),array('v' => $tempGewicht),array('v' => $tempPuls)));

        }
    $table['rows'] = $rows;

    $chart->load(json_encode($table));
thegrunt
  • 1,054
  • 1
  • 11
  • 22