我有一個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
對不起...我想要它完成....任何想法? –