Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter passing data controller to view

Here is my controller:

   class CommonController extends CI_Controller {
      public function __construct() {
        parent::__construct();
          $this->load->model('common_model'); //load your model my model is "common model"
      }

     public function add_work(){
     $names = $_POST['name'];
     $works = $_POST['work'];

     $allValues = array(); // array to contains inserted rows 
     foreach($names as $key => $name){
             $name= "your specified name";
             $insertdata = array();
             $insertdata['work'] = $works[$key];
             $insertdata['name'] = $name;
             $this->common_model->insert($insertdata);

             array_push($allValues,$insertdata);
        //$insert = mysql_query("INSERT INTO work(name,work) values ( '$name','$work')");
            }
  foreach($allValues as $insertRow){
     echo $insertRow['work'];
     echo $insertRow['name'];//this shows data well. but how to pass data in view.php
  }
  //view code will add here to show data in browser
}

Basically I want to pass all data to view.php for printing or exporting purpose. How can I do so.

like image 296
nikolas Avatar asked Jun 24 '26 13:06

nikolas


1 Answers

To load a view you should do like this.

$this->load->view("filename");

If you want to pass data to view, you should do like this.

$this->load->view("filename",$data);

$data should have all parameters which you want to print in view.

The syntax goes like this.

$this->load->view("filename","data to view","Returning views as data(true / false");

If third parameter is true, view will come as data. It will not go to browser as output.

Edit:

Change

$this->load->view('print_view',$insertdata);

to

$data['insertdata'] = $insertdata; 
$this->load->view('print_view',$data);

For more info, check this link

like image 163
Niranjan N Raju Avatar answered Jun 26 '26 03:06

Niranjan N Raju



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!