我还是CI的新手。我的一个朋友告诉我要开始研究它。我正在尝试将我刚才用PHP做的网站转换为CI。但我一直遇到问题。
我尝试的每次谷歌搜索似乎都无法找到结果。
我需要在同一页面上拥有2个不同的查询。
我的模特
class Fittv_model extends CI_Model
function __construct()
{
parent::__construct();
$this->load->database();
}
function get_fittv_chain()
{
//get all entry
$club_id = $_GET['club_id'];
$query = $this->db->query("SELECT * FROM company_details WHERE id = '$club_id'");
return $query->result();
}
function get_last_installs()
{
//this will get the last 3 installs
$club_id = $_GET['club_id'];
$last_installs = $this->db->query("SELECT * FROM fit_tv_locations WHERE club_id = '$club_id' ORDER BY date DESC LIMIT 3");
return $last_installs->result();
}
我的控制器。
class Fittv extends CI_Controller {
function index()
{
$this->load->model('fittv_model');
##$data['query'] = $this->fittv_model->get_fittv_chain();
$data = $this->fittv_model->get_fittv_chain();
$data = $this->fittv_model->get_last_installs();
$this->load->helper('url');
$this->load->view('fittv/index', $data);
}
}
问题是我对如何在视图页面上查看2个不同的模型没有任何线索。
我目前的错误如下:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: last_installs
Filename: fittv/index.php
Line Number: 23
下面是一个简单的视图,我试图用
查看数据<?php
foreach ($last_installs->result() as $row)
{
echo $row->location_name;
}
?>
答案 0 :(得分:1)
您必须使用associative array
来分配值
function index()
{
$this->load->model('fittv_model');
##$data['query'] = $this->fittv_model->get_fittv_chain();
$data['chain'] = $this->fittv_model->get_fittv_chain();
$data['installs'] = $this->fittv_model->get_last_installs();
$this->load->helper('url');
$this->load->view('fittv/index', $data);
}
在视图页面中使用$chain
和$installs
变量。
答案 1 :(得分:1)
您的模型中存在语法错误。 您应该更喜欢CodeIgniter Active Record Method类来执行数据库查询。
class Fittv_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function get_fittv_chain()
{
$tablename = 'yourTable';
//get all entry
$club_id = $_GET['club_id'];
$query = $this->db->get_where($tablename, array('id' => $club_id));
return $query->result();
}
function get_last_installs()
{
//this will get the last 3 installs
$club_id = $_GET['club_id'];
$last_installs = $this->db->query("SELECT * FROM fit_tv_locations WHERE club_id = '$club_id' ORDER BY date DESC LIMIT 3"); //Convert this also
return $last_installs->result();
}
}
你必须从控制器向你的视图传递assosiative array 。
class Fittv extends CI_Controller {
function index()
{
$this->load->model('fittv_model');
##$data['query'] = $this->fittv_model->get_fittv_chain();
$data['chain'] = $this->fittv_model->get_fittv_chain();
$data['install'] = $this->fittv_model->get_last_installs();
$this->load->helper('url');
$this->load->view('fittv/index', $data);
} }
在您看来,您的数据将以变量名称 $ chain和$ install 提供(无论您为数据数组添加的任何键在您的视图中变为多变)。