在 CodeIgniter 中获取函数中的类别不起作用

Get categories in function in CodeIgniter not working

我在 CodeIgniter 框架中创建了一个函数,因此我可以在侧面菜单栏上看到我数据库中的所有产品类别,但是当我尝试回显所有类别时出现错误。我使用 db_helper.php 文件来完成。

一些数据库信息:

db category table name: categories
I have 2 rows in the categories table:

Row 1 name of categories table: id
Row 2 name of categories table: name

这是我的 db_helper.php 文件:

<?php

function get_categories_h(){
    $CI = get_instance();
    $categories = $CI->Product_model->get_categories();
    return $categories;
}

这是我的 Product_model.php 文件:

<?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 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
*/

 function get_categories(){
        $ci =& get_instance();
        $ci->db->select('*');
        $ci->db->from('categories');
        $ci->db->get()->result_array();
        return $q;
    }


}

?>

    public function get_categories(){
        $this->db->select('*'); 
    $this->db->from('categories'); 
    $query = $this->db->get(); 
    $result = $query->result_array();
    }

这是视图文件中的边栏,我试图在其中回显我的数据库的所有类别 (allecadeaus.php):

<div class="container-fluid">
<div class="row">
    <div class="col-lg-3">
<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 $category->name; ?> </a> </li>
            <?php endforeach; ?>
        </ul>
            </div>
            </div>

当我尝试加载视图文件时,我在边栏上看到了这个错误,而不是类别:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/allecadeaus.php

Line Number: 25

Backtrace:

File: /home/ubuntu/workspace/application/views/allecadeaus.php
Line: 25
Function: _error_handler

File: /home/ubuntu/workspace/application/controllers/AlleCadeausController.php
Line: 12
Function: view

File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once

当我打印 get categories 的输出时:

A PHP Error was encountered



Severity: Notice


Message:  Undefined variable: q


Filename: models/Product_model.php


Line Number: 42





Backtrace:










            File: /home/ubuntu/workspace/application/models/Product_model.php

            Line: 42

            Function: _error_handler            








            File: /home/ubuntu/workspace/application/helpers/db_helper.php

            Line: 6

            Function: get_categories            








            File: /home/ubuntu/workspace/application/views/allecadeaus.php

            Line: 38

            Function: get_categories_h          












            File: /home/ubuntu/workspace/application/controllers/AlleCadeausController.php

            Line: 12

            Function: view          












            File: /home/ubuntu/workspace/index.php

            Line: 315

            Function: require_once  

您可以按照以下解决方案更改代码。

请在autoload.php文件中添加助手:

File Path : application/config/autoload.php

<?php 
/*
  | -------------------------------------------------------------------
  |  Auto-load Helper Files
  | -------------------------------------------------------------------
  | Prototype:
  |
  |   $autoload['helper'] = array('url', 'file');
 */

$autoload['helper'] = array('db_helper');

?>

请更改您的 db_helper.php 文件:

File Path : application/helpers/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;
    } } ?>

您可以检查您的模态文件:

File Path : application/modal/Product_model.php

<?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 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_array();
        return $result;
    }
}
?>

You can change your view file.

<div class="container-fluid">
        <div class="row">
            <div class="col-lg-3">
                <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 $category['name']; ?>
                                </a> 
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>
        </div>
    </div>

希望对您有所帮助。谢谢!

您可以在帮助程序 itsef 中的函数中编写代码,并将结果 return 写入视图文件,而不是从帮助程序调用模型。另外你需要在cofig目录下的autoupload.php文件中添加helper。

 function get_categories(){
        $ci =& get_instance();
        $ci->db->select('*');
        $ci->db->from('categories');
        $q= $ci->db->get()->result();
        return $q;
    }