循环输入复选框

时间:2017-07-25 06:27:15

标签: php checkbox while-loop

我正在尝试从数据库中调用数据作为复选框,如果row = 5复选框,则它会进入下一行,

我试过用这个:

    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>

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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;
      }
}

答案 2 :(得分: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");
}

答案 3 :(得分: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>