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

Code:
<input type="number" id="primaryincome1"  min="1"  max="999" name="noc[]"
value="<?php echo (!isset($student1['noc'])) ? 0 : $student1['noc'] ?>" >

<input type="number" id="primaryincome2"  min="1"  max="999" name="noc[]"
value="<?php echo (!isset($student2['noc'])) ? 0 : $student2['noc'] ?>">

<input type="number" id="primaryincome3"  min="1"  max="999" name="noc[]"
value="<?php echo (!isset($student3['noc'])) ? 0 : $student3['noc'] ?>" >

<input type="number" id="primaryincom4"  min="1"  max="999" name="noc[]"
value="<?php echo (!isset($student4['noc'])) ? 0 : $student4['noc'] ?>" >

<input type="number" id="primaryincome5"  min="1"  max="999" name="noc[]"
value="<?php echo (!isset($student5['noc'])) ? 0 : $student5['noc'] ?>" >

<input type="number" id="primaryincome6"  min="1"  max="999" name="noc[]"
value="<?php echo (!isset($student6['noc'])) ? 0 : $student6['noc'] ?>"


Code:
$certids = $this->input->post('certid');  
$nocs = $this->input- >post('noc');
$result = array();
foreach ($certids as $index=>$certid)
{
   $result[] = $certid . '_' . $nocs[$index];
}

echo '<pre>';print_r($result);echo '</pre>';
after I made changes in the input item, it gives an output:

Code:
Array
 (
[0] => 1_2
[1] => 2_2
[2] => 3_2
[3] => 4_2
[4] => 5_2
[5] => 6_2
[6] => 7_
 )
Reply
#2

You do know that arrays start at 0
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(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.
Reply
#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
#5

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


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
(); 

In the thread-71588 you answered my post. it works well. The problem is only repopulate that items , some times it working and some time show error.

My form should be like.

Attached Files Thumbnail(s)
   
Reply
#6

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)
PHP Code:
<?php foreach ($certs as $cert) : ?>
   <tr>
     <td><?= form_input(array( 'name' => 'noc[' $cert['id'] . ']''value' => set_value('noc[' $cert['id'] . ']'$cert['noc']));?></td>
   </tr>
<?php endforeach; ?>

A good way to find out what's in the $_POST data is this:
PHP Code:
echo '<pre>';
print_r($_POST);
echo 
'</pre>';
die(); 
Reply
#7

(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)
PHP Code:
<?php foreach ($certs as $cert) : ?>
   <tr>
     <td><?= form_input(array( 'name' => 'noc[' $cert['id'] . ']''value' => set_value('noc[' $cert['id'] . ']'$cert['noc']));?></td>
   </tr>
<?php endforeach; ?>

A good way to find out what's in the $_POST data is this:
PHP Code:
echo '<pre>';
print_r($_POST);
echo 
'</pre>';
die(); 
i ve attached my View. kindly change according to my requirement


Code:
$data['getcert1'] = $this->User_Model->getcert1($appno);
 $data['getcert2'] = $this->User_Model->getcert2($appno);
 $data['getcert3'] = $this->User_Model->getcert3($appno);
 $data['getcert4'] = $this->User_Model->getcert4($appno);
 $data['getcert5'] = $this->User_Model->getcert5($appno);

Model
Code:
public function getcert1($appno)
{
 $regno = $this->session->userdata('username');
 $this->db->select('id,certificate_id,fee,APPNO,regno,certid,noc , sum(noc*fee) as paid');
// $this->db->select('*');
 $this->db->from('certificate_det');
 $this->db->where('appdet.regno',$regno);
 $this->db->where('appdet.appno',$appno);
 $this->db->where('certificate_det.certificate_id',1); // based on
 $this->db->join('appdet', 'appdet.certid = certificate_det.certificate_id', 'left');
 $query = $this->db->get();
 return $query->result_array();
 echo $query;  
   }

Code:
public function getcert2($appno)
{
 $regno = $this->session->userdata('username');
 $this->db->select('id,certificate_id,fee,APPNO,regno,certid,noc , sum(noc*fee) as paid');
// $this->db->select('*');
 $this->db->from('certificate_det');
 $this->db->where('appdet.regno',$regno);
 $this->db->where('appdet.appno',$appno);
 $this->db->where('certificate_det.certificate_id',2);
 $this->db->join('appdet', 'appdet.certid = certificate_det.certificate_id', 'left');
 $query = $this->db->get();
 return $query->result_array();
 echo $query;  
   }
Code:
Controller
********
$certids = $this->input->post('certid');  //here you leave the [ ] out!
    $nocs = $this->input->post('noc');
    $result = array();
    foreach ($certids as $index=>$certid)
 {
       $result[] = $certid . '_' . $nocs[$index];
    }

 echo '<pre>';
 print_r($result);
 echo '</pre>';
    $date = new DateTime("now");
    $today = $date->format('Y-m-d');

    foreach($result as $value)
       {
       list($certid,$noc) = explode ('_',$value);
        $insert = array();
       $insert[] = array(
           'appno' => $appno,
           'regno' => $regno,
          'certid' => $certid,
             'noc' => $noc,
             'date' => $today
               );      
     
       $this->load->model('user_Model');
       $this->User_Model->studreginsert($insert);        
      }


Attached Files
.php   application.php (Size: 7.7 KB / Downloads: 27)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB