我试图调用为复选框,从数据库数据,如果行= 5复选框,然后它去下一行,While循环输入复选框
我尝试使用这样的:
while($row = $result->fetch_assoc()) {
$x=0;
while ($x < 5) {
echo "<input type='checkbox' name='food[]' value ='". $row['price']."'>";
echo $row['name'] . " ";
$x++;
}
}
但事情是,结果成了这个样子....
如果你们知道什么是错的,请帮助!
这是我的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'] . " ";
$x++;
}
}
}
else {
echo "0 results";
}
$con->close();
?>
</form>
</body>
</html>
谢谢!它的工作原理,但我的$ i = 1;是一个错字,它应该是x对吗?它在我做到这一点时起作用。谢谢! –
是的,请看更新的答案。 –
乐于帮助,并欢迎Stack Overflow。如果此答案或任何其他人解决了您的问题,请将其标记为已接受, –