west
west

Reputation: 25

how to store database query result in a variable in view

I want to store my database query result in my view section in a variable.I am trying but not work.

My View code

$cd=''.base_url().'/video/series/';
$count = count($cd);
for ($i = 0; $i < $count; $i++) 
{
  print'"'.$cd[$i][1].'",';  

}

My Controller code

public function series() {

    $result= $this->video_model->series_list();

    return $result;


}

My Model Code

function series_list()
{

    $string = trim($this->input->get_post('term'));
    $query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");


    return $query->row_array();

}

$cd=''.base_url().'/video/series/'; not get any array data ,only get blank data

Upvotes: 0

Views: 746

Answers (2)

k10gaurav
k10gaurav

Reputation: 462

You need to pass variable inside some array(in view method) so that all variables inside common array would be accessible on view page

Controller code would be

public function series() 
{
  $inputData     =trim(strip_tags($this->input->get_post('term')));
  $data['result']= $this->video_model->series_list($inputData);
  $this->load->view('directory/viewpage',$data); //$data is array with all variables inside it
}

Model code would be

function series_list($data)
{
 $string = $data;
 $query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");
 if( $query->num_rows>0)
 {
  return $query->row_array();
 }
 else
 {
   return false;
 }
}

View code would be

<?php
     if(isset($result)&&($result!=''))
    {
      echo "<pre/>";
      print_r($result); // Or var_dump($result);
    }
?>

Upvotes: 0

Naincy
Naincy

Reputation: 2943

You can get post variable value in controller only. But you trying to get it in model. That's wrong.

Controller:

public function series() {
$string = trim($this->input->get_post('term'));    
$data['result']= $this->video_model->series_list($string);
$this->load->view('folder/filename', $data);  
// in your case i think folder= video and filename = series
// in this way you can pass value from controller to view

}

Model:

function series_list($string = null)
{
    if($string != ''){
       $query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");
       return $query->row_array();
    }
    else
       return false;
}

View:

<?php
 var_dump($data);
?>

you can get your resultset in view and can play with it as you want.

Upvotes: 2

Related Questions