Используя PHP, я собрал все записи в таблице, и пользователь может выбрать определенные записи из него с помощью флажков. Выбранные записи просматриваются на другой странице. Но проблема заключается в перезагрузке страницы, результаты не отображаются.
Этот код используется для извлечения всех записей из базы данных и использования флажков для отправки
<div class="col-lg-8 card">
<table class="table table-striped table-hover table-bordered datatable">
<thead style="background: #967272; color: black;">
<tr>
<th style="width: 50px;"></th>
<th style="color: #3b3b3b;">Company Name</th>
<th style="color: #3b3b3b;">Service</th>
<th style="color: #3b3b3b;">email</th>
<th style="color: #3b3b3b;">Logo</th>
</tr>
</thead>
<tbody>
<form name="" action="index.php" method="POST">
<?php
$sql="select * from company_details ";
$result = $con->query($sql);
if ($result->num_rows > 0) {
?>
<?php
while($row = $result->fetch_assoc()) {
?>
<tr>
<td>
<center><input type="checkbox" name="check[<?php echo $row['id'];?>]" value="1"></center>
</td>
<td><input type="" name="company_name[]" value="<?php echo $row['company_name'];?>"></label></td>
<td><input type="" name="service[]" value="<?php echo $row['service'];?>"></label></td>
<td><input type="label" name="email[]" value="<?php echo $row['email'];?>"></label></td>
<td><?php echo '<img src="' . $row['image_path4']. '" width="100" height="100">'; ?></td>
</tr>
<?php }} //end of while loop ?>
</div>
<button type="submit" class="btn btn-hg btn-success" name="AddEbooks">Submit</button>
</form>
Этот код показывает выбранные записи
<?php include 'config.php';?>
<?php
session_start();
if(!isset($_POST['check']))
{
?>
<?php
$sql="select * from company_details";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$ID=$row['id'];
/* Get the Book ID and now we have to fetch from $_POST the value from the form */
if (array_key_exists($ID, $_POST["check"])) {
$ischecked=$_POST["check"][$ID];
/* See if this has a value of 1. If it does, it means it has been checked */
if ($ischecked==1) {
?>
<div class="col-lg-6" style="padding-top: 20px;">
<section>
<div class="well" >
<div class="card" >
<div class="row ">
<div class="col-md-4">
<?php echo '<img src="' . $row['image_path4']. '" width="100" height="100">'; ?>
</div>
<div class="col-md-8 px-3">
<div class="card-block px-3">
<h4 class="card-title"><?php echo $row ['company_name']; ?></h4>
<div >
<label>Service Type</label>
<?php echo $row ['service']; ?>
</div>
<div >
<label>email</label>
<?php echo $row ['email']; ?>
</div>
<div >
<label>About</label>
<?php echo substr($row['details'], 0, 300); ?>
</div>
<a href="#" class="btn btn-success">Read More</a>
</div>
</div>
</div>
</div>
</section>
</div>
<?php
}
}
}
}
?>
Храните $_POST["check"]
в одной переменной сеанса и используйте эту переменную сеанса для проверки с помощью array_key_exists
Обновить
<?php
session_start();
include 'config.php';
if(!isset($_SESSION['checked'])){
$_SESSION['checked']=$_POST['check'];
}
$checked = $_SESSION['checked'];
if(!empty($checked))
{
$sql="select * from company_details";
$result = $con->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$ID=$row['id'];
/* Get the Book ID and now we have to fetch from $_POST the value from the form */
if (array_key_exists($ID, $checked)) {
$ischecked=$checked[$ID];
/* See if this has a value of 1. If it does, it means it has been checked */
if ($ischecked==1)
{
?>
<div class="col-lg-6" style="padding-top: 20px;">
<section>
<div class="well">
<div class="card" >
<div class="row ">
<div class="col-md-4">
<?php echo '<img src="' . $row['image_path4']. '" width="100" height="100">'; ?>
</div>
<div class="col-md-8 px-3">
<div class="card-block px-3">
<h4 class="card-title"><?php echo $row ['company_name']; ?></h4>
<div >
<label>Service Type</label>
<?php echo $row ['service']; ?>
</div>
<div >
<label>email</label>
<?php echo $row ['email']; ?>
</div>
<div >
<label>About</label>
<?php echo substr($row['details'], 0, 300); ?>
</div>
<a href="#" class="btn btn-success">Read More</a>
</div>
</div>
</div>
</div>
</section>
</div>
<?php
}
}
}
}
}
?>