[英]use two tables from same database in one view codeigniter
Hi guys i have problem using two tables from same DB in one view.大家好,我在一个视图中使用来自同一个数据库的两个表时遇到问题。 When i use only one of the tables it works but when i try to fetch both of them my view does not load.当我只使用其中一个表时,它可以工作,但是当我尝试获取它们时,我的视图不会加载。 This is my model: schedule_model.php:这是我的模型:schedule_model.php:
<?php
class Schedule_model extends CI_Model {
public function __construct()
{
}
public function get_schedules()
{
$this->db->select('schedule.id, schedule.name');
$this->db->from('Schedule');
$this->db->group_by("schedule.id");
$query = $this->db->get();
return $query->result_array();
}
public function get_subscheds()
{
$this->db->select('subsched.id, subsched.name, subsched.enable, subsched.from, subsched.to, subsched.mode, subsched.fcu1, subsched.fcu2, subsched.fcu3, subsched.pon, subsched.vt, subsched.sr, subsched.cet, subsched.pet, subsched.sab, subsched.ned');
$this->db->from('Subsched');
$this->db->group_by("subsched.id");
$query = $this->db->get();
return $query->result_array();
}
}
?>
This is the controller: schedule.php这是控制器:schedule.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Schedule extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('schedule_model');
}
public function index()
{
if(!$this->session->userdata('loggedIn')){
redirect("login");
}
$data['schedules'] = $this->schedule_model->get_schedules();
//$data['subscheds'] = $this->schedule_model->get_subscheds();
$this->load->view('schedule', $data); //my view is also named schedule :)
}
}
?>
Notice that i have commented the line that calls the function which gets the elements from the second table.请注意,我已经注释了调用从第二个表中获取元素的函数的行。 If i uncomment it, my view freezes.如果我取消注释,我的观点就会冻结。 Otherwise it works fine with only one table.否则它只适用于一张桌子。 In my view i loop through elements in foreach loops.在我看来,我遍历 foreach 循环中的元素。 What could be the problem?可能是什么问题呢? Thanks谢谢
Hope this one help you..希望这个能帮到你..
$schedules = $this->schedule_model->get_schedules();
$subscheds = $this->schedule_model->get_subscheds();
$data['schedules'] = $schedules;
$data['subscheds'] = $subscheds;
## and then pass to view.
$this->load->view('schedule', $data);
Try this one..I have made some modification..You can access "schedules" as $schedules and "subscheds" as $subscheds in 'schedule' view you have defined.试试这个..我做了一些修改..你可以在你定义的'schedule'视图中访问“schedules”作为$schedules和“subscheds”作为$subscheds。
$data['schedules'] = $this->schedule_model->get_schedules();
$data['subscheds'] = $this->schedule_model->get_subscheds();
This will load both datas from shedules table and subsheds table.这将加载来自 shedules 表和 subsheds 表的数据。 and then you can load view with these data by..然后你可以通过..加载带有这些数据的视图
$this->load->view('schedule', $data);
Now you can loop through $data by using $data['schedules'] and $data['subscheds'] to use db fields wherever necessary.现在,您可以使用 $data['schedules'] 和 $data['subscheds'] 循环遍历 $data,以便在必要时使用 db 字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.