2017-07-25 115 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>