Welcome Guest, Not a member yet? Register   Sign In
when repopulate of input item insert as null
#4

(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>';
 
 }


View:
PHP Code:
<?= validation_errors(); ?>

<?= form_open();?>

<table>

<tr><th>ID</th><th>Name</th><th>NOC</th></tr>

<?php foreach($students as $student) : ?>

    <tr>
    <td><?= $student['id'];?></td>
    <td><?= $student['name'];?></td>
    <td>
        <?= form_input(
 
          array(
 
             'name'=>'noc[' $student['id'] . ']',
 
             'value'=> set_value('noc[' $student['id'] . ']',$student['noc']),
 
             'type'=>'number',
 
             'min' => 1,
 
             'max' => 999
           
));
        
?>
    </td>
 </tr>

<?php endforeach; ?> 

</table>

<?= form_submit('submit','Submit');?>
<?= form_close
(); 
Reply


Messages In This Thread
RE: when repopulate of input item insert as null - by Wouter60 - 09-17-2018, 08:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB