我在PHP
和MySQL
中退出了新功能,我正在尝试开发一个练习网站。但我面临一些麻烦。这个网站对我来说最有用,所以经过多次尝试我决定在这里问一下。
我的问题是我已经为页眉,页脚,侧栏创建了部分分隔文件,并在索引,产品或其他文件中包含这些文件。当我尝试从index.php获取数据库结果时,它工作正常但我还必须在sidebar.php中创建类别ID,这也应该从数据库中获取。
当我在index.php中获取数据时工作正常但在sidebar.php中当我尝试获取数据时它警告我未定义的变量当我在sidebar.php中包含数据库类时它警告我不能重新声明类数据库。有我的代码和文件夹结构。
注意:关于我的知识包括initialize.php index.php在非常乞讨和之后包括header.php所以它应该使用initialize.php但我已尝试使用include initialize也在sidebar.php但它生成错误“不能redeclare类数据库“。当我从sidebar.php中删除此初始化时,它会生成错误“未定义的变量连接”... 当我工作是单个文件,我的意思是我删除sidebar.php并直接在index.php内写入孔代码,而不是工作正常。
我喜欢的文件夹结构。
| Practice_site
|---| includes
|---|---| class
|---|---|---| database.class.php
|---|---|---| validation.class.php
|---|---|---| upload.class.php
|---|---| functions.php
|---|---| initialize.php
|---| public
|---|---| site_template
|---|---|---| header.php
|---|---|---| footer.php
|---|---|---| sidebar.php
|---|---| index.php
|---|---| about.php
|---|---| products.php
|---|---| product_detail.php
|---|---| cart.php
in Databse.class.php file
class Database
{
private $connection;
private $error;
private $stmt;
// Set options
private $options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
public function __construct($host,$dbname,$uname,$dbpassword="")
{
try{
$dns = "mysql:host=".$host.";dbname=".$dbname;
$this->connection = new pdo($dns,$uname,$dbpassword,$this->options);
}catch(PDOException $e){
$this->error = $e->getMessage();
}
(isset($this->error))? die("<h3>".$this->error."</h3>"):null;
}
public function query($sql)
{
return $this->connection->query($sql);
}
public function fetch()
{
return $this->connection->fetch();
}
public function rowCount()
{
return $this->connection->rowCount();
}
public function fetchAll()
{
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
}//end class
$connection = new Database('localhost','dbname','user','password');
in function.php file
in header.php file
<!DOCTYPE html>
<html>
<head>
<title>title or the page</title>
</head>
<body>
<?php include "sidebar.php"; ?>
in initialize.php file
<?php
defined('DS')? null: define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT')? null: define('SITE_ROOT', 'C:'.DS.'wamp'.DS.'www'.DS.'OnlineStore');
defined('LIB') ? null : define('LIB', SITE_ROOT.DS.'includes');
include LIB.DS."class".DS."Database.class.php";
include LIB.DS."functions.php";
?>
in sidebar.php file
try{
require "../includes/initialize.php"; // when remove this line php generate undefined variable connection else generate error cannot redeclare class databas line 9
$sql = "SELECT * FROM category ORDER BY position";
$categories = $connection->query($sql);
}catch(EXCEPTION $e){
$error = $e->getMessage();
}
<?php while ( $category = $categories->fetch() ): ?>
<li><a href="#"><?php echo $category['category']; ?></a></li>
<?php endwhile; ?>
in index.php file
<?php
try{
// require "../includes/initialize.php";
$sql = "SELECT * FROM products ORDER BY product_id DESC";
$products = $connection->query($sql);
$sql = "SELECT * FROM category ORDER BY position";
$categories = $connection->query($sql);
}catch(EXCEPTION $e){
$error = $e->getMessage();
}
include "header.php";
<?php while( $row = $products->fetch() ): ++$counter ?>
<div class="product_box <?php echo ( ($counter%3)===0 ) ? 'no_margin_right' : '' ?>">
<h3><?php echo htmlentities($row['product_name']) ?></h3>
<a href="productdetail.php"><img src="images/products_img/<?php echo $row['product_image']; ?>" alt="Shoes 3" /></a>
<p><?php echo htmlentities(substr($row['description'], 0,100)) ?></p>
<p class="product_price">$ 60</p>
<a href="shoppingcart.php" class="addtocart"></a>
<a href="productdetail.php" class="detail"></a>
</div>
<?php echo ( ($counter%3)===0 ) ? '<div class="cleaner"></div>' : '' ?>
<?php endwhile; ?>