douzhicui2209 2015-10-27 09:50
浏览 45

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

I'm fetching data from a website with Json Objects and I will show it in table as the last images (check it bottom) but I'm really complicated with luckNumber and productName because I will create 5 column for this table and insert value to each Draw types.

Here is Json data

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)

This is PHP function to select all that data and generate tables

Function description:

luckNumber Contain a defference value depend on 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;
}

The incorrect result enter image description here

The result that I want to get 78087 This number for example only enter image description here

  • 写回答

1条回答 默认 最新

  • dsft8327 2015-10-27 10:49
    关注

    I would try first to collect all data in structure like this one:

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

    This way you're not dependent of whenever each DRAW data you get is sorted by time or not.

    Then you can easily output this table.

    Try something like this:

    // 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>';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法