我在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
当我打印获取类别的输出时:
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
答案 0 :(得分:2)
您可以将代码更改为以下解决方案。
请在autoload.php文件中添加帮助:
文件路径:application / config / autoload.php
spyOn(o, "foo")
请更改db_helper.php文件:
文件路径:application / helpers / db_helper.php
<?php
/*
| -------------------------------------------------------------------
| Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('db_helper');
?>
您可以查看模态文件:
文件路径:application / modal / Product_model.php
<?php if (!function_exists('get_categories_h')) {
function get_categories_h(){
$CI = get_instance();
$categories = $CI->Product_model->get_categories();
return $categories;
} } ?>
您可以更改视图文件。
<?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;
}
}
?>
我希望这会对你有所帮助。谢谢!
答案 1 :(得分:0)
您可以在帮助程序itsef中的函数中编写代码,而不是从帮助程序调用模型,并将结果返回到视图文件。另外,您需要在cofig目录的autoupload.php文件中添加帮助程序。
function get_categories(){
$ci =& get_instance();
$ci->db->select('*');
$ci->db->from('categories');
$q= $ci->db->get()->result();
return $q;
}