我对php比较陌生,我正在使用oscommerce在报告中创建类别,子类别以及子类别中可用产品的列表。我已经设法创建了类别列表,其后面是子(子类别)。在尝试列出子类别之后的产品时,我已经走到了尽头。以下是编码的一部分:
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[] = ' ';
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();
要列出产品,我需要使用两个表,product_to_cat
和prod_descrip
,使用product_id
字段加入。要使其在正确的父级中列出,product_to_cat
和cat
表将与category_id
连接。我如何打印正确类别的正确产品?
答案 0 :(得分:0)
假设这些表格在SQL Fiddle中, 那么这只是所有表之间的简单连接
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
我不知道sort_order如何适应这张图片。
如果您只想将产品用于一个类别,请在循环中执行以下选择
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']