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