我有一个PHP网站,我正在建设,我似乎有一些渲染问题。 我有我的网站分成多个部分,如Header.php,sidebar.php和footer.php。我的所有类别在侧边栏中都呈现没有问题,但是当我尝试呈现所选类别的产品并且该页面未呈现完整页面时。PHP页面没有正确渲染
我有一个简单的数据库与类别和产品。 下面是我的connect_db.php,categories_db.php和products_db.php文件的代码,它处理所有数据库的东西。
注意:如果我只是显示所有产品,我可以让我的product_list.php呈现,但我需要在侧边栏中呈现所选类别的产品。
下面是我的index.php或主视图
<?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>
现在侧边栏正确呈现的类别,但是当我点击一个类别也不会呈现product_list.php,符合市场预期。
我的边栏正确加载所有类别,代码如下。
<?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>
现在为product_list.php文件将不会呈现。
<?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 -->
我不确定是否因为$ category_id没有设置或什么,请别人帮我解决这个问题。
ERRORS:
说明:未定义变量:在CATEGORY_ID /var/www/WidgetCorp/views/product_list.php第4行
说明:未定义变量:CATEGORY_ID在/ var/WWW/WidgetCorp /上行视图/ product_list.php 7
致命错误:调用未定义方法PDO ::取()在/var/www/WidgetCorp/models/categories_db.php线路22上
puh,那是很多代码... –
Wayyyy代码太多了。 “你没有渲染”是什么意思?你有什么错误吗?它只是不显示你想要的? – Mat
对不起...我想要它完成....任何想法? –