0

我对 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;
    }
?>
4

2 回答 2

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变量。

Codeigniter 视图

于 2013-06-11T05:21:48.240 回答
1

您的模型中有语法错误。你应该更喜欢 CodeIgniter Active Record 方法类来执行数据库查询。

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();
   } 
}

而且您必须将关联数组从您的控制器传递到您的视图。

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下可用(您给数据数组的任何键都会在您的视图中变为变量)。

于 2013-06-11T05:33:53.270 回答