通常有一个类别菜单,每个菜单链接到一个类别页面,其中显示该类别下的所有项目。如何在每个类别下显示多个类别和产品?
现在我需要在同一页面中显示PHP/MySQL下的所有类别和项目。
所以它会是这样的。
Category 1
description of category 1
item 1 (some details, such as name, thumbnail etc for each item)
item 2
..
Category 2
description of category 2
item 5
item 6
..
Category 3
description of category 3
item 8
item 9
...
...
我在我的数据库中有类别和产品表。
看起来好像我必须通过类别循环,并在每个类别下循环以获取所有项目。
但我不知道如何继续。
CREATE TABLE IF NOT EXISTS `omc_product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`thumbnail` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`product_order` int(11) DEFAULT NULL,
`class` varchar(255) DEFAULT NULL,
`grouping` varchar(16) DEFAULT NULL,
`status` enum('active','inactive') NOT NULL,
`category_id` int(11) NOT NULL,
`featured` enum('none','front','webshop') NOT NULL,
`other_feature` enum('none','most sold','new product') NOT NULL,
`price` float(7,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
CREATE TABLE IF NOT EXISTS `omc_category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`status` enum('active','inactive') NOT NULL,
`parentid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
我会感谢您的帮助。
在此先感谢。
如何“检查自上一行以来category_id是否已更改”?提前致谢。 – shin 2010-05-16 21:07:54
在循环开始处设置'$ previouscatid = NULL',然后在循环中检查$ product ['category_id']!== $ previouscatid',并设置$ $ previouscatid = $ product ['category_id ']'就在循环结束之前。 – bobince 2010-05-16 21:12:26