如何根据数据库值检查所有单选按钮?

时间:2022-09-11 13:46:25

I'm busy making a small comment system. I want users to be able to give ratings, besides giving comments, and right now i'm storing these rating values (that come from radio buttons) in the database. However, when I try to populate radio buttons with rating values for each comment from the database, the following script only checks the radio button that corresponds to the last value in the database.

我正忙着制作一个小评论系统。我希望用户能够给出评级,除了给出评论,现在我将这些评级值(来自单选按钮)存储在数据库中。但是,当我尝试使用数据库中每个注释的评级值填充单选按钮时,以下脚本仅检查与数据库中最后一个值对应的单选按钮。

    <?php 
$sql = mysql_query("SELECT * FROM comments WHERE id_post = '$id_post'") or die(mysql_error());;
while($affcom = mysql_fetch_assoc($sql)){ 
    $name = $affcom['name'];
    $email = $affcom['email'];
    $comment = $affcom['comment'];
    $rating = $affcom['rating'];
    $date = $affcom['date'];

    // Get gravatar Image 
    // https://fr.gravatar.com/site/implement/images/php/
    $default = "mm";
    $size = 35;
    $grav_url = "http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=".$default."&s=".$size;

?>
<div class="cmt-cnt">
    <img src="<?php echo $grav_url; ?>" />
    <div class="thecom">
        <h5><?php echo $name; ?></h5><span data-utime="1371248446" class="com-dt"><?php echo $date; ?></span>
        <br/>
        <p>
            <?php echo $comment; ?>
        </p>
        <p>

           <input type="radio" name="hoi" value="1" <?php echo ($rating=='1')?'checked':'' ?> />
           <input type="radio" name="hoi" value="2" <?php echo ($rating=='2')?'checked':'' ?> />
           <input type="radio" name="hoi" value="3" <?php echo ($rating=='3')?'checked':'' ?> />
           <input type="radio" name="hoi" value="4" <?php echo ($rating=='4')?'checked':'' ?> />
           <input type="radio" name="hoi" value="5" <?php echo ($rating=='5')?'checked':'' ?> />

        </p>
    </div>
</div><!-- end "cmt-cnt" -->
<?php } ?>

This is an image of what it looks like right now:

这是一张现在看起来像的图片:

如何根据数据库值检查所有单选按钮?

Can someone tell me how to check all of the boxes (based on database values)?

有人能告诉我如何检查所有框(基于数据库值)?

2 个解决方案

#1


0  

I don't understand your question properly???

我不明白你的问题???

If you want to check the radio buttons according to ratting you can use in this way:

如果你想根据鼠标检查单选按钮,你可以这样使用:

<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2"  />
           <input type="radio" name="hoi3" value="3"  />
           <input type="radio" name="hoi4" value="4" />
           <input type="radio" name="hoi5" value="5"  />
           <input type="radio" name="hoi6" value="5" />
</p>
</form>
<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2" checked />
           <input type="radio" name="hoi3" value="3"  />
           <input type="radio" name="hoi4" value="4"  />
           <input type="radio" name="hoi5" value="5"  />
           <input type="radio" name="hoi6" value="5"  />
</p>
</form>
<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2" checked />
           <input type="radio" name="hoi3" value="3" checked />
           <input type="radio" name="hoi4" value="4"  />
           <input type="radio" name="hoi5" value="5"  />
           <input type="radio" name="hoi6" value="5"  />
</p>
</form>
<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2" checked />
           <input type="radio" name="hoi3" value="3" checked />
           <input type="radio" name="hoi4" value="4" checked />
           <input type="radio" name="hoi5" value="5"  />
           <input type="radio" name="hoi6" value="5"  />
</p>
</form>
<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2" checked />
           <input type="radio" name="hoi3" value="3" checked />
           <input type="radio" name="hoi4" value="4" checked />
           <input type="radio" name="hoi5" value="5" checked />
           <input type="radio" name="hoi6" value="5"  />
</p>
</form>
<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2" checked />
           <input type="radio" name="hoi3" value="3" checked />
           <input type="radio" name="hoi4" value="4" checked />
           <input type="radio" name="hoi5" value="5" checked />
           <input type="radio" name="hoi6" value="5" checked />
</p>
</form>

Use different names for the tags. And you can put them inside the form tag.

为标记使用不同的名称。你可以将它们放在表单标签中。

#2


0  

Never mind, fixed it. The name was the problem. It should be unique for every set of radio buttons ;). Tnx for your help Dineth

没关系,修理它。这个名字就是问题所在。对于每组单选按钮,它应该是唯一的;)。 Tnx为您提供帮助Dineth

#1


0  

I don't understand your question properly???

我不明白你的问题???

If you want to check the radio buttons according to ratting you can use in this way:

如果你想根据鼠标检查单选按钮,你可以这样使用:

<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2"  />
           <input type="radio" name="hoi3" value="3"  />
           <input type="radio" name="hoi4" value="4" />
           <input type="radio" name="hoi5" value="5"  />
           <input type="radio" name="hoi6" value="5" />
</p>
</form>
<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2" checked />
           <input type="radio" name="hoi3" value="3"  />
           <input type="radio" name="hoi4" value="4"  />
           <input type="radio" name="hoi5" value="5"  />
           <input type="radio" name="hoi6" value="5"  />
</p>
</form>
<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2" checked />
           <input type="radio" name="hoi3" value="3" checked />
           <input type="radio" name="hoi4" value="4"  />
           <input type="radio" name="hoi5" value="5"  />
           <input type="radio" name="hoi6" value="5"  />
</p>
</form>
<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2" checked />
           <input type="radio" name="hoi3" value="3" checked />
           <input type="radio" name="hoi4" value="4" checked />
           <input type="radio" name="hoi5" value="5"  />
           <input type="radio" name="hoi6" value="5"  />
</p>
</form>
<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2" checked />
           <input type="radio" name="hoi3" value="3" checked />
           <input type="radio" name="hoi4" value="4" checked />
           <input type="radio" name="hoi5" value="5" checked />
           <input type="radio" name="hoi6" value="5"  />
</p>
</form>
<form>
<p>
           <input type="radio" name="hoi1" value="1" checked />
           <input type="radio" name="hoi2" value="2" checked />
           <input type="radio" name="hoi3" value="3" checked />
           <input type="radio" name="hoi4" value="4" checked />
           <input type="radio" name="hoi5" value="5" checked />
           <input type="radio" name="hoi6" value="5" checked />
</p>
</form>

Use different names for the tags. And you can put them inside the form tag.

为标记使用不同的名称。你可以将它们放在表单标签中。

#2


0  

Never mind, fixed it. The name was the problem. It should be unique for every set of radio buttons ;). Tnx for your help Dineth

没关系,修理它。这个名字就是问题所在。对于每组单选按钮,它应该是唯一的;)。 Tnx为您提供帮助Dineth