按类别循环产品

时间:2012-02-27 11:39:13

标签: php mysql arrays loops

我设法让这个工作,但我有一个小问题,我无法解决,希望有人可以提供帮助。

如果父类别中有产品,我工作正常但是如果父级为空,即使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 个答案:

答案 0 :(得分: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,
    );
}