【发布时间】:2012-11-23 10:42:35
【问题描述】:
我对 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 连接。我将如何在正确的类别中打印正确的产品?
【问题讨论】:
标签: php mysql loops join parent-child