dtkago3208 2012-12-05 15:01
浏览 15
已采纳

在其父级(类别)之后列出产品

I'm relatively new to php and I am using oscommerce to create a list of category's, subcategory's, and the products available in the subcategory's, in a report. I've managed to create the list of category's, with the child(subcategory) after it. I have hit a dead end in trying to list the products after the subcategory's. Here is a snip of the coding:

function category_list( $category_parent_id = 0 )
{
$sql  = 'select cd.categories_name,c.categories_id, c.parent_id, c.sort_order from ' . TABLE_CATEGORIES . ' c, ' . TABLE_CATEGORIES_DESCRIPTION . ' cd where c.categories_id = cd.categories_id AND c.parent_id='.$category_parent_id;
$res  = tep_db_query( $sql );
$cats = array();
 while ( $cat = tep_db_fetch_array( $res ) )
  {
    $cats[] = $cat;
  }
 if (count($cats) == 0)
    {
  return '';
  }
 $list_items = array();
foreach ( $cats as $cat )
 {
$list_items[] = '<tr class="dataTableRow"><td class="dataTableContent">'; 
if($category_parent_id != 0)$list_items[] = '&nbsp;&nbsp;&nbsp;';
if($category_parent_id == 0)$list_items[] = '<b>';
$list_items[] = $cat['categories_name'];


if($category_parent_id == 0)$list_items[] = '</b>';
$list_items[] = '</td><td class="dataTableContent">'; 
$list_items[] = category_list( $cat['categories_id'] );
$list_items[] = '</td></tr>'; 
}
$list_items[] = '';
return implode( '', $list_items );

}  
echo category_list();

For listing products, I have two tables that will need to be used, product_to_cat, and prod_descrip, joined by using the product_id field. For it to be listed in the correct parent, the product_to_cat and cat table are joined with the category_id. How would I go about printing the correct products in the correct category?

  • 写回答

1条回答 默认 最新

  • 普通网友 2012-12-05 15:53
    关注

    Assuming the tables look like in this SQL Fiddle, then this is just a simple join between all tables

    select c.parent_id, c.categories_id, cd.categories_name,
           p.product_id, pd.description
    from categories c
    join categories_description cd on cd.categories_id = c.categories_id
    left join product_to_cat p on p.category_id = c.categories_id
    left join prod_descrip pd on pd.product_id = p.product_id
    order by c.parent_id, c.categories_id, p.product_id
    

    I don't know how the sort_order fits in this picture.

    If you want the products for one category only, do the following select in your loop

    select p.product_id, pd.description
    from product_to_cat p
    left join prod_descrip pd on pd.product_id = p.product_id
    where p.category_id = $cat['categories_id']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源