我正在做一个具有不同问题类别的测验生成器(类别:英语,数学,编程等),我必须设置每个类别的项目数。
我使用这段代码从tbl_category中检索数据:
SetItemLimit.php
<?php
$con=mysqli_connect("localhost","root","admin","learning_assessment") or die("Cannot connect to database!");
$sql="select * from tbl_category;";
$rs=mysqli_query($con,$sql);
$rows=mysqli_fetch_array($rs);
?>
<html>
<head></head>
<body>
<form action="SetItemLimit.php" method="post">
<table border="1">
<tr>
<th>Category</th>
<th>Number of Items</th>
</tr>
<?php do{?>
<tr>
<td><input type="text" value="<?php echo $rows['category'];?>" readonly="readonly" /></td>
<td><input type="text" name="perCatLimit" /></td>
</tr>
<?php }while($rows=mysqli_fetch_array($rs));?>
</table>
TOTAL NUMBER OF THE TEST ITEMS: <input type="text" name="numQ"/><br />
<input type="submit" name="SetItemLimit" value="SET ITEM LIMIT" />
</form>
</body>
</html>
我想要发生的是,当我为每个类别设置项目数并单击提交按钮(SetItemLimit)时 - 这些值将被添加并显示到文本框(numQ)。
<?php
if(isset($_POST['SetItemLimit'])){
}
我可以使用
array_sum()
答案 0 :(得分:0)
您需要将perCatLimit
设为数组输入perCatLimit[]
。见下文:
<?php
if(isset($_POST['perCatLimit'])){
$setLimit = array_sum($_POST['perCatLimit']);
}
$con = mysqli_connect("localhost","root","admin","learning_assessment") or die("Cannot connect to database!");
$sql = "select * from tbl_category;";
$rs = mysqli_query($con,$sql);
$rows = mysqli_fetch_array($rs); ?>
<html>
<head>
</head>
<body>
<form action="SetItemLimit.php" method="post">
<table border="1">
<tr>
<th>Category</th>
<th>Number of Items</th>
</tr>
<?php do{?>
<tr>
<td><input type="text" value="<?php echo $rows['category'];?>" readonly="readonly" /></td>
<td><input type="text" name="perCatLimit[]" /></td>
</tr>
<?php }while($rows = mysqli_fetch_array($rs));?>
</table>
TOTAL NUMBER OF THE TEST ITEMS:
<input type="text" <?php echo (isset($setLimit) && is_numeric($setLimit))? 'value="'.$setLimit.'"':''; ?> name="numQ"/>
<br />
<input type="submit" name="SetItemLimit" value="SET ITEM LIMIT" />
</form>
</body>
</html>