如何使用PHP在Foreach中生成表列?

时间:2015-10-27 09:50:13

标签: php json

我使用Json Objects从网站上获取数据,我将在表格中显示 作为最后的图片(检查它的底部),但我真的很复杂的 luckNumber productName 因为我将为此表创建5列,并为每个 Draw 类型插入值。

这是Json数据

object(stdClass)[5]
  public 'luckNumber' => string '78087' (length=5)
  public 'productJson' =>
    object(stdClass)[4]
      public 'luckDrawDesc' => string 'DRAW3' (length=5)
      public 'validPeriod' => int 2
  public 'productName' => string 'DRAW3' (length=5)

object(stdClass)[5]
  public 'luckNumber' => string '78087' (length=5)
  public 'productJson' =>
    object(stdClass)[4]
      public 'luckDrawDesc' => string 'DRAW4' (length=5)
      public 'validPeriod' => int 2
  public 'productName' => string 'DRAW4' (length=5)

object(stdClass)[5]
  public 'luckNumber' => string '78087' (length=5)
  public 'productJson' =>
    object(stdClass)[4]
      public 'luckDrawDesc' => string 'DRAW3' (length=5)
      public 'validPeriod' => int 2
  public 'productName' => string 'DRAW3' (length=5)

object(stdClass)[3]
  public 'luckNumber' => string '78087' (length=5)
  public 'productJson' =>
    object(stdClass)[2]
      public 'luckDrawDesc' => string 'DRAW2' (length=5)
      public 'validPeriod' => int 2
  public 'productName' => string 'DRAW2' (length=5)

object(stdClass)[1]
  public 'luckNumber' => string '78087' (length=5)
  public 'productJson' =>
    object(stdClass)[0]
      public 'luckDrawDesc' => string 'DRAW1' (length=5)
      public 'validPeriod' => int 2
  public 'productName' => string 'DRAW1' (length=5)

这是用于选择所有数据并生成表的PHP函数

功能说明:

luckNumber 包含一个取决于 productName (DRAW1,DRAW2,DRAW3,DRAW14)的差异值。

public function det_data() {

    $c_arr = json_decode($this->urlData);
    $i = 0;
    $html = '<table>';
    $html .= '<thead>';
    $html .= '<th>Date</th>';
    $html .= '<th>Draw1</th>';
    $html .= '<th>Draw2</th>';
    $html .= '<th>Draw3</th>';
    $html .= '<th>Draw4</th>';
    $html .= '</thead>';
    $html .='<tbody>';
    foreach ($c_arr as $items) {
        if ($i == 30)
            break;
        {

            $html .= '<tr>';
            if ($items->productName =='DRAW1') {
                $html .= '<td>' . $items->createTime . '</td>';
                $html .= '<td>' . $items->luckNumber . '</td>';  
            } 
            if ($items->productName =='DRAW2') {
                $html .= '<td>' . $items->luckNumber. '</td>';  
            } 
            if ($items->productName =='DRAW3') {
                $html .= '<td>' . $items->luckNumber. '</td>';  
            } 
            if ($items->productName =='DRAW4') {
                $html .= '<td>' . $items->luckNumber. '</td>';  
            } 
            $html .= '</tr>';

            $i++;
        }
    }
    $html .='</tbody>';
    $html .= '</table>';
    return $html;
}

结果不正确 enter image description here

我想要的结果 78087 此数字仅供参考 enter image description here

1 个答案:

答案 0 :(得分:0)

我会首先尝试收集结构中的所有数据,如下所示:

array (unix_timestamp_of_date => array('DRAW1' => xxxxx, 'DRAW2' => xxxxx, 'DRAW3' => xxxxx, 'DRAW4' => xxxxx))

这样,您不依赖于每次获得的每个抽奖数据是否按时间排序。

然后您可以轻松输出此表。

尝试这样的事情:

// 1. collect data
$output = array();
foreach ($c_arr as $items) {
    $timestamp = strtotime($items->createTime);
    if (!isset($output[$timestamp])) $output[$timestamp] = array();

    $output[$timestamp][$items->productName] = $items->luckNumber;
}

// uncomment below line if you want to sort your results by time
//ksort($output);


// 2. print data
foreach ($output as $timestamp => $data) {
    $html .= '<tr>';
    $html .= '<td>'.date('Y-m-d H:i:s', $timestamp).'</td>';
    for ($draw = 1; $draw <= 4; $draw++) {
        $html .= '<td>';
        $html .= isset($data['DRAW'.$draw]) ? $data['DRAW'.$draw] : 0; // will display 0 if there is no lucky number
        $html .= '</td>';
    }
    $html .= '</tr>';
}