2011-05-04 19 views
2

行,所以我有這個疑問如何以適當的格式獲取這些數據?

select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id from system_step ss 
    join system as s on s.system_id=ss.system_id 
    join category_description as cd on cd.category_id=ss.sub_category_id 
    join system_step_product as ssp on ss.system_step_id=ssp.system_step_id 
    join product as p on p.product_id=ssp.product_id where s.system_id = 41 
    order by ss.step_number, ssp.class_id; 

which yields this result 

7 1 Screens   808 115.0000 1 
7 1 Screens   809 381.9000 2 
7 1 Screens   810 441.9000 3 
8 2 Printers  811 112.3200 1 
8 2 Printers  812 201.0400 2 
8 2 Printers  813 202.8700 3 
9 3 Cash Drawers 814 135.7000 1 
9 3 Cash Drawers 815 86.5400 2 
9 3 Cash Drawers 816 135.7000 3 

有沒有辦法把它變成三個元素的PHP數組這樣

Array 
(
    [0] => Array 
     (
      [name] => "Screens" 
      [standard_product] => Array ([id] => 808, [price] => '115.0000') 
      [business_product] => Array ([id] => 809, [price] => '381.9000') 
      [premium_product] => Array ([id] => 810, [price] => '441.9000') 
     )      

    [1] => Array    
     (      
      [name] => "Printers" 
      [standard_product] => Array ([id] => 811, [price] => '112.3200') 
      [business_product] => Array ([id] => 812, [price] => '201.0400') 
      [premium_product] => Array ([id] => 813, [price] => '202.8700') 
     ) 
    [2] => Array    
     (      
      [name] => "Cash Drawers" 
      [standard_product] => Array ([id] => 814, [price] => '135.7000') 
      [business_product] => Array ([id] => 815, [price] => '86.5400') 
      [premium_product] => Array ([id] => 816, [price] => '135.7000') 
     ) 
) 


$sql = "select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id, pd.name as product_name, pd.description from system_step ss join system as s on s.system_id=ss.system_id join category_description as cd on cd.category_id=ss.sub_category_id join system_step_product as ssp on ss.system_step_id=ssp.system_step_id join product as p on p.product_id=ssp.product_id join product_description as pd on pd.product_id=p.product_id where s.system_id = {$system['system_id']} order by ss.step_number, ssp.class_id;"; 
$result = mysql_query($sql); 
$steps = array(); 
while($row_r = mysql_fetch_assoc($result)){ 
    $steps[] = $row_r; 
} 

這樣的步驟是在全陣列9元

正如你所看到的唯一值得注意的是class_id 1是standard_product class_id 2是business_product和class_id 3是premium_product

+0

它可以用foreach()循環完成 – 2011-05-04 21:00:08

+1

我假設你通過類似'mysql_fetch_assoc()'的方式獲得查詢結果嗎?如果您能向我們展示您迄今爲止的代碼,那麼提供特定答案會更容易。 – 2011-05-04 21:02:10

+0

確定我會更新我的問題 – Trace 2011-05-04 21:34:05

回答

1

您可以在獲取SQL結果或拆分爲幾個SQL命令時獲取填充多維數組,因此可以構建數組(第1次獲取名稱,比使用每個名稱的值的產品)。

我不知道你更喜歡哪一個,但是因爲你有一個大的連接分裂,SQL在你的代碼的可讀性方面可能不是最差的。性能影響可能不同。

1

依我看這將做到這

<?php 

    $finalArray = array(); 
    $nameArray = array('Screens', 'Printers', 'Cash Drawers'); 

    foreach ($nameArray as $name) { 

     while ($row = mysql_fetch_assoc($result)) { 

      if ($row['name'] == $name) { 

       if ($row['class_id'] == 1) { 

        $standard = array('id' => $row['system_step_product_id'], 'price' => $row['cost']); 
       } 
       else if ($row['class_id'] == 2) { 

        $business = array('id' => $row['system_step_product_id'], 'price' => $row['cost']); 
       } 
       else if ($row['class_id'] == 3) { 

        $premium = array('id' => $row['system_step_product_id'], 'price' => $row['cost']); 
       } 
      } 
     } 

     array_push($finalArray, array('name' => $name, 'standard_product' => $standard, 'business_product' => $business, 'premium_product' => $premium,)); 
    } 

?> 

希望我得到了所有的列名權。

這是我測試過它的,我得到了正確的輸出http://codepad.org/yjU3gxbB

你可以改變什麼是創造的名稱數組動態

while ($row = mysql_fetch_assoc($result)) { 

     array_push($nameArray, $row['name']); 
    } 

    $nameArray = array_unique($nameArray); 

但唯一的一點,PHP不喜歡它,當你在相同的查詢上執行兩次mysql_fetch_assoc。你可能想要做另一個查詢,你只選擇cd.name

相關問題