Page 1 of 1

Problem to display Category Name

Posted: Mon Dec 13, 2010 2:02 am
by shafiq2626
Hi! to all
I am displaying product list related to different categories.
Product list displaying correctly . But I want to display product list Heading should be Category then all product of this category then Heading of Next Category then all product of this category so on.
But i am facing this problem code is below ant also attachment of current display. where category heading displaying with each row.

Code: Select all

$sql = $this->MMdbobj->query("SELECT pro.*,cat.catname FROM tbl_products pro,tbl_category cat where pro.status='1' and cat.cat_id=pro.cat_id order by cat.cat_id ASC limit $eu, $limit");

$i=0;
										foreach($this->params['ProductList'] as $Products)
										{
											$i++;
											$start++;
											$cat=$Products['catname'];
									?>

                                     
                                     <tr><td colspan="4" align="center"><?php echo $Products['catname'];?></td></tr>
                                   
                           			
										<tr bgcolor="<?php if($i%2){?>#efefef<?php }?>">
                                        	<td class="para" style="padding-left:5px; padding-right:5px; border:2px #000 thick;"><?php echo $start;?></td>
											<td class="para" style="padding-left:5px; padding-right:5px; border:2px #000 thick; "><?php echo $Products['pname'];?></td>
											<td class="para" style="padding-left:5px;padding-right:5px; border:2px #000 thick; "><?php echo $Products['detail'];?></td>
											<td height="20" class="para" style="padding-left:5px;padding-right:5px; border:2px #000 thick; "><?php echo $Products['company'];?></td>
                                            
                                            <td height="20" class="para" style="padding-left:5px;padding-right:5px; border:2px #000 thick; ">
                                            <?php if($Products['hot']==1){?>
                                            <input type="button" value="view" style="cursor:pointer;" onClick="window.location.href='index.php?module=productdetail&pro_id=<?php echo $Products['pro_id'];?>'" class="readmore">
                                            <?php }?>
                                            </td>
                                      </tr>
										
								
								<?php
											}
											
												?> 
please help with thanks

Re: Problem to display Category Name

Posted: Mon Dec 13, 2010 4:42 pm
by Christopher
This show the basic logic of category headings. You need to sort first by category and then item title. The check if the category has changed each loop and only show the category when it changes. Check the logic so see why it show the first category and when it changes the "current" category.

Code: Select all

$sql = $this->MMdbobj->query("SELECT pro.*,cat.catname FROM tbl_products pro,tbl_category cat WHERE pro.status='1' AND cat.cat_id=pro.cat_id 
ORDER BY cat.catname,pro.pname ASC 
LIMIT $eu, $limit");

$cat='';
$i=0;
foreach($this->params['ProductList'] as $Products)
{
	$i++;
	$start++;

	// check if the categroy name as changed (think includes first time in loop)
	if ($cat != $Products['catname'])
	{
?>
               <tr><td colspan="4" align="center"><?php echo $Products['catname'];?></td></tr>
<?php
		// save the new current category value
		$cat = $Products['catname'];
	}
?>                           			
										<tr bgcolor="<?php if($i%2){?>#efefef<?php }?>">
                                        	<td class="para" style="padding-left:5px; padding-right:5px; border:2px #000 thick;"><?php echo $start;?></td>
											<td class="para" style="padding-left:5px; padding-right:5px; border:2px #000 thick; "><?php echo $Products['pname'];?></td>
											<td class="para" style="padding-left:5px;padding-right:5px; border:2px #000 thick; "><?php echo $Products['detail'];?></td>
											<td height="20" class="para" style="padding-left:5px;padding-right:5px; border:2px #000 thick; "><?php echo $Products['company'];?></td>
                                            
                                            <td height="20" class="para" style="padding-left:5px;padding-right:5px; border:2px #000 thick; ">
                                            <?php if($Products['hot']==1){?>
                                            <input type="button" value="view" style="cursor:pointer;" onClick="window.location.href='index.php?module=productdetail&pro_id=<?php echo $Products['pro_id'];?>'" class="readmore">
                                            <?php }?>
                                            </td>
                                      </tr>
										
								
								<?php
											}
											
												?> 

Re: Problem to display Category Name

Posted: Mon Dec 13, 2010 11:23 pm
by shafiq2626
Thanks Christopher problem has been solved.
Thanks a lot for response.
Take Care