2011-05-04 7 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 

가 있습니까이 쿼리이

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; 
} 

그래서 단계 등의 세 가지 요소의 PHP는 배열로를 설정하는 방법을 9 개 요소

와 전체 배열되어 있습니다

참고로 주목할만한 점은 class_id 1이 standard_product class_id 2가 business_product이고 class_id 3이 premium_product입니다.

+0

foreach() 루프를 사용하여 수행 할 수 있습니다 –

+1

mysql_fetch_assoc()과 같은 방법으로 쿼리 결과를 얻고 있다고 가정합니까? 지금까지 가지고있는 코드를 보여줄 수 있다면 특정 답변을 제공하는 것이 더 쉬울 것입니다. –

+0

내 질문이 업데이트됩니다. – Trace

답변

1

SQL 결과를 가져 오는 동안 다차원 배열을 채우거나 여러 SQL 명령으로 분할하여 배열을 빌드 할 수 있습니다 (모든 이름의 값을 가진 제품보다 이름을 먼저 가져옴).

어느 쪽이 좋을지 모르지만 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

관련 문제