mardi 29 juin 2021

Codeigniter Checked Loop Data into DB

I want to do a weekly royalty points giveaway table for my member. However, the rewards points are not fixed amount (count by percentage) and has its minimum total weekly sales requirement.

i.e. $1000 x 10% = 10 Points.. and below $100 will not get any points

if i have a table loop results of all the members of have purchase something on my website within the selected week...i wanted to add a checkbox in front of those "ACTIVE" member, and i can directly giveaway the bonus to those members with "checked". I need to insert Member id and Point Amount into the mysql.

My View looks like

[Button Add Reward]
    Member  |   Total Sales  |   Points
[✓] Alex    |     $1000      |    10
[ ] Beck    |      $100      |    0
[✓] Cath    |     $2000      |    20

My View

   <form action="http://localhost/lkmaster/vip/add_rebate" method="post">
     <button type="submit" name="submit" class="btn btn-danger">Add Selected</button>
        <table class="table table-striped table-bordered" style="width:100%">
          <thead>
             <tr class="btn-primary">
                <th></th>
                       <th></th>
                       <th>Member</th>
                       <th>Point Amount</th>
                     </tr>
                 </thead>
                 <tbody>
                    <tr>
                       <td>
                         <input type="checkbox" name="member_id[]" value="281" checked>
                       </td>
                       <td>1</td>
                       <td class='text-left'><strong>Alex</strong></a></td>                    
                       <td>10                                      
                         <input type="hidden" name ="point_amount[]" value="10" >
                       </td>
                    </tr>
                    <tr>
                       <td>
                         <input type="checkbox" name="member_id[]" value="101" disabled>
                       </td>
                       <td>1</td>
                       <td class='text-left'><strong>Alex</strong></a></td>                    
                       <td>0                                   
                         <input type="hidden" name ="point_amount[]" value="0" >
                       </td>
                    </tr>
                    <tr>
                       <td>
                         <input type="checkbox" name="member_id[]" value="1" checked>
                       </td>
                       <td>1</td>
                       <td class='text-left'><strong>Alex</strong></a></td>                    
                       <td>20                                  
                         <input type="hidden" name ="point_amount[]" value="20" >
                       </td>
                    </tr>
                  
                 </tbody>
               </table>
         </form>
   

My Controller

    <?php
    public function add_rebate() {
        $member_id = $this->input->post('member_id'); //here i am getting member id from the checkbox
        $point_amount = $this->input->post('point_amount'); 
        
           for ($i=0; $i < sizeof($member_id); $i++) {
             $data = array('member_id' => $member_id[$i],
                           'point_amount' => $point_amount[$i]);
            $this->db->insert('tbl_bonus_giveaway',$data);
            
            }
    
                      
             return redirect(base_url('vip/rebate'),'refresh');
    }
    ?>

Something wrong with this code, as If I unchecked the previous rows.

For previous example, Alex get points with no issue if first row, but Cath will get "0" from Beck, if I uncheck ALEX from the list, it will add Cath's member ID but Alex's Points into database (previous rows points amount).

What do i miss there to make correct data go into DB correctly followed by its checked row.




Aucun commentaire:

Enregistrer un commentaire