• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiple checkboxes

#1
PHP Code:
<div class="container">
 
                           <label>Service Offering</label></br>
                             <?
php
$new_array
=array('1'=>'Engineering and Design','2'=>'Operations and support','3'=>'Product management','4'=>'Developer relations and technical solutions','5'=>'Sales and account management','6'=>'Partnerships','7'=>'Sales and operations','8'=>'Administrative services','9'=>'Business strategy planning','10'=>'Finance solutions','11'=>'Legal and government relations','12'=>'Marketing and communications','13'=>'Real estate and workplace services','14'=>'Social impact solutions','15'=>'Consultancy services','16'=>'Investors and funding');

 
 if(isset($new_array)) 
     
 $services array_column($services,'service_offered');
 
        foreach($new_array as $key=>$val):

 
       if($db_key array_search($val,$services))
 
         ?>
            <input type="checkbox" name='service[]'    value="<?php echo $key ?>" checked ><?php echo $val?> <br/>
       <?php  }
 
        else
 
        
?>
           <input type="checkbox" name='service[]'    value="<?php echo $key ?>" ><?php echo $val?><br/>
        <?php   }

 endforeach; 
?>
 </div>




In this view file $services is fetched from another controller file as an object and then converted to array here so as to process the data in systematic way..........but here the problem i am facing is whenever i check multiple boxes suppose 3,,,,,the first in the list which is checked doest show up checked but the others show up......more cleary explaining....suppose $services has service1,service2 and service3................when i check these three boxes and click on update button it stores all the three services in database table but only service2 and service3 are being shown as checked.plz help me..i did print_r and checked the value of var and service both exactly matching then too service1 goes to else part 
Reply

#2
The problem you are describing is probably here:

PHP Code:
if ($db_key array_search($val$services)) 

If $val is the first entry in $services, the key is probably 0, so your if() statement evaluates to false and moves on to the else clause. The manual entry for array_search() includes a warning about this behavior: http://php.net/manual/en/function.array-...turnvalues

So, you could rewrite this as:

PHP Code:
$db_key array_search($val$services);
if (
$db_key !== FALSE

or you could do something like this:

PHP Code:
$db_key array_search($val$services);
?>
<input type='checkbox' name='service[]' value='<?php echo $key?>'<?php echo $db_key === false '' ' checked="checked"'?> /> 

or you could use the form helper's form_checkbox() or set_checkbox() functions.
https://codeigniter.com/userguide2/helpe...elper.html
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.