Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6169803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:55:10+00:00 2026-05-23T22:55:10+00:00

I have a PHP site I’m building and I seem to be having some

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:55:11+00:00Added an answer on May 23, 2026 at 10:55 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code on my PHP powered site that creates a random hash
I have a simple php site that I built, but for some reason my
I have a php site which has multiple php scripts. I need to provide
I have a PHP/MySQL site I'm thinking about converting into a HTML5/JavaScript that could
now i have such structure on my server ./site.com/ ./site.com/mage ./site.com/mage/Mage.php ./site.com/mage/app ......... ./site.com/public
I have a PHP based site with dynamic pages , such as /page.php?ID=1. The
I have a PHP site that uses a fairly common authentication scheme. The entire
I have a PHP-driven site that includes an XML stock feed, which is remotely
In my live site I have php include() and require() that have full path
do you have any good suggestions how to backup remote php site and mysql,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.