0

我怎样才能在循环中生成这样的东西?:

$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));

我已经渲染了一个具有相同数据的表,如下所示:

 <?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); ?>

更新:这是现在的循环,仍然无法正常工作

$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));
4

1 回答 1

0

不知何故,这可以解决问题:

$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));
于 2013-10-29T21:09:23.793 回答