【发布时间】:2011-07-17 16:34:53
【问题描述】:
我正在构建一个 PHP 站点,但我似乎遇到了一些渲染问题。 我将我的网站分成多个部分,例如 Header.php、sidebar.php 和 footer.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 没有设置还是什么,请有人帮我解决这个问题。
错误:
注意:未定义变量:第 4 行 /var/www/WidgetCorp/views/product_list.php 中的 category_id
注意:未定义变量:第 7 行 /var/www/WidgetCorp/views/product_list.php 中的 category_id
致命错误:在第 22 行的 /var/www/WidgetCorp/models/categories_db.php 中调用未定义的方法 PDO::fetch()
【问题讨论】:
-
那里的代码太多了。
does not render是什么意思?你有什么错误吗?只是没有按您的意愿显示吗? -
对不起...我希望它是完整的...有什么想法吗?
-
另外,如果这是您正在开发的网站,您能否链接到该网站,以便我们准确查看哪里出了问题?
-
@Carl:如果没有呈现整个页面,那通常是由某种错误引起的。通过报告所有错误并显示它们更容易找到它们。将此添加到代码的最开头:
error_reporting(-1); ini_set('display_errors', 1);- 如果您收到通知/警告/错误,请将它们添加到您的问题中。 -
它没有完成页面渲染,并切断了 products_list.php 应该渲染的 HTML。