I have a PHP site I’m building and I seem to be having some rendering issues.
I have my site broke up into multiple parts, such as Header.php, sidebar.php and footer.php. All my categories render in the sidebar no problem, but when I try to render the products for the selected category and the page doesn’t render the complete page.
I have a simple database with Categories and Products.
Below is the code for my connect_db.php, categories_db.php and products_db.php files, which handle all the database stuff.
NOTE: That I can get my product_list.php to render if I just display all products, but I need to render the products for the selected category in the sidebar.
Below is my index.php or main view
<?php
error_reporting(-1);
ini_set('display_errors', 1);
require('models/connect_db.php');
require('models/categories_db.php');
require('models/products_db.php');
$categories = get_categories();
if (!isset($category_id)) {
$category_id = 1;
}
else {
$category_id = $_GET['category_id'];
}
// get all products by category
$products = get_products_by_category($category_id);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<?php include('views/layout/head.php');?>
</head>
<body>
<!-- Wrapper -->
<div class="wrapper">
<!-- Header -->
<?php include('views/layout/header.php');?>
<!-- Main -->
<div id="main">
<div class="cl"> </div>
<!-- Content -->
<div id="content">
<?php include('views/product_list.php');?>
</div>
<!-- End Content -->
<!-- Sidebar -->
<?php include('views/layout/sidebar.php');?>
</div>
<!-- End Main -->
<!-- Footer -->
<?php include('views/layout/footer.php');?>
</div>
<!-- End Wrapper -->
</body>
</html>
Now the sidebar renders the categories correctly, but when I click on a category it won’t render the product_list.php, as expected.
My sidebar loads all the categories properly and the code is below.
<?php
$categories;
$categories = get_categories();
$category_id = $_GET['category_id'];
if (!isset($category_id)) {
$category_id = 1;
}
?>
<div id="sidebar">
<!-- Categories -->
<div class="box categories">
<h2>Categories </h2>
<div class="box-content">
<ul>
<!-- get category menu items -->
<?php foreach($categories as $category) : ?>
<li><a href="?category_id=<?php echo $category['category_id'];?>">
<?php echo $category['name'];?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<!-- End Categories -->
</div>
<!-- End Sidebar -->
<div class="cl"> </div>
Now for the product_list.php file that will not render.
<?php
// get all products by category
$products = get_products_by_category($category_id);
// get category name
$category_name = get_category_name($category_id);
?>
<!-- Products -->
<div class="products">
<div class="cl"> </div>
<ul>
<?php foreach($products as $product) : ?>
<li>
<a href="#"><img src="css/images/big1.jpg" alt="" /></a>
<div class="product-info">
<h3><?php echo $product['name'];?></h3>
<div class="product-desc">
<h4><?php echo $category_name;?></h4>
<p><?php echo $product['description'];?></p>
<strong class="price"><?php echo $product['price'];?></strong>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
<div class="cl"> </div>
</div>
<!-- End Products -->
I’m not sure if it’s because the $category_id is not getting set or what, please someone help me get past this problem.
ERRORS:
Notice: Undefined variable: category_id in /var/www/WidgetCorp/views/product_list.php on line 4
Notice: Undefined variable: category_id in /var/www/WidgetCorp/views/product_list.php on line 7
Fatal error: Call to undefined method PDO::fetch() in /var/www/WidgetCorp/models/categories_db.php on line 22
I believe the problem is you’re including the sidebar.php file AFTER the product_list.php file. Product_list.php references $category_id, but $category_id doesn’t seem to be included until the sidebar.php file. So you’re trying to pass an undefined variable to your various functions, and it’s spawning an error.
Part of this issue is that you’ve marshaled your resources incorrectly. Try putting all the code at the top of your template files (the stuff that grabs the data you need), so as to separate it from your presentation code.
In reference to your comment, the ordering of the code needs to flip to something like this: