【问题标题】:Product category function not working in CodeIgniter产品类别功能在 CodeIgniter 中不起作用
【发布时间】:2017-10-12 08:29:57
【问题描述】:

我正在 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

【问题讨论】:

  • $category-&gt;id替换$category['id']name也一样
  • 嘿,我把它改成了这样: 名称; ?> 我现在收到此错误:遇到 PHP 错误 严重性:通知消息:未定义变量:行文件名:views/allecadeaus.php 行号:26 回溯:
  • 不,您只需对 idname 使用对象表示法。您可以在上面的链接或下面的答案中查看
  • 删除不必要的代码。您的问题是产品详细信息还是产品类别?

标签: php codeigniter codeigniter-3


【解决方案1】:

您可以尝试以下解决方案来解决您的问题:

整个 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>

【讨论】:

  • 您好,allecadeaus.php 视图文件不再有错误,但是当我单击某个类别链接时,它显示此错误:发生数据库错误错误编号:1054 未知列“category_id”在 'where 子句' SELECT * FROM categories WHERE category_id = '10' 文件名:models/Product_model.php 行号:55
  • 请发布您的 get_categories_h() 函数文件或 Helper 文件,并请更改您的查询SELECT * FROM categories WHERE id = '10'
  • 好吧,我编辑了我的问题并显示了我的 db_helper.php
  • 检查您的类别表。我认为 ID 将在那里而不是 category_id。然后像这样查询 SELECT * FROM categories WHERE id = '10'
  • @mayankverma 在上面的问题中提到了表结构table 2: categories: 2 columns: 1.id 2.name,所以我可以进行更改。
【解决方案2】:

您的数据库是否已经有归档的 category_id,名称是否正确?你能从你的类别和产品表中显示屏幕吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多