0

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

这是我的代码

       <?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 页时才会停留在第一页上,结果或查询将丢失。

4

2 回答 2

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>;
于 2013-04-20T23:07:12.270 回答
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  
   }

对您的设备使用相同的过程。就个人而言,这整个代码可以用更好的方式编写..但它是您的选择和您的代码..希望这会有所帮助。

丁斯

于 2013-04-20T23:50:25.247 回答