Welcome Guest, Not a member yet? Register   Sign In
Checkbox checked value with foreach 2 arrays of different size
#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


Messages In This Thread
RE: Checkbox checked value with foreach 2 arrays of different size - by rubens - 03-06-2017, 08:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB