如何使用php值放置单选按钮

时间:2016-02-26 02:46:37

标签: php html

我目前在使用单选按钮时遇到问题,因为我的代码输出而导致混淆。在左边的图片中,我的输出是什么样的,在右边的图片中我想要我的输出。当我从候选人中选择时,两个单选按钮都可以选择,而不是只有一个可以选择。

这是我的代码:

import urllib.request
with urllib.request.urlopen("http://www.powerball.com/powerball/winnums-text.txt") as file:
    next(file)
    for bline in file:
        line = str(bline, "utf-8")
        print(line)

An example image

2 个答案:

答案 0 :(得分:0)

尝试回显这样的单选按钮。

 <?php
 $YearNow=Date('Y');
 $dsds=$rowasa['posid'];
 $results = $db->prepare("SELECT * FROM candidates,student,school_year,partylist where student.idno = candidates.idno AND school_year.syearid = candidates.syearid AND posid =:a AND candidates.partyid = partylist.partyid AND  school_year.from_year like $YearNow  ");

                $results->bindParam(':a', $dsds);
                $results->execute();
                for($i=0; $rows = $results->fetch(); $i++){

        //here's the part that i was confuse    


    echo "<input type ='radio'><input style='padding: 35px 50px 35px 80px; background:url('admin/candidates/images/". $rows['image'] . "')  no-repeat scroll 5px 7px / 70px auto rgba(0, 0, 0, 0);  
    value='" . $rows['candid'] . " - "$rows['lastname'] ", ". $rows['firstname'] . "'>" . $rows['lastname'] . "," . $rows['firstname'];
echo $rows['firstname'] ." - ". $rows['party_name'];


                }

            ?>

答案 1 :(得分:0)

看起来你的SQL查询给你正确的结果,但我真的会考虑使用JOINs。无论如何,我假设你的SQL结果是这样的:

$candidates = array(
    array(
        "id" => "1",
        "firstname" => "John",
        "image" => "some/image/path",
        "party_name" => "Party1",
    ),
    array(
        "id" => "2",
        "firstname" => "Jane",
        "image" => "some/image/path",
        "party_name" => "Party2",
    )
);

要遍历这个并构建HTML,使用foreach会更容易:

<form method="post" action="submit.php">
    <?php
    foreach ($candidates as $candidate) {
        ?>
        <div class="box">
            <div class="image">
                <img src="admin/candidates/images/<?php echo $candidate['image']; ?>" alt="">
            </div>
            <div class="input">
                <input type="radio" name="candidate_selected" value="<?= $candidate['id'] ?>">
            </div>
            <div class="text">
                <?php echo $candidate['firstname'] . " - " . $candidate['party_name'] ?>
            </div>
        </div>
        <?php
    }
    ?>
    <input type="submit">
</form>

请注意input如何与候选人结果具有相同的名称和id。提交后,您应该只在id处理程序中看到所选候选人的submit.php

现在添加一些CSS:

.box {
    display: inline-block;
    text-align: center;
}

.box .image {
    padding: 15px;
}

.box .image img {
    width: 150px;
    height: 150px;
    display: block;
}

希望这有帮助。