简体   繁体   中英

Displaying products available in each parent category

I'm creating a report page to show a daily total of produce sold each day, using osCommerce. Here is the markup I have so far to display the category and subcategories:

<table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr class="dataTableHeadingRow">
<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_TOTAL_WEIGHT; ?>&nbsp;</td>
</tr>


<?php


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();


?>

</table>

What I want to show after the subcategory, is the product in each one, this means using the TABLE_PRODUCTS_TO_CATEGORIES table. I have the following code which is used to count the products $query = 'SELECT COUNT(*) FROM '.TABLE_PRODUCTS_TO_CATEGORIES.' WHERE $query = 'SELECT COUNT(*) FROM '.TABLE_PRODUCTS_TO_CATEGORIES.' WHERE categories_id` = "'.$cat['categories_id'].'"';.

Where Would I put this into my markup, and how would I show the products?

Why not JOIN the table TABLE_PRODUCTS_TO_CATEGORIES in the same query you are using like so:

select 
  cd.categories_name,
  c.categories_id, 
  c.parent_id, 
  c.sort_order,
  p.* -- You can here select the products details you want to select.
from TABLE_CATEGORIES  c
INNER JOIN TABLE_CATEGORIES_DESCRIPTION cd ON c.categories_id = cd.categories_id 
INNER JOIN TABLE_PRODUCTS_TO_CATEGORIES p  ON p.categories_id = c.categories_id 
WHERE c.parent_id = '.$category_parent_id

HERE IS THE IDEA TO GET PRODUCTS, WHERE YOU ARE CHECKING CATEGORY YOU ALSO GETTING CATEGORIES_ID

SIMPLY MAKE ANOTHER QUERY AFTER IT AND CALL SAME PROCESS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM