I've got two tables. Skills, that contains all the available skills. and user_skills, that contains which skills user got. I need to output all the available skills, and those skills that the user have (I select it by session['username']
) will be marked (checked checkbox).
$get_all_skills = mysqli_query($conn, "SELECT * FROM skills");
$web_design = array();
$web_develop = array();
$automation = array();
$security = array();
while($show_row = mysqli_fetch_array($get_all_skills)){
switch($show_row['skill_type']){
case '1':
array_push($web_design, $show_row['skill_name']);
break;
case '4':
array_push($web_develop, $show_row['skill_name']);
break;
case '3':
array_push($automation, $show_row['skill_name']);
break;
case '2':
array_push($security, $show_row['skill_name']);
break;
}
}
How can possibly I make this work? Part of the html is:
<div class="">
<ul class="to_do">
<?php
for($i=0;$i<count($web_develop);$i++){
?>
<li>
<p>
<input type="checkbox" class="flat"> <?php echo $web_develop[$i];?> </p>
</li>
<?php } ?>
</ul>
</div>
Considering you have a user_skills array ($user_skills_array) declared and have the data, use the below html code
<div class="">
<ul class="to_do">
<?php
for($i=0;$i<count($web_develop);$i++){
// check the global skill is available in the user skill set
$checked = (in_array($web_develop[$i], $user_skills_array)) ? 'checked="checked"': '';
?>
<li>
<p>
<input type="checkbox" class="flat" <?php echo $checked;?>> <?php echo $web_develop[$i];?> </p>
</li>
<?php } ?>
</ul>
</div>
try the following, explanation below.
<?php
$get_all_skills = mysqli_query($conn, "SELECT * FROM skills");
$availableSkills = [];
$skillTypes = [
1 => 'Webdesign',
2 => 'Security',
3 => 'Automation',
4 => 'Web Development',
];
while($show_row = mysqli_fetch_array($get_all_skills)){
$skill = [
'id' => $show_row['id'];
'name' => $show_row['name'];
];
$skillType = $show_row['skill_type'];
$availableSkills[$skillType][] = $skill;
}
// assuming user_skills:
$userSkills = [
1,
2,
5,
8
];
?>
<ul class="to_do">
<?php foreach ($availableSkills as $type => $skillsForType) : ?>
<li>
<?= $skillTypes[$type] ?>
<ul>
<?php foreach ($skillsForType as $skill) : ?>
<li>
<input
type="checkbox"
class="flat"
name="skill[<?= $type ?>]"
value="<?= $skill['id'] ?>"
<?= in_array($skill['id'], $userSkills) ? 'checked="checked"' : ''
> <?= $skill['name'] ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
First you gather all skills, as have you already done. Next I optimized your different skills array into one and listed all skill types.
The available skills are indexed by their type id (see $skillTypes).
Next I assumed your user-skill collection could be like shown.
In the HTML section I am iterating over every skill type and in a nested foreach echo all corresponding skills. If the skill id of the current iteration is in_array of the user-skill-relation the "checked" attribute is set.
To clarify I used short syntax in this example.
[] --> array()
<?= --> <?php echo
foreach () : endforeach;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.