我有一个带有下面代码的脚本和一个分页,用户可以在其中选中复选框以缩小显示的结果,一切似乎都很好,唯一的问题是当用户单击分页中的下一页时,结果会丢失并且默认结果显示。我想我可能需要使用会话来存储查询或语句,但我不确定如何使用它们。如果有人可以帮助我或指出我正确的方向,我将非常感激。
这是我的代码
<?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 页时才会停留在第一页上,结果或查询将丢失。