0

I want to insert data in mysql as well as show in browser the insert value Is it possible to get all $insertdata value

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'];

 foreach($names as $key => $name){
             $name= "your specified name";
             $insertdata = array();
             $insertdata['work'] = $works[$key];
             $insertdata['name'] = $name;
             $this->common_model->insert($insertdata);
        //$insert = mysql_query("INSERT INTO work(name,work) values ( '$name','$work')");
            }
  //view code will add here to show data in browser
  }
}

Thanks in advance.

Russell
  • 1,624
  • 5
  • 23
  • 43

1 Answers1

1

Definitely yes, look at this code:

$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'];
  }
  //view code will add here to show data in browser
}

or you can display the inserted row in your code after this line:

$this->common_model->insert($insertdata);
echo $insertdata['work'];
echo $insertdata['name'];
Firas Rassas
  • 509
  • 4
  • 12
  • getting `Message: array_push() expects parameter 1 to be array, null given` and `Invalid argument supplied for foreach()` – Russell Oct 17 '15 at 09:54
  • you should add this line before the loop: $allValues = array(); // define array – Firas Rassas Oct 17 '15 at 09:56
  • Now `Message: Array to string conversion` – Russell Oct 17 '15 at 09:57
  • sorry my bad, you can't echo an array.. but you can make a new loop for $insertRow['work'] and echo every value. – Firas Rassas Oct 17 '15 at 10:00
  • `echo $insertdata['work'];` and `echo $insertdata['name'];` just shows one data. I need to show all data – Russell Oct 17 '15 at 10:00
  • If I put ` foreach($stockdata as $insertRow){ echo $insertRow['name']; }` it shows `Message: Illegal string offset 'name'` – Russell Oct 17 '15 at 10:02
  • try to use my code above.. what I did is defining a new array and push every row($insertdata) you insert into database to it. after the loop finish, $allValues have all rows inserted to database, and you can display them. – Firas Rassas Oct 17 '15 at 10:03
  • Use `insert_batch()` db method instead `insert()` to avoid insert in foreach loop. Check [here](http://stackoverflow.com/questions/17875706/how-to-create-codeigniter-batch-insert-array). – Tpojka Oct 17 '15 at 13:48