Reputation: 21
I am creating a very simple app, but as I am really rusty with code, and new to OOP I don't seem to understand the logic for creating a simple link that will display just the products relevant to that category. THe App is not ecommerce it just pulls data from the database, displays it, when the user clicks on a link, and writes data into in the products and category tables. The interface I am trying to do is a way to display the linking products when the user clicks on the respective categories. THe only problem is the categories are multilevel and there is where I get completely stuck, have no idea how to pass the variable, or pull the appropriate select statement. My problem i I have been browsing and trying to look ate examples that show how to display product listings and none are for multilevel subcategories. I need to add a url where the link is the category associated to that product. I have two simple tables, and a dynamic menu.
--
-- Table structure for table `products1`
--
CREATE TABLE IF NOT EXISTS `products1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`description` text NOT NULL,
`price` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`created` datetime NOT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;
--
-- Table structure for table `menu`
--
CREATE TABLE IF NOT EXISTS `menu` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 NOT NULL,
`parent_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=136 ;
The table menu is just a way to relate the parent categories to their siblings. As I have over 103 categories, all related to each other by their parent id, I have no idea, how to write the logic to display the active category in the menu. My logic is flawed. Can't figure out what kind of comparison I need to do, hat code needs to be done to pass the right category id, depending on where the active Li tag is.
THis is the menu generated by that table.
<?php
$items = $rows;
$id = '';
echo '<ul class="dl-menu">';
foreach($items as $item){
if($item['parent_id'] == 0) {
echo '<li><a href="?category=<?php echo $category->id?>">'. $item['name'] . '</a>';
$id = $item['id'];
sub($items, $id);
echo '</li>';
}
}
echo "</ul>";
function sub($items, $id){
echo '<ul class="dl-submenu">';
foreach($items as $item){
if($item['parent_id'] == $id) {
echo '<li><a href="?category=<?php echo $category->id?>">'. $item['name'] . '</a>';
// $id = $item['id'];
sub($items, $item['id']);
echo '</li>';
}
}
echo '</ul>';
}
?>
My problem is with the line of code that echoes the category id. I have no idea what script I need to output that, I mean the logic I need to compare to get that result.
Upvotes: 0
Views: 1274
Reputation: 4265
Your current lines which output a category id look like the following:
echo '<li><a href="?category=<?php echo $category->id?>">' . $item['name'] . '</a>';
As these are eachos from within PHP, you don't need the tags inside the echo, so changing those lines to :
echo '<li><a href="?category=' . $category->id . '">'. $item['name'] . '</a>';
should fix your problem, if I am understanding the question correctly.
Upvotes: 0