我有一个搜索页面,并且已设置分页,但是每次我刷新搜索变量时,其设置将恢复为默认值...如果我尝试导航到分页中,它也会恢复为默认值...所以我想知道如何保持搜索变量的值...
搜索表单在另一页中。
我已经尝试过使用session_start()方法,但是它似乎并没有保留搜索变量...
<?php require_once("../includes/initialize.php"); ?>
<?php
session_start();
if(isset($_POST['submit'])){
// 1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
// 2. records per page ($per_page)
$per_page = 2;
// 3. total record count ($total_count)
$_SESSION['search'] = $_POST['search'];
echo $_SESSION['search'];
$search = $database->escape_value($_SESSION['search']);
$sql = "SELECT COUNT(*) FROM products WHERE image LIKE '%$search%'";
$sql .= " OR title LIKE '%$search%'";
$sql .= " OR slug LIKE '%$search%'";
$sql .= " OR description LIKE '%$search%'";
$sql .= " OR price LIKE '%$search%'";
$sql = $database->query($sql);
$sql = $database->fetch_array($sql);
$total_count = array_shift($sql);
// Find all photos
// use pagination instead
$pagination = new Pagination($page, $per_page, $total_count);
// Instead of finding all records, just find the records
$sql = "SELECT * FROM products WHERE image LIKE '%$search%'";
$sql .= " OR title LIKE '%$search%'";
$sql .= " OR slug LIKE '%$search%'";
$sql .= " OR description LIKE '%$search%'";
$sql .= " OR price LIKE '%$search%'";
$sql .= " ORDER by id DESC";
$sql .= " LIMIT {$per_page}";
$sql .= " OFFSET {$pagination->offset()}";
$photos = Product::find_by_sql($sql);
$tcount = count($photos);
}
?>
<?php include_layout_template('header2.php'); ?>
<div class="container">
<P class="lead error-msg">There are <?php echo $tcount; ?> results out of <?php echo " " . $total_count; ?></P>
<div class="row">
<div id="pagination" style="clear: both;">
<nav aria-label="Page navigation example">
<ul class="pagination">
<?php
for($i=1; $i <= $pagination->total_pages(); $i++){
if($i == $page) {
echo "<li class='page-item'><a class='page-link'> <span class=\"selected\">{$i}</span></a></li> ";
} else{
echo " <li class='page-item'><a class='page-link' href=\"search.php?page={$i}\">{$i}</a></li> ";
}
}
if($pagination->total_pages() > 1) {
if($pagination->has_previous_page()) {
echo "<li class='page-item'><a class='page-link' href=\"search.php?page=";
echo $pagination->previous_page();
echo "\">« Previous</a></li> ";
}
if($pagination->has_next_page()){
echo "<li class='page-item'><a class='page-link' href=\"search.php?page=";
echo $pagination->next_page();
echo "\">Next »</a></li>";
}
}
?>
</ul>
</nav>
</div>
</div>
</div>
<div class="container">
<div class="row">
<?php foreach ($photos as $photo): ?>
<div class="col-md-4 col-sm-6">
<div class="thumbnail">
<form method="post" action="cart.php?action=add&id=<?php echo $photo->id; ?>" role="form" class="form-vertical">
<a href="order_review.php?id=<?php echo $photo->id; ?>"><img src="<?php echo $photo->image_path();
?>" class="img-thumbnail" alt="responsive image"></a>
<div class="caption">
<h3 class="text-info text-center"><?php echo $photo->title; ?></h3>
<p class="text-muted text-center price card-header"><span class="currency">N</span><?php echo $photo->price; ?></p>
<p class="text-center"><em><?php echo $photo->description; ?></em></p>
<input type="hidden" name="id" class="form-control" value="<?php echo $photo->id; ?>" />
<input type="hidden" name="title" class="form-control" value="<?php echo $photo->title; ?>" />
<input type="hidden" name="slug" class="form-control" value="<?php echo $photo->slug; ?>" />
<input type="hidden" name="description" class="form-control" value="<?php echo $photo->description; ?>" />
<input type="hidden" name="price" class="form-control" value="<?php echo $photo->price; ?>"/>
<div class="col-sm-4 mx-auto d-block">
<select name="quantity" class="form-control" name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
<div class="col-sm-4 col-sm-push-3 mx-auto d-block">
<input type="submit" name="add_to_cart" class="btn bg-success" value="Add to cart" />
</div><br/>
</form>
</div>
</div>
<?php endforeach;
?>
</div>
</div>
<?php include_layout_template('footer2.php'); ?>
答案 0 :(得分:0)
尝试调用session_start();在任何php代码的开头。如果这不起作用,请创建新页面进行测试,是否有任何会话在起作用。也许配置不正确。
<?php
session_start();
$_SESSION['foo'] = "test";
var_dump($_SESSION);
答案 1 :(得分:0)
<?php
//var_dump($_POST);
//var_dump($_SESSION);
if(isset($_POST['submit']) || isset($_SESSION['search'])){
// 1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
// 2. records per page ($per_page)
$per_page = 2;
// 3. total record count ($total_count)
if(isset($_POST['submit'])) {
$_SESSION['search'] = $_POST['search'];
$searchvalue = $_POST['search'];
} else {
isset($_SESSION['search']);
$searchvalue = $_SESSION['search'];
}
$search = $database->escape_value($searchvalue);
$sql = "SELECT COUNT(*) FROM products WHERE image LIKE '%$search%'";
$sql .= " OR title LIKE '%$search%'";
$sql .= " OR slug LIKE '%$search%'";
$sql .= " OR description LIKE '%$search%'";
$sql .= " OR price LIKE '%$search%'";
$sql = $database->query($sql);
$sql = $database->fetch_array($sql);
$total_count = array_shift($sql);
// Find all photos
// use pagination instead
$pagination = new Pagination($page, $per_page, $total_count);
// Instead of finding all records, just find the records
$sql = "SELECT * FROM products WHERE image LIKE '%$search%'";
$sql .= " OR title LIKE '%$search%'";
$sql .= " OR slug LIKE '%$search%'";
$sql .= " OR description LIKE '%$search%'";
$sql .= " OR price LIKE '%$search%'";
$sql .= " ORDER by id DESC";
$sql .= " LIMIT {$per_page}";
$sql .= " OFFSET {$pagination->offset()}";
$photos = Product::find_by_sql($sql);
$tcount = count($photos);
}
// var_dump($_SESSION);
?>