2015-10-27 29 views
0

我從JSON對象網站獲取數據,我會告訴它在表 作爲最後的圖像(檢查底部),但我真的luckNumber產品名稱複雜,因爲我將爲此表創建5列,並向每個繪製類型插入值。如何使用PHP在Foreach中生成表列?

這裏是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內含一百米運動員值取決於產品名稱(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

我想要的結果獲得 這個數字僅作爲舉例 enter image description here

回答

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>'; 
} 
+0

我想作爲上述圖像 –

+0

你應該 - 在某種程度上,這有什麼不同? –