简体   繁体   中英

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:

 <?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

<?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. 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.

$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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM