如何从数组中获取所有数据?

时间:2015-10-17 09:33:36

标签: php codeigniter

我想在mysql中插入数据以及在浏览器中显示插入值是否有可能获得所有$ insertdata值

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
  }
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

肯定是的,看看这段代码:

$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
}

或者您可以在此行后面的代码中显示插入的行:

$this->common_model->insert($insertdata);
echo $insertdata['work'];
echo $insertdata['name'];