2012-02-27 2 views
-3

我已經設法使這個工作,但我有一個輕微的錯誤,我不能修復,並希望有人可以幫助。按類別循環產品

如果在父類別中有產品,我工作正常,但是如果父類爲空即使SQL返回正確的結果,PHP循環將根據有多少產品重複數據。即。 4個產品= 4組結果。但正如我所說,如果在父類別中有一個記錄,它可以完美地工作。

<?php $_GET["category_id"]=$category_id; ?> 

    <?PHP 
    $sql = " 
     SELECT DISTINCT JVPCX.category_id, 
      JVP.product_id, 
      JVP.product_error, 
      JVP.product_exchange, 
      JVP.product_sku, 
      JVP.product_name, 
      JVP.product_url, 
      JVP.product_in_stock, 
      jos_vm_category.category_name 
    FROM jos_vm_product_category_xref JVPCX 
      INNER JOIN jos_vm_product JVP ON (JVPCX.product_id = JVP.product_id) 
      INNER JOIN jos_vm_category ON (JVPCX.category_id = 
      jos_vm_category.category_id) 
    WHERE JVPCX.category_id IN (SELECT jos_vm_category_xref.category_child_id FROM 
     jos_vm_category_xref WHERE jos_vm_category_xref.category_parent_id =".$category_id.") OR (JVPCX.category_id =".$category_id.")"; 

    $res = mysql_query($sql); 
    $list = array(); 
    while ($r = mysql_fetch_object($res)) 
    { 
     if (! isset($list[ $r->category_name ])) 
     { 
      $list[ $r->category_name ] = array(); 

     } 

     $list[ $r->category_name ][ $r->product_id ] = array(
      'SKU'  => $r->product_sku, 
      'Name' => $r->product_name, 
      'Prod_id' => $r->product_id, 
      'Prod_error' => $r->product_error, 
      'Prod_exchange' => $r->product_exchange, 
      'Prod_url' => $r->product_url, 
      'Prod_in_stock' => $r->product_in_stock 
     ); 
    } 
    ?> 

    <table width="70%" border="0"> 
     <tr> 
     <td align="center" valign="middle"> Error: <img src="alert.gif" title="Error" alt="Error" /> 
     </td> 
     <td align="center" valign="middle"> Exchange: <img src="exchange.gif" title="Exchange" alt="Exchange" /> 
     </td> 
     <td align="center" valign="middle"> Information:<img src="info.gif" title="info" alt="info" /> 
     </td> 
     <td align="center" valign="middle"> Stock:<img src="stock.gif" title="Stock" alt="Stock" /> 
     </td> 
     <td align="center" valign="middle"> Buy:<img src="cart-icon.png" title="Stock" alt="Buy" /> 
     </td> 
     </tr> 
    </table> 

    <?PHP 

    echo("<BR/>"); 
    foreach ($list as $catID => $products) { ?> 
    <?php 
     print($catID); 
     echo("<BR/>"); 
     ?> 
    <table width="100%" border="1" class="product-head"> 
     <tr> 
     <th width="20%"> <?php echo("Part Number:"); ?> 
     </th> 
     <th width="45%"> <?php echo("Description:"); ?> 
     </th> 
     <th width="15%"> <?php echo("Information:"); ?> 
     </th> 
     </tr> 
    </table> 

    <table width="100%" border="1" class="product"> 
    <?php foreach ($products as $prodID => $data) { ?> 
     <tr> 
     <td width="20%"> 
     <?php print($data['SKU']); 
      echo("<BR/>");  
     ?> 
     </td> 


    <td width="45%"><?php 
     print($data['Name']); 
    echo("<BR/>");  ?></td> 

    <td width="15%" valign="middle"><?php 

       if ($data['Prod_error'] == "Y"){ 

      echo '<img src="alert.gif" title="Error" alt="Error" />'; 

      } 

       if ($data['Prod_exchange'] == "Y"){ 
       echo '<img src="exchange.gif" title="Exchange" alt="Exchange" />'; 


      } 

    if ($data['Prod_url'] > ""){ 
      echo '<a href="'. 
      $data['Prod_url'] 
      .'">'.'<img src="info.gif" title="info" alt="info" />'.'</a>'; 


      } 

    if ($data['Prod_in_stock'] > "0"){ 
      echo '<img src="stock.gif" title="Stock" alt="Stock" />'; 

      } 
    ?> 
    </td> 
     </tr> 
    </table> 
    <?php echo("<BR/>"); ?> 
    <?php } ?> 
+1

後你有代碼。 – spencercw 2012-02-27 11:40:51

+1

奇怪的是,沒有人建議你發佈你的代碼 - 所以請發佈你的代碼 – mkk 2012-02-27 11:53:18

+0

我一直在試圖從另一個帖子得到這個工作: – Paul 2012-02-27 11:55:32

回答

0

首先。刪除jos_vm_product_category_xref.product_id, jos_vm_product_category_xref.product_list

$sql = " 
    SELECT DISTINCT 
     JVPCX.`category_id`, 
     JVP.`product_id`, 
     JVP.`product_sku`, 
     JVP.`product_s_desc` 
    FROM 
     `jos_vm_product_category_xref` JVPCX 
    INNER JOIN `jos_vm_product` JVP 
     ON JVPCX.`product_id` = JVP.`product_id` 
"; 

,你也改變你的循環

$list = array(); 

while ($r = mysql_fetch_object($res)) 
{ 
    if (! isset($list[ $r->category_id ])) 
    { 
     $list[ $r->category_id ] = array(); 
    } 

    $list[ $r->category_id ][ $r->product_id ] = array(
     'name'  => $r->product_sku, 
     'whatever' => $r->product_s_desc, 
    ); 
}