PHP页面无法正确呈现

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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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:

<?php
/* sidebar.php */
$categories;
$categories = get_categories();

$category_id = $_GET['category_id'];
if (!isset($category_id)) {
    $category_id = 1;
}
/* product_list.php */
// get all products by category
$products = get_products_by_category($category_id);

// get category name
$category_name = get_category_name($category_id);

Okay! If I am understanding your structure correctly, index.php is the main script that is rendering all the components - the header, sidebar, the products and the footer. The sidebar.php and product_list.php only render specific content.

Now, when clicked on a category, the category_id is passed as a GET parameter to index.php. When the code includes "views/product_list.php", it searches for $category_id that doesn't exist yet (I assume that "register_globals" is set to off). Please try using $_GET['category_id'] in the first 2 statements in product_list.php, like:

// get all products by category
$products = get_products_by_category($_GET['category_id']);

// get category name
$category_name = get_category_name($_GET['category_id']);

I also noticed that in sidebar.php, you are correctly using $_GET['category_id'] to set $category_id. This means that $category_id will be available in all the following code. However, since "product_list.php" is called before "sidebar.php", $category_id isn't available in "product_list.php".

Additional Suggestion

For the scenario like above, it is best to use $_GET['category_id'] in all places where the value is needed. This avoids any conflicts or undefined-variable issues. Another alternative if it is required to read $_GET['category_id'] once like in your example, initialize $category_id in the main "index.php" rather than in any of the included files. Doing so will ensure that the variable is available throughout. For example, you might add these lines just after the first 3 requires:

$category_id = 1;
if (isset($_GET['category_id']) && !empty($_GET['category_id'])) {
    $category_id = $_GET['category_id'];
}

Hope the above explanation helps!