复选框并从数据库中设置其默认选中的值

问题描述:

I have the following database table called artist_genres. I currently have a function which finds all genre_id's by artist and echo's them in an object oriented way:

<?php
$artistgenres = Artistgenres::find_all_genres_by_artist_id($_SESSION['artist_id']);
foreach($artistgenres as $artistgenre){
  echo $artistgenre->genre_id."<br>";
}
?>

enter image description here

When the artist with id 8 logs in, and goes to this page, I would like for him to be able to update the current genre_ids associated with them.

The form data looks like:

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<div class="checkbox">
  <label><input type="checkbox" name="Genres[]" value="2">Classical</label>
</div>
<div class="checkbox">
  <label><input type="checkbox" name="Genres[]" value="3">Hiphop</label>
</div>
<div class="checkbox">
  <label><input type="checkbox" name="Genres[]" value="4">Jazz</label>
</div>
<input type="submit" name="submit" class="btn btn-primary btn-lg active" id="grad" value="Login" />
</form> 

So effectively, for the classical example, what I want to achieve is to look up and find all genre_id, see if the value 2 is in the list of genre_ids, if so change the markup to:

  <label><input type="checkbox" name="Genres[]" value="2"<? php if(somecondition = 2) {echo "checked"} ?> >Classical</label>

However, I'm having a hard time putting this into code. Can you help me out?

我有以下数据库表名为artist_genres。 我目前有一个函数可以找到艺术家的所有genre_id,并以面向对象的方式回显它们: p>

 &lt;?php 
 $ artistgenres = Artistgenres :: find_all_genres_by_artist_id($ _ SESSION)  ['artist_id']); 
foreach($ artistgenres as $ artistgenre){
 echo $ artistgenre-&gt; genre_id。“&lt; br&gt;”; 
} 
?&gt; 
  code>   pre> 
 
 

p>

当身份识别为8的艺术家登录并转到此页面时,我希望 让他能够更新与他们相关的当前genre_ids。 p>

表单数据如下所示: p>

 &lt; form action =“&lt;?php echo $ _SERVER ['PHP_SELF'];  ?&gt;”中 method =“post”&gt; 
&lt; div class =“checkbox”&gt; 
&lt; label&gt;&lt; input type =“checkbox”name =“Genres []”value =“2”&gt; Classical&lt; / label&gt  ; 
&lt; / div&gt; 
&lt; div class =“checkbox”&gt; 
&lt; label&gt;&lt; input type =“checkbox”name =“Genres []”value =“3”&gt; Hiphop&lt; / label&gt  ; 
&lt; / div&gt; 
&lt; div class =“checkbox”&gt; 
&lt; label&gt;&lt; input type =“checkbox”name =“Genres []”value =“4”&gt; Jazz&lt; / label&gt  ; 
&lt; / div&gt; 
&lt; input type =“submit”name =“submit”class =“btn btn-primary btn-lg active”id =“grad”value =“Login”/&gt; 
&lt; / 形式&GT;  
  code>  pre> 
 
 

如此有效,对于经典示例,我想要实现的是查找并找到所有genre_id,看看值2是否在genre_ids列表中 ,如果是这样,将标记更改为: p>

 &lt; label&gt;&lt; input type =“checkbox”name =“Genres []”value =“2”&lt;?  php if(somecondition = 2){echo“checked”}?&gt;  &gt;经典&lt; / label&gt; 
  code>  pre> 
 
 

但是,我很难将其放入代码中。 你能救我吗? p> div>

Put all of the selected genres of the user in an array:

$selectedgenres = array();
foreach($artistgenres as $artistgenre){
    $selectedgenres[] = $artistgenre->genre_id;
}

List all genres as checkboxes (I assume you have a separate table for all of your genres). Then use in_array() to check each genre if it is part of the selected genres by the user:

foreach(your condition here for getting all genres){

?>    
    <div class="checkbox">
        <label><input type="checkbox" name="Genres[]" value="<?=($genreid)?>"><?=($genredesc)? <?php echo (in_array($genreid, $selectedgenres))?' checked':''; ?>></label>
    </div>

<?php

}