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']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多