![](/img/trans.png)
[英]Compare two different columns from two different tables and show other column's data from either table in laravel 5.2
[英]Compare and show different data from two tables
我有兩張桌子。 技能,其中包含所有可用技能。 和user_skills,其中包含用戶獲得的技能。 我需要輸出所有可用的技能,並且用戶所擁有的那些技能(我通過session['username']
選擇它)將被標記(選中復選框)。
$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;
}
}
我怎么可能做這項工作? html的一部分是:
<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>
考慮到您已聲明一個user_skills數組($ user_skills_array)並具有數據,請使用以下html代碼
<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>
請嘗試以下說明。
<?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>
首先,您已經掌握了所有技能。 接下來,我將您的不同技能陣列優化為一個,並列出了所有技能類型。
可用技能通過其類型ID進行索引(請參閱$ skillTypes)。
接下來,我假設您的用戶技能集合可能如圖所示。
在HTML部分中,我將遍歷每種技能類型,並在嵌套的foreach中回顯所有相應的技能。 如果當前迭代的技能ID是user-skill-relation的in_array,則設置“ checked”屬性。
為了闡明這一點,我在本示例中使用了短語法。
[] --> array()
<?= --> <?php echo
foreach () : endforeach;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.