Receive message values ​​when a key is unknown in CodeIgniter

CodeIgniter allows you to access POSTed data through:

$this->input->post('input_name'); 

where 'input_name' is the name of the form field. This works well for a static form, where each input name is known in advance.

In my case, I am loading a collection of key / value pairs from the database. The form contains text input for each key / value pair.

I am wondering if there is a way to get an array of published data via CodeIgniter api?

Thanks!

+6
input post php forms codeigniter
source share
4 answers

According to the documentation, no. I would suggest just using array_keys($_POST) to get the keys.

+9
source share
 foreach($this->input->post() as $key => $val) { echo "<p>Key: ".$key. " Value:" . $val . "</p>\n"; } 

which can be used for

+5
source share

Of course, if you have an array of keys from the database, you can use this, for example:

 foreach ($arrayFromDb as $key => $value) { $newValue = $this->input->post($key); } 

Then you have the advantage that people, if people submit additional fields (for example, by changing the form and submitting it themselves), these fields will be ignored

+1
source share
 $array_db_columns = $this->db->query('SHOW COLUMNS FROM ci_props'); $array_db_columns = $array_db_columns->result_array(); $array_save_values = array(); foreach ( $array_db_columns as $value ) { $array_save_values[$value['Field']] = $this->input->post($value['Field']); } 

Paste:

$this->db->insert('props', $array_save_values);

update:

$this->db->where('id',$id); $this->db->update('props',$array_save_values);

+1
source share

All Articles