• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checkbox checked value with foreach 2 arrays of different size

#1
Hi guys.

I need to know how I can do the code below in a better way, because although it works fine for what I need, the code does not look good / organized.

Explanation of the code:

. $list => an array of data coming from a database table with 2 fields (Id and name)
. $sess_temp = an array containing some values (strings and arrays). This array was created by function $this->session->set_tempdata()

Values of the variable $sess_temp:
PHP Code:
array(
'type'=> 1
'step1' => array(SOME DATA HERE), 
'step2' => array(SOME DATA HERE), 
'step3' => array(SOME DATA HERE),
'step4' => array(checkbox values here// Like: array(0 => checkboxId1, 1 => checkboxId10)
);

//if user checked some checkboxes(like checkbox value 1/3/15/50), then step4 will be
'step4' => array(
 
          '0' => '1',
 
          '1' => '3'
 
          '2' => '15'
 
          '3' => '50'


Values of the variable $list:
PHP Code:
array(
 
 array('id' => 1'name' => 'List item 1'),
 
 array('id' => 2'name' => 'List item 2'),
 
 array('id' => 3'name' => 'List item 3'),
 
 array('id' => 4'name' => 'List item 4'),
 
 array('id' => 5'name' => 'List item 5'),
 
 //...continue with more data
);

// id == int(11) PRIMARY auto increment
// name == varchar(232) 

PHP Code:
if(isset($sess_temp['step4'])){
 
     //Foreach step4 array to get checkbox checked values
 
     foreach($sess_temp['step4'] as $key => $value){
 
      $sess[$value] = $value;
 
     }
}
//Foreach array $list from database
foreach ($list as $lt){
 
 //Add checked attr if ID of current item match with the array stored in $sess_temp['step4']
 
 if(isset($sess[$lt['id']]) == $lt['id']){
 
    $ck 'checked';
 
  }else{
 
       $ck='';
 
  }
 
  echo '<li class="checkbox">
             <label>
                 <input type="checkbox" name="lists[]" value="'
.$lt['id'].'" '.set_checkbox('lists'$lt['id']).' '.$ck.' />
                         '
.$lt['name'].'
             </label>
         </li>'
;                                           


This code will print this:
PHP Code:
<li class="checkbox"><label><input type="checkbox" name="lists[]" value="1" checked>Item name 1 here</label></li//Checked attr, like above mentioned
<li class="checkbox"><label><input type="checkbox" name="lists[]" value="2">Item name 2 here</label></li>
<
li class="checkbox"><label><input type="checkbox" name="lists[]" value="3" checked>Item name 3 here</label></li//Checked attr, like above mentioned
<li class="checkbox"><label><input type="checkbox" name="lists[]" value="4">Item name 4 here</label></li>
<!-- CONTINUE -->
<
li class="checkbox"><label><input type="checkbox" name="lists[]" value="15" checked>Item name 5 here</label></li//Checked attr, like above mentioned
<li class="checkbox"><label><input type="checkbox" name="lists[]" value="16">Item name 16 here</label></li>
<!-- CONTINUE -->
<
li class="checkbox"><label><input type="checkbox" name="lists[]" value="49">Item name 49 here</label></li>
<
li class="checkbox"><label><input type="checkbox" name="lists[]" value="50" checked>Item name 50 here</label></li//Checked attr, like above mentioned 
Who knows how to redo the code to keep it working just the way it is now, but in a less polluted and organized way, please comment below.
Thanks guys
Reply

#2
In your 3rd code-block, you have 2 foreach ... loops.
Forget the first one.
In the second one:
PHP Code:
$ck in_array($sess_temp['step4'],$lt['id']) ? 'checked' NULL
This single line of code can "calculate" the correct value for $ck. It checks if the $lt['id'] value is in the $sess_temp['step4'] array.

The step4 array is an associative array, but you don't do anything with the $key, so you don't really need that.

The rest of the code seems OK to me.
Reply

#3
(03-06-2017, 11:56 AM)Wouter60 Wrote: In your 3rd code-block, you have 2 foreach ... loops.
Forget the first one.
In the second one:
PHP Code:
$ck in_array($sess_temp['step4'],$lt['id']) ? 'checked' NULL
This single line of code can "calculate" the correct value for $ck. It checks if the $lt['id'] value is in the $sess_temp['step4'] array.

The step4 array is an associative array, but you don't do anything with the $key, so you don't really need that.

The rest of the code seems OK to me.

Hi friend.

I just had to make two changes to make it perfect, see how it is now.

PHP Code:
foreach ($list as $lt){
 
 $ck = @in_array($lt['id'],$sess_temp['step4']) ? 'checked' NULL;
 
  echo '<li class="checkbox">
             <label>
                 <input type="checkbox" name="lists[]" value="'
.$lt['id'].'" '.set_checkbox('lists'$lt['id']).' '.$ck.' />
                         '
.$lt['name'].'
             </label>
         </li>'
                                          


I just had to invert the values of the in_array, because it was necessary to see if the second parameter was contained in the first, not the other way around.

I also had to add the @ so that it only returns "true" if the $sess_temp['step4'] is different from empty.

Thanks for your suggestion, it worked very well for what I was needing.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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