2013-08-28 60 views
0

現在我很難將所有的值放入關聯數組中。 因爲我只能從查詢中獲取最後一個值。並非所有的價值。我無法發現錯誤。 這裏是我的代碼:使用關聯數組方法獲取所有數組值的錯誤

$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_orders.total AS total, cscart_order_data.data AS data_serialize, cscart_orders.timestamp AS date, cscart_orders.status AS status FROM cscart_orders 
            LEFT JOIN cscart_order_data 
            ON cscart_orders.order_id = cscart_order_data.order_id 
            WHERE cscart_order_data.type = 'I' 
            AND cscart_orders.timestamp BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') AND UNIX_TIMESTAMP('2013-01-31 23:59:59') limit 10 
            "; 

     $resultGetSerialize = $this->db->query($sqlGetSerializedValues); 

     echo "<pre>"; 
     $var_data = array(); 

     foreach($resultGetSerialize->result_array() as $row1){ 

      $var_data[] = array(
          'id' => $row1['order_id'], 
          'total' => $row1['total'], 
          'status' => $row1['status'], 
          'data' => unserialize($row1['data_serialize']), 
          'date' => $row1['date'] 
         );    

     } 

     $range = array(); 

     foreach($var_data as $data){ 

       $id = $data['id']; 
       $total = $data['total']; 
       $cost = $data['data']['cost']; 
       $var_date = $data['date']; 
       $status => $data['status']; 

       $date_var = date('Y-m-d H:i:s',$var_date); 

       $grand_total = $total + $cost; 
       $cost_ratio = ($cost/$grand_total) * 100; 
       $paid_ratio = ($total/$grand_total) * 100; 

       $range[] = $cost_ratio; 

       $test = array(
        'id' => $data['id'], 
        'ratio' => $cost_ratio, 
        'status' => $status 
       ); 

      } 

     echo "</table>"; 

     print_r($test); //this will return the last index from my array 

是什麼;我的問題,大家好我希望你能幫助我。謝謝。

回答

2
$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_orders.total AS total, cscart_order_data.data AS data_serialize, cscart_orders.timestamp AS date, cscart_orders.status AS status FROM cscart_orders 
            LEFT JOIN cscart_order_data 
            ON cscart_orders.order_id = cscart_order_data.order_id 
            WHERE cscart_order_data.type = 'I' 
            AND cscart_orders.timestamp BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') AND UNIX_TIMESTAMP('2013-01-31 23:59:59') limit 10 
            "; 

     $resultGetSerialize = $this->db->query($sqlGetSerializedValues); 

     echo "<pre>"; 
     $var_data = array(); 

     foreach($resultGetSerialize->result_array() as $row1){ 

      $var_data[] = array(
          'id' => $row1['order_id'], 
          'total' => $row1['total'], 
          'status' => $row1['status'], 
          'data' => unserialize($row1['data_serialize']), 
          'date' => $row1['date'] 
         );    

     } 

     $range = array(); 
     $test = array(); 
     foreach($var_data as $data){ 

       $id = $data['id']; 
       $total = $data['total']; 
       $cost = $data['data']['cost']; 
       $var_date = $data['date']; 
       $status => $data['status']; 

       $date_var = date('Y-m-d H:i:s',$var_date); 

       $grand_total = $total + $cost; 
       $cost_ratio = ($cost/$grand_total) * 100; 
       $paid_ratio = ($total/$grand_total) * 100; 

       $range[] = $cost_ratio; 

       $test[] = array(
        'id' => $data['id'], 
        'ratio' => $cost_ratio, 
        'status' => $status 
       ); 

      } 

     echo "</table>"; 

     print_r($test); //this will return the last index from my array 
+0

確定由於我忘了數組聲明外移動。謝謝。 – Jerielle

+0

提到不......,我認爲它會爲你工作 –

1

這段代碼加載的標量值

$test = array(
       'id' => $data['id'], 
       'ratio' => $cost_ratio, 
       'status' => $status 
      ); 

而且應該是

$test[] = array(
       'id' => $data['id'], 
       'ratio' => $cost_ratio, 
       'status' => $status 
      );