简体   繁体   English

在一个视图中使用来自同一数据库的两个表

[英]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.

相关问题 CodeIgniter:在视图中显示两个表中的数据-使用一个循环? - CodeIgniter: Displaying data from two tables in a view - use one loop? 从Codeigniter的一个视图(取决于查询)中的两个表中获取数据 - Get Data from two tables in one view (depending queries) in Codeigniter 我们可以在Codeigniter的同一视图中使用两个数据库吗? - Can we use two database in same view in Codeigniter? 如何在Codeigniter中的视图中使用两个表数据 - How to use two tables data in a view in Codeigniter 如何在Codeigniter中从具有相同数据库列名称的两个联接表中获取数据? - How to fetch data from two join tables with same database column names in codeigniter? Codeigniter从数据库中加载第二个不同的数据数组,第二个依赖于第一个,并加载到视图中 - Codeigniter loading two different data array from a database with the second one is dependent with the first one and load into a view CodeIgniter:通过来自相同形式的两个数组填充两个表? - CodeIgniter: Populating two tables through two arrays from the same form? 如何合并两个表并使用一个名称进行调用。 [codeigniter] - How to combine two tables and use one name to call. [codeigniter] Codeigniter:从两个数据库表的帮助程序传递数据库 - Codeigniter: Passing databa from helper from two database tables 显示两个表中的数据-Codeigniter中的一对多关系 - Display data from two tables - one to many relationship in codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM