我编写的代码,只需在点击“提交”按钮时在同一页面上打印所选书籍的Book_id。屏幕上显示多本书籍,并且可以选择在屏幕上一次查看最多100本书籍。但是,我不希望看到超过10个。现在当我选择3,9和7都显示在页面上时,它们似乎在选中复选框时显示。 但如果我选择1,3,1001,243。该页面中最后选择的值即。 243打印在屏幕上。这是我的代码
<form action="more_book_issue.php" method="post">
<input type="submit" name="submit" Value="Submit"/>
<?php
include 'connect_db.php';
include 'login_check.php';
include 'check_box_value.php';
$sql = "select * from Books where id NOT IN (select Book_Id from Issued)";
$result = mysqli_query($connection,$sql);
echo '<table class="table table-hover table-striped" id = "issue-table">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Last Name</th>
<th>Genre</th>
<th>Other</th>
<th>Rate</th>
<th>Category</th>
<th>Issue</th>
<th></th>
</tr>
</thead>
<tbody>';
while($row = mysqli_fetch_array($result)){
echo "<tr>
<th>$row[Id]</th>
<td>$row[Title]</td>
<td>$row[Lastname]</td>
<td>$row[Genre]</td>
<td>$row[Other]</td>
<td>$row[Rate]</td>
<td>$row[Category]</td>
<td>"
?>
<input type="checkbox" name="addToCart[]" value="<?php echo $row['Id']; ?>"/>
<?php
"</td>
</tr>";
}
?>
</form>
<?php
和check_box_value.php就在这里
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['addToCart'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['addToCart']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach($_POST['addToCart'] as $selected) {
echo "<p>".$selected ."</p>";
}
echo "<br/><b>Note :</b> <span>Similarily, You Can Also Perform CRUD Operations using These Selected Values.</span>";
}
else{
echo "<b>Please Select Atleast One Option.</b>";
}
}
?>
答案 0 :(得分:0)
如果我理解你的问题,你只需要在$ checked_count = count之前执行以下代码($ _ POST ['addToCart']); :
if (sizeof($_POST['addToCart']) > 10) {
$_POST['addToCart'] = array_slice($_POST['addToCart'], 0, 10);
}