0

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
2
  • Can you show us the database query and how you're trying to store the result? Commented Aug 28, 2014 at 17:33
  • And the result of var_dump($query); exit(); is? Commented Aug 28, 2014 at 17:35

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@NaincyGupta you need to keep the result variable inside an array and then pass array in view.
@k10gaurav: he is trying to get post variable in model which is wrong.
hmm... right @NaincyGupta but variable accessibility requires to be in array so that if I have n number of variables in controller they would be accessible in view.
@k10gaurav: I have made changes in my code accordingly. :)
@NaincyGupta me too. Happy coding :)
0

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);
    }
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.