Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass multiple Arrays to view in code igniter

my model is this,,, two functions view and spl

 function view(){

            $result = $this -> db -> get('tb_ourcity');
        return $result->result_array();

        //$query = $this->db->query('SELECT * from `adds`');
        //return $query->result_array();
}    


 function spl(){

            $result2 = $this -> db -> get('td_spei');
        return $result2->result_array();

        //$query = $this->db->query('SELECT * from `adds`');
        //return $query->result_array();
}

my controller looks like ths

    public function index()
{
    $data['result']=$this->load_city->view();
    $data2['result']=$this->load_city->spl();
    $this->load->view('home_view',$data);
}

i have two arrays named $data and $data2..

i want to use these two arrays simultaneously in the home_view. so how to pass these 2 arrays in single view

like image 844
NEET JASSI Avatar asked Nov 29 '22 23:11

NEET JASSI


1 Answers

Now you passing in your view $result contains only data of $this->load_city->spl();

public function index() 
{
   $data['result']=$this->load_city->view();
   $data2['result']=$this->load_city->spl();
   $this->load->view('home_view',$data);
}

You can pass two variables like

METHOD 1 Same variable name change key

public function index() 
{
   $data['data1']=$this->load_city->view();
   $data['data2']=$this->load_city->spl();
   $this->load->view('home_view',$data);
}

METHOD 2 Merge Your array

public function index() 
{
   $data['data1']=$this->load_city->view();
   $data2['data2']=$this->load_city->spl();
   $new_array = array_merge($data,$data2);
   $this->load->view('home_view',$new_array );
}

if you want to make shortcut just add direct in method

$this->load->view('home_view',array_merge($data, $data2));
like image 175
Maninderpreet Singh Avatar answered Dec 04 '22 08:12

Maninderpreet Singh