2017-07-25 96 views
0

我試圖調用爲複選框,從數據庫數據,如果行= 5複選框,然後它去下一行,While循環輸入複選框

我嘗試使用這樣的:

while($row = $result->fetch_assoc()) { 

        $x=0; 
        while ($x < 5) { 

      echo "<input type='checkbox' name='food[]' value ='". $row['price']."'>"; 
      echo $row['name'] . "&nbsp"; 

        $x++; 

         } 
        } 

但事情是,結果成了這個樣子....

the result of the code below

如果你們知道什麼是錯的,請幫助!

這是我的HTML,PHP代碼:

 <!DOCTYPE html> 
    <html> 
    <head> 
     <title></title> 
    </head> 
    <body> 


    <form action="another_sample.php" method="POST"> 

    <?php 
      include "connection.php"; 

      $sql = "SELECT m.type, m.name, m.price, mt.name as 'type' FROM table_menu m LEFT JOIN table_menu_type mt ON m.type = mt.id WHERE m.type LIKE '%1%' "; 
      $result = $con->query($sql); 

      if ($result->num_rows > 0) { 
       // output data of each row 

       while($row = $result->fetch_assoc()) { 

          $x=0; 
          while ($x < 5) { 

        echo "<input type='checkbox' name='food[]' value ='". $row['price']."'>"; 
        echo $row['name'] . "&nbsp"; 

          $x++; 

           } 
          } 
      } 

      else { 
       echo "0 results"; 
      } 

      $con->close(); 

      ?> 
    </form> 

    </body> 
    </html> 

回答

1
echo ='<ul class="checkbox"> 
    <li><input type='checkbox' name='food[]' value ='". $row['price']."'></li> 
</ul>'; 

CSS:

ul.checkbox { 
    margin: 0; 
    padding: 0; 
    margin-left: 20px; 
    list-style: none; 
} 

ul.checkbox li input { 
    margin-right: .25em; 
} 

ul.checkbox li { 
    border: 1px transparent solid; 
    display:inline-block; 
    width:12em; 
} 

ul.checkbox li label { 
    margin-left: ; 
} 
ul.checkbox li:hover, 
ul.checkbox li.focus { 
    background-color: lightyellow; 
    border: 1px gray solid; 
    width: 12em; 
} 
0

試試下面的代碼:

$x = 1; 
while($row = $result->fetch_assoc()) { 

     echo "<input type='checkbox' name='food[]' value ='". $row['price']."'>"; 
     echo $row['name'] . "&nbsp"; 

     $x++; 
     if($x > 5){ 
      echo "<br/>"; 
      $x = 1; 
     } 
} 
+0

謝謝!它的工作原理,但我的$ i = 1;是一個錯字,它應該是x對嗎?它在我做到這一點時起作用。謝謝! –

+0

是的,請看更新的答案。 –

+0

樂於幫助,並歡迎Stack Overflow。如果此答案或任何其他人解決了您的問題,請將其標記爲已接受, –

0

嘗試:

while ($row = $result->fetch_assoc()) { 
    $x=0; 
    while ($x < 5) { 
     echo "<input type='checkbox' name='food[]' value ='". $row['price']."'>"; 
     echo $row['name'] . "&nbsp"; 

     $x++; 
    } 

    echo nl2br ("\n"); 
} 
0
<!DOCTYPE html> 
    <html> 
    <head> 
     <title></title> 
    </head> 
    <body> 


    <form action="another_sample.php" method="POST"> 

    <?php 


      include "connection.php"; 

      $sql = "SELECT m.type, m.name, m.price, mt.name as 'type' FROM table_menu m LEFT JOIN table_menu_type mt ON m.type = mt.id WHERE m.type LIKE '%1%' "; 
      $result = $con->query($sql); 

      if ($result->num_rows > 0) { 
       // output data of each row 
       $x = 1; 
       while($row = $result->fetch_assoc()) { 



        echo "<input type='checkbox' name='food[]' value ='". $row['price']."'>"; 
        echo $row['name'] . "&nbsp"; 
         $x++; 
          if($x > 5){ 
           echo "<br/>"; 
           $x = 1; 
           }         
         } 
      } 

      else { 
       echo "0 results"; 
      } 

      $con->close(); 

      ?> 
    </form> 

    </body> 
    </html>