Problem to display Category Name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shafiq2626
Forum Commoner
Posts: 88
Joined: Wed Mar 04, 2009 1:54 am
Location: Lahore
Contact:

Problem to display Category Name

Post 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
Attachments
productlist.JPG
productlist.JPG (130.37 KiB) Viewed 221 times
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem to display Category Name

Post 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
											}
											
												?> 
(#10850)
shafiq2626
Forum Commoner
Posts: 88
Joined: Wed Mar 04, 2009 1:54 am
Location: Lahore
Contact:

Re: Problem to display Category Name

Post by shafiq2626 »

Thanks Christopher problem has been solved.
Thanks a lot for response.
Take Care
Post Reply