我有一个带有下面代码的脚本和一个分页,用户可以在其中检查复选框以缩小结果显示一切似乎工作得很好唯一的问题是当用户点击分页中的下一页时结果丢失并显示默认结果。我想我可能需要使用会话来存储查询或语句,但我不确定如何使用它们。如果有人能帮助我或指出我正确的方向,我会非常感激。
这是我的代码
<?php
//get the function
include ($_SERVER['DOCUMENT_ROOT'] .'/scripts/function1.php');
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 14;
$startpoint = ($page * $limit) - $limit;
if(isset($_POST['muscle'])) {
// Runs mysql_real_escape_string() on every value encountered.
$clean_muscle = array_map('mysql_real_escape_string', $_REQUEST['muscle']);
// Convert the array into a string.
$muscle = implode("','", $clean_muscle);
if (!empty($_POST['muscle'])) {
$options[] = "muscle IN ('$muscle')";
} else {
$options[] = "";
}
} else {
$options[] = "allmuscle = 'all' ";
}
if(isset($_POST['equipment'])) {
// Runs mysql_real_escape_string() on every value encountered.
$clean_equipment = array_map('mysql_real_escape_string', $_REQUEST['equipment']);
// Convert the array into a string.
$equipment = implode("','", $clean_equipment);
if (!empty($_POST['equipment'])) {
$options[] = "equipment IN ('$equipment')";
} else {
$options[] = "";
}
} else {
$options[] = "allequipment = 'all'";
}
if(isset($_POST['all'])) {
$options[] = "";
}
$fullsearch = implode(' AND ', $options);
$statement = "exercise ";
if ($fullsearch <> '') {
$statement .= " WHERE " . $fullsearch;
} else {
$statement .= "";
}
if(!$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}"))
{
echo "Cannot parse query";
}
elseif(mysql_num_rows($query) == 0) {
echo "No records found";
}
else {
while($row = mysql_fetch_assoc($query)) {
echo "".$row['name'] ."<br />
".$row['description'] ."";
}
}
echo "<div class=\"new-pagination\">";
echo pagination($statement,$limit,$page);
echo "</div>";
?>
这是分页脚本
<?php
function pagination($query, $per_page = 10, $page = 1, $url = '?'){
$query = "SELECT COUNT(*) as `num` FROM {$query}";
$row = mysql_fetch_array(mysql_query($query));
$total = $row['num'];
$adjacents = "1";
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
$firstPage = 1;
$prev = ($page == 1)?1:$page - 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total/$per_page);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<ul class=\"pagination1\">";
if ($page == 1)
{
$pagination.= "<li><a class=\"inactive\">First</a></li>";
$pagination.= "<li><a class=\"inactive\">Prev</a></li>";
}
else
{
$pagination.= "<li><a href=\"{$url}page=$firstPage\" >First</a></li>";
$pagination.= "<li><a href=\"{$url}page=$prev\" >Prev</a></li>";
}
$pagination .= "<li class=\"details\">Page $page of $lastpage</li>";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class=\"current\">$counter</a></li>";
else
$pagination.= "<li><a href=\"{$url}page=$counter\" >$counter</a></li>";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class=\"current\">$counter</a></li>";
else
$pagination.= "<li><a href=\"{$url}page=$counter\" >$counter</a></li>";
}
$pagination.= "<li class=\"dot\">... </li>";
$pagination.= "<li><a href=\"{$url}page=$lpm1\" >$lpm1</a></li>";
$pagination.= "<li><a href=\"{$url}page=$lastpage\" >$lastpage</a></li>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<li><a href=\"{$url}page=1\" >1</a></li>";
$pagination.= "<li><a href=\"{$url}page=2\" >2</a></li>";
$pagination.= "<li class=\"dot\">...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class=\"current\">$counter</a></li>";
else
$pagination.= "<li><a href=\"{$url}page=$counter\" >$counter</a></li>";
}
$pagination.= "<li class=\"dot\">..</li>";
$pagination.= "<li><a href=\"{$url}page=$lpm1\" >$lpm1</a></li>";
$pagination.= "<li><a href=\"{$url}page=$lastpage\" >$lastpage</a></li>";
}
else
{
$pagination.= "<li><a href=\"{$url}page=1\" >1</a></li>";
$pagination.= "<li><a href=\"{$url}page=2\" >2</a></li>";
$pagination.= "<li class=\"dot\">..</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class=\"current\">$counter</a></li>";
else
$pagination.= "<li><a href=\"{$url}page=$counter\" >$counter</a></li>";
}
}
}
if ($page < $counter - 1){
$pagination.= "<li><a href=\"{$url}page=$next\">Next</a></li>";
$pagination.= "<li><a href=\"{$url}page=$lastpage\" >Last</a></li>";
}else{
$pagination.= "<li><a class=\"inactive\">Next</a></li>";
$pagination.= "<li><a class=\"inactive\">Last</a></li>";
}
$pagination.= "</ul>\n";
}
return $pagination;
}
?>
这是我的复选框
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="criteria">
<ul>
<li class="head">
<input type="checkbox" name="allmuscle[1]" class="remember_cb" id="allMuscles" value="selectedMuscles" >All Muscle Groups<br /></li>
<li><input type="checkbox" name="muscle[2]" class="remember_cb" id="abdominals" value="abdominals"/>Abdominals<br /></li>
<li><input type="checkbox" name="muscle[3]" class="remember_cb" id="biceps" value="biceps"/>Biceps<br /></li>
<li><input type="checkbox" name="muscle[4]" class="remember_cb" id="calves" value="calves"/>Calves<br /></li>
<li><input type="checkbox" name="muscle[5]" class="remember_cb" id="chest" value="chest" />Chest<br /></li>
<li><input type="checkbox" name="muscle[6]" class="remember_cb" id="forearms" value="forearms"/>Foreamrs<br /></li>
<li><input type="checkbox" name="muscle[7]" class="remember_cb" id="glutes" value="glutes"/>Glutes<br /></li>
<li><input type="checkbox" name="muscle[8]" class="remember_cb" id="hamstrings" value="hamstrings"/>Hamstrings<br /></li>
<li><input type="checkbox" name="muscle[9]" class="remember_cb" id="lats" value="lats"/>Lats<br /></li>
<li><input type="checkbox" name="muscle[10]" class="remember_cb" id="lower-back" value="lower-back"/>Lower Back<br /></li>
<li><input type="checkbox" name="muscle[11]" class="remember_cb" id="middle-back" value="middle-back" />Middle Back<br /></li>
<li><input type="checkbox" name="muscle[12]" class="remember_cb" id="neck" value="neck"/>Neck<br /></li>
<li><input type="checkbox" name="muscle[13]" class="remember_cb" id="quadriceps" value="quadriceps" />Quadriceps<br /></li>
<li><input type="checkbox" name="muscle[14]" class="remember_cb" id="shoulders" value="shoulders" />Shoulders<br /></li>
<li><input type="checkbox" name="muscle[15]" class="remember_cb" id="traps" value="traps" />Traps<br /></li>
<li><input type="checkbox" name="muscle[16]" class="remember_cb" id="triceps" value="triceps"/>Triceps<br /></li>
</ul>
<br /><p><strong>EQUIPMENT</strong></p>
<ul>
<li class="head"><input type="checkbox" name="allequip[]" class="remember_cb" id="allEquipment" value="selectedEquipment" >All Equipment<br /></li>
<li><input type="checkbox" name="equipment[1]" class="remember_cb" id="equipment" value="bands"/>Bands<br /></li>
<li><input type="checkbox" name="equipment[2]" class="remember_cb" id="equipment" value="barbell" />Barbell<br /></li>
<li><input type="checkbox" name="equipment[3]" class="remember_cb" id="equipment" value="body-only" />Body Only<br /></li>
<li><input type="checkbox" name="equipment[4]" class="remember_cb" id="equipment" value="cable" />Cable<br /></li>
<li><input type="checkbox" name="equipment[5]" class="remember_cb" id="equipment" value="dumbbell" />Dumbbell<br /></li>
<li><input type="checkbox" name="equipment[6]" class="remember_cb" id="equipment" value="ez-curl-bar"/>EZ Curl Bar<br /></li>
<li><input type="checkbox" name="equipment[7]" class="remember_cb" id="equipment" value="exercise-ball"/>Exercise Ball<br /></li>
<li><input type="checkbox" name="equipment[8]" class="remember_cb" id="equipment" value="foam-roll" />Foam Roll<br /></li>
<li><input type="checkbox" name="equipment[9]" class="remember_cb" id="equipment" value="kettlebell" />Kettlebell<br /></li>
<li><input type="checkbox" name="equipment[10]" class="remember_cb" id="equipment" value="machine" />Machine<br /></li>
<li><input type="checkbox" name="equipment[11]" class="remember_cb" id="equipment" value="medicine-ball"/>Medicine Ball<br /></li>
<li><input type="checkbox" name="equipment[12]" class="remember_cb" id="equipment" value="none" />None<br /></li>
<li><input type="checkbox" name="equipment[13]" class="remember_cb" id="equipment" value="other" />Other<br /></li>
该脚本一切正常我唯一的问题是,一旦复选框被选中,结果将被返回,但只有在您单击下一页或第2页时结果或查询丢失时才会保留在第一页上。
答案 0 :(得分:0)
对于会话,只需将搜索参数存储到$_SESSION
数组。当请求来自初始搜索表单时,不要忘记覆盖存储的参数。
$_SESSION['muscle'] = ...
不使用用户会话的另一种方法是在结果列表中重新发布搜索参数。您可以将它们放在隐藏的表单元素中,也可以手动添加到页面前进/后退链接的URL:
"<form action=\"...\" id=\"page_prev\">".
"<input type=\"hidden\" name=\"muscle\" value=\"$_POST['muscle']\" .
"<input type=\"hidden\" name=\"page\" value=\"$prev\";
或
"<a href=\"{$url}page=$prev&muscle=$_POST['muscle']\"
对于表单变体,您还需要一些JS代码来提交表单作为链接点击:
<a href="#" onclick="document.getElementById('page_prev').submit()">Prev</a>;
答案 1 :(得分:0)
因为你想用会话来做..这就是你可以做到的方式
//Your first part of the code
<?php
session_start(); // this should be in the beginning of your page
//get the function
include ($_SERVER['DOCUMENT_ROOT'] .'/scripts/function1.php');
.
.. //code in between
...
if(isset($_POST['muscle'])) {
// Runs mysql_real_escape_string() on every value encountered.
$clean_muscle = array_map('mysql_real_escape_string', $_REQUEST['muscle']);
//since you had a post request you can save it in session now
// you can either save it as an array or string depending on what exactly you want to do with it.
// if you want to do more complex work like db quering save ie in array like this
foreach($clean_muscle as $cm){
$_SESSION["muscle"][] = $cm;
}
// or if you just want to display the clean_muscle string do it this way after your implode
$muscle = implode("','", $clean_muscle);
$_SESSION["muscle"] = $muscle;
现在无论何时加载页面,您都必须检查会话是否已设置
//check if session is set
if(isset($_SESSION["muscle"]) && !empty($_SESSION["muscle"])){
// do whatever you want to do with your variable
}
对您的设备使用相同的过程。就个人而言,这整个代码可以用更好的方式编写..但它是你的选择和你的代码..希望这会有所帮助。
DINS