【问题标题】:How to check all radio buttons based on database values?如何根据数据库值检查所有单选按钮?
【发布时间】:2016-05-08 22:15:47
【问题描述】:

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

    <?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 } ?>

这是它现在的样子:

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

【问题讨论】:

    标签: mysql radio-button


    【解决方案1】:

    我没有正确理解你的问题???

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

    <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>

    为标签使用不同的名称。你可以把它们放在表单标签里面。

    【讨论】:

    • 嗨,Dieth,感谢您的回答。但是,我想要实现的是根据已经存储在数据库中的值检查所有单选按钮。例如,“sds”选择了单选按钮 3,因此值 3 现在记录在数据库中。但是你可以看到“ads”中的第三个单选按钮现在没有被选中,只有“dsadh”的单选按钮被选中。我现在的问题是如何检查所有这些单选按钮(因为所有值都在数据库中)?现在唯一被检查的是最后一个。
    【解决方案2】:

    没关系,修好了。名字是个问题。对于每组单选按钮,它应该是唯一的;)。 Tnx 为您提供帮助,Dieth

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 2013-03-19
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      • 2022-10-17
      • 2017-12-03
      • 1970-01-01
      相关资源
      最近更新 更多