(09-17-2018, 03:48 AM)InsiteFX Wrote: You do know that arrays start at 0
I know that. at first time insertion is working well. after repopulate and made some changes in that input item it shows error. why? tell me the reason.
09-17-2018, 08:52 AM (This post was last modified: 09-17-2018, 10:19 AM by Wouter60.)
It occurs to me that you DON't know how arrays work.
If you build up your view by manually creating inputs with $student1, $student2 etc., you're giving yourself a really hard time.
What if you had 200 students instead of 6? Very tedious work and very sensitive to errors.
You can make it much easier if you use the form_validation library.
Here's an example.
Controller:
PHP Code:
public function test() { $this->load->library('form_validation'); $this->form_validation->set_rules('noc[]','Number of courses','required');
if ($this->form_validation->run() == FALSE) { // just an example; in real life you would get these data from a database table $data['students'] = array( array( 'id' => 1, 'name' => 'student1', 'noc' => 0, ), array( 'id' => 2, 'name' => 'student2', 'noc' => 0, ), array( 'id' => 3, 'name' => 'student3', 'noc' => 0, ), array( 'id' => 4, 'name' => 'student4', 'noc' => 0, ), ); $this->load->view('test_students',$data); } else { echo '<pre>'; print_r($this->input->post()); echo '</pre>'; } }
09-17-2018, 11:52 PM (This post was last modified: 09-18-2018, 12:12 AM by kvanaraj.)
(09-17-2018, 08:52 AM)Wouter60 Wrote: It occurs to me that you DON't know how arrays work.
If you build up your view by manually creating inputs with $student1, $student2 etc., you're giving yourself a really hard time.
What if you had 200 students instead of 6? Very tedious work and very sensitive to errors.
You can make it much easier if you use the form_validation library.
Here's an example.
Controller:
PHP Code:
public function test() { $this->load->library('form_validation'); $this->form_validation->set_rules('noc[]','Number of courses','required');
if ($this->form_validation->run() == FALSE) { // just an example; in real life you would get these data from a database table $data['students'] = array( array( 'id' => 1, 'name' => 'student1', 'noc' => 0, ), array( 'id' => 2, 'name' => 'student2', 'noc' => 0, ), array( 'id' => 3, 'name' => 'student3', 'noc' => 0, ), array( 'id' => 4, 'name' => 'student4', 'noc' => 0, ), ); $this->load->view('test_students',$data); } else { echo '<pre>'; print_r($this->input->post()); echo '</pre>'; } }
Your greatest challenge is: how to repopulate only the fields that were enabled.
If you carefully study my last reply, you will see that the names of the input fields aren't just noc[] or cbx[], but the id of the current row is used as the key of the element.
Field arrays without a user defined index, automatically get a numeric key starting with 0 in the $_POST data.
Let's say you have 5 fields named "noc[]", and you only enable the second and fifth. In $this->input->post('noc') you will see just 2 elements being returned: noc[0] and noc[1]. So it's impossible to find out which ones were posted.
If the 5 fields have the names "noc[1]", "noc[2]", "noc[3]", "noc[4]" and "noc[5]", now after you post the form, $this->input->post('noc') will hold noc[2] and noc[5].
You can dynamically assign the keys of noc[…] in your foreach {} structure and repopulate them with set_value():
(View)
09-19-2018, 12:43 AM (This post was last modified: 09-19-2018, 02:02 AM by kvanaraj.)
(09-18-2018, 11:09 PM)Wouter60 Wrote: Your greatest challenge is: how to repopulate only the fields that were enabled.
If you carefully study my last reply, you will see that the names of the input fields aren't just noc[] or cbx[], but the id of the current row is used as the key of the element.
Field arrays without a user defined index, automatically get a numeric key starting with 0 in the $_POST data.
Let's say you have 5 fields named "noc[]", and you only enable the second and fifth. In $this->input->post('noc') you will see just 2 elements being returned: noc[0] and noc[1]. So it's impossible to find out which ones were posted.
If the 5 fields have the names "noc[1]", "noc[2]", "noc[3]", "noc[4]" and "noc[5]", now after you post the form, $this->input->post('noc') will hold noc[2] and noc[5].
You can dynamically assign the keys of noc[…] in your foreach {} structure and repopulate them with set_value():
(View)