我正在使用CodeIgniter创建一个网站,我正在尝试制作一个包含所有类别的菜单栏,当您点击某个类别时,它必须显示该类别中的所有产品,但它不是真的有效
类别显示在类别侧面菜单栏上,但是当我点击某个类别链接时,它会转到右侧视图页面而不是产品信息,我收到此错误:
A PHP Error was encountered
Severity: Error
Message: Cannot use object of type stdClass as array
Filename: views/category.php
Line Number: 41
Backtrace:
category.php中的第41行是这一行:
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
所以我猜它无法回应产品详细信息? 这是我的Allecadeaus控制器,用于回应产品:
<?php
class AlleCadeausController extends CI_Controller {
public function index()
{
$this->load->model('Product_model');
$this->load->model('allecadeaus_model');
$data['products'] = $this->allecadeaus_model->get_products();
$this->load->view('allecadeaus', $data);
}
}
我用来回应产品的Allecadeaus_model模型文件:
<?php
class Allecadeaus_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_products()
{
$query = $this->db->query('SELECT * FROM products');
$result = $query->result_array();
return $result;
}
}
category.php视图文件:
<?php include_once ('templates/header.php'); ?>
<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 bg-warning" style="font-size:25px">
<center>Alle cadeaus</center>
</div>
</div>
</div>
<hr />
<br>
<!-- Cadeau categorie side menu -->
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<a href="<?php echo base_url().'Product/category/' . $category->id; ?>"> <?php echo $category->name; ?></a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<!-- Laat cadeau zien op alle cadeaus pagina -->
<div class="col-md-8">
<?php foreach($products as $product) : ?>
<div class="col-md-2">
<div id="product">
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>">
</a>
<div class="product_naam"><?php echo $product['product_naam']; ?></div>
<div class="ophaal_plaats">
<?php echo $product['ophaal_plaats']; ?>
</div>
<div class="aangeboden_door">
<p>Aangeboden door: Peter</p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
我的整个allecadeaus.php视图文件:
<?php include_once ('templates/header.php'); ?>
<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 bg-warning" style="font-size:25px">
<center>Alle cadeaus</center>
</div>
</div>
</div>
<hr />
<br>
<!-- Cadeau categorie side menu -->
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<a href="<?php echo base_url('Product/category/'.$category['id']);?>"> <?php echo $category['name']; ?></a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<!-- Laat cadeau zien op alle cadeaus pagina -->
<div class="col-md-8">
<?php foreach($products as $product) : ?>
<div class="col-md-2">
<div id="product">
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>">
</a>
<div class="product_naam"><?php echo $product['product_naam']; ?></div>
<div class="ophaal_plaats">
<?php echo $product['ophaal_plaats']; ?>
</div>
<div class="aangeboden_door">
<p>Aangeboden door: Peter</p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
我的Product.php控制器文件:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
var $data = array();
public function index()
{
//Laad kado uploaden view
$this->load->view('product_form', $this->data);
}
public function details($product_id)
{
//load the Product_model
$this->load->model('Product_model');
//call function getdata in de Product_model
$data['userdetail_list'] = $this->Product_model->getdata();
//get product details
$data['product'] = $this->Product_model->get_product_details($product_id);
//laad view
$data['main_content'] = 'details';
$this->load->view('details',$data);
}
//categorie product function
public function category($id)
{
$data['title'] = 'Category';
$data['page'] = 'Product/category';
$data['category'] = $this->Category_model->findByCategory($id);
$data['products'] = $this->Product_model->findByCategory($id);
$this->Category_model->findByCategory($id);
$this->load->view('category', $data);
}
public function __construct()
{
parent::__construct();
$this->load->model('Product_model');
$this->load->helper(array('form', 'url'));
}
我的Product_model文件:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product_model extends CI_model {
public function saveProduct($data) {
$this->db->insert('products', $data);
$product_id = $this->db->insert_id();
return $product_id;
}
public function getdata()
{
$this->db->select('users.user_id,users.email,users.voornaam,products.product_id,products.category_id,products.product_naam');
$this->db->from('users');
$this->db->join('products','products.user_id = users.user_id');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
public function get_product_details($product_id) {
$arrReturn = array();
$this->db->select('*');
$this->db->from('products');
$this->db->where('product_id', $product_id);
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result)) {
$arrReturn = $result[0];
}
return $arrReturn;
}
/*
Get categories
*/
public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$query = $this->db->get();
$result = $query->result();
return $result;
}
public function findAll(){
return $this->db->get('products')->result();
}
public function findByCategory($id){
$this->db->where('id', $id);
return $this->db->get('categories')->result();
}
}
?>
我的Category_model文件:
<?php
class Category_model extends CI_Model {
public function findAll(){
$this->db->get('categories')->result();
}
public function findByCategory($id)
{
$this->db->where('id', $id);
$this->db->get('categories')->result();
return $this->db->get('categories')->row();
}
}
?>
我的db_helper.php
<?php if (!function_exists('get_categories_h')) {
function get_categories_h(){
$CI = get_instance();
$categories = $CI->Product_model->get_categories();
return $categories;
} } ?>
一些数据库信息:
Table 1: 5 columns: products
-product_id
-product_naam
-product_beschrijving
-user_id
-category_id
table 2: categories: 2 columns:
1.id
2.name
答案 0 :(得分:0)
您可以针对您的问题尝试此解决方案:
整个allecadeaus.php查看文件:
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<a href="<?php echo base_url().'Product/category/' . $category->id; ?>"> <?php echo $category->name; ?></a>
</li>
<?php endforeach; ?>
</ul>
答案 1 :(得分:0)
您的数据库是否已经提交了category_id,名称是否正确?你能从你的类别和产品表中显示屏幕吗?