我想打印清单数组值,这里为Food表的每一行动态创建清单。我怎么能这样做?如果我点击提交没有提交任何值我猜是这样的,如何使用提交按钮?实际上在哪里使用..plz帮帮我
<?php // File: anyco.php
require('anyco_ui.inc.php');
// Create a database connection
$conn = oci_connect('system','123','localhost/orcl');
ui_print_header('FoodItemList');
//session_start();
//$cid=$_SESSION['cid'];
//do_query($conn, 'SELECT Fooditem_ID,Food_item_name,price,day_available,time_available,discount_percentage,start_date,deadline FROM Food_Item');
ui_print_footer(date('Y-m-d H:i:s'));
function do_query($conn, $query)
{
$stid = oci_parse($conn, $query);
$r = oci_execute($stid,OCI_DEFAULT);
print '<table border="1">';
print '<tr>';
print '<td>Food_ID<td>Food_Name<td>Price(tk)<td>Dvailable_day<td>Avaliable_time<td>Discount<td>Dis_start date<td>Dis_finish date<td>selected item<td>quanity';
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
print '<tr>';
$num=1;
$val="";
foreach ($row as $item)
{
if($num==1)
{
$val = $item;
$num=2;
}
print '<td>'.($item!== null ? htmlentities($item) : ' ').'</td>';
}
//echo$val;
echo '<td><input type="checkbox" name = "invite[]" value="$val>" </td>';
echo '<td><input type="number" name = "name[]" ></td>';
print '</tr>';
}
print '</table>';
}
if(isset($_POST['submit']))
{
if (is_array($_POST['invite']))
{
foreach($_POST['invite'] as $key=>$name)
{
echo $key, '=>', $name,'<br/>';
//Here $key is the array index and $name is the value of the checkbox
}
}
}
?>
<html>
<style>
body
{
background:orange;
}
</style>
<body text="green">
<br><br>
<form method="post">
<?php do_query($conn, 'SELECT Fooditem_ID,Food_item_name,price,day_available,time_available,discount_percentage,start_date,deadline FROM Food_Item'); ?>
<input type ="submit" value="submit" name="submit"><br><br>
</form>
</body>
</html>
答案 0 :(得分:0)
您的表单内部没有任何输入,因为您在打印表单标记之前调用了函数方式。
此行需要移至以下格式:
do_query($conn, 'SELECT Fooditem_ID,Food_item_name,price,day_available,time_available,discount_percentage,start_date,deadline FROM Food_Item');
所以代码看起来像这样,整个表及其输入在表单标签内打印。我还为提交按钮添加了一个名称:
<form method="post">
<?php do_query($conn, 'SELECT Fooditem_ID,Food_item_name,price,day_available,time_available,discount_percentage,start_date,deadline FROM Food_Item'); ?>
<input type ="submit" value="submit" name="submit"><br><br>
</form>
最后,当您在输入中回显值时,此行中有两个语法错误:
echo '<td><input type="checkbox" name = "invite[]" value="$val>" </td>';
您可以通过多种方式将变量回显到字符串中 - 这里有两个。 1)如果变量在单引号之间,PHP将自动将变量转换为其值。在这种情况下,它是双引号,所以不要去。 2)用单引号启动echo命令,这样你就可以用单引号中断字符串,用一个句点连接变量,然后用另一个单引号重新打入字符串,如下所示:
echo '<td><input type="checkbox" name = "invite[]" value="' . $val . '"> </td>';