2016-02-26 81 views
0

我目前有一个麻烦使用单选按钮,我因为我的代码输出变得混乱。在左边的图片中,我的输出就是这样,在正确的图片中我想要我的输出。当我从候选人中选择时,单选按钮可以选择而不是只有一个应该可以选择。如何把单选按钮与php值

这里是我的代码:

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


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

        - <?php 
          echo $rows['party_name']?> 
         <?php 

       } 

      ?> 

An example image

+1

在收音机输入上设置name属性,所有对讲机都一样,你只能选择1. – Matt

回答

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。提交后,您只能在submit.php处理程序中看到所选候选人的id

现在添加一些CSS:

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

.box .image { 
    padding: 15px; 
} 

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

希望这有助于。

+0

谢谢我得到了这个:) – miming

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']; 


       } 

      ?>