我从数据库中获取数据。但是我收到错误。我需要在页面中显示产品名称和图像。但是我在页面中收到错误未定义变量'name'。请检查我的代码,提出宝贵的建议,谢谢。
我的控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class productdisplay_ctrl extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model("productdisplay_model");
}
public function productfetch(){
$this->load->model("productdisplay_model");
$result = $this->productdisplay_model->fetchData();
$data['name'] = $result->sub3_category_name;
$this->load->view('home', $data);
}
}
?>
我的模特
<?php
class productdisplay_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function fetchData() {
$this->db->where($where);
$this->db->order_by('rand()');
$this->db->select('*');
$this->db->from('sub3_category');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
?>
我的观点
<div class="container-main">
<div class="col-xs-12 col-sm-4 col-md-3">
<div class="prod-container">
<a href="<?php echo base_url(); ?>index.php/welcome/productlist">
<div class="prod_img">
<img src="<?php echo base_url();?>images/img-electronics.jpg" width="100%"/>
</div>
<div class="prod_desc">
<div class="prod-round-icon"></div>
<h4 class="prod_title"><?php echo $name; ?></h4>
<p class="prod_text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="view-more"> view more</div>
</a>
</div>
</div>
答案 0 :(得分:4)
控制器
defined('BASEPATH') OR exit('No direct script access allowed');
class productdisplay_ctrl extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model("productdisplay_model");
}
public function productfetch(){
$this->load->model("productdisplay_model");
$result = $this->productdisplay_model->fetchData();
$data['name'] = $result->sub3_category_name;
$this->load->view('home', $data);
}
}
?>
型号:
<?php
class productdisplay_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function fetchData() {
$this->db->select('*');
$this->db->where($where);
$this->db->order_by('rand()');
$this->db->from('sub3_category');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
}
return false;
}
}
?>
视图:
<div class="container-main">
<div class="col-xs-12 col-sm-4 col-md-3">
<div class="prod-container">
<a href="<?php echo base_url(); ?>index.php/welcome/productlist">
<div class="prod_img">
<img src="<?php echo base_url();?>images/img-electronics.jpg" width="100%"/>
</div>
<div class="prod_desc">
<div class="prod-round-icon"></div>
<h4 class="prod_title"><?php echo $name; ?></h4>
<p class="prod_text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="view-more"> view more</div>
</a>
</div>
</div>
答案 1 :(得分:0)
更改model.like this.use row()
返回带有匹配row.so的对象格式的结果,您可以使用箭头操作符访问列。
那你的$where
变量怎么样呢。
public function fetchData() {
$this->db->select('*');
$this->db->where($where);
$this->db->order_by('rand()');
$this->db->from('sub3_category');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
}
return false;
}
答案 2 :(得分:0)
在foreach循环中,记住$ row var是一个对象
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row->field;
}