分页动态SQL结果

时间:2017-11-08 10:09:28

标签: php jquery html sql pagination

我有一个过滤SQL结果的多个复选框。我将期待有很多记录返回,所以我想分页。我目前的分页代码仅适用于像SELECT * FROM movies LIMIT {$startpoint} , {$per_page}这样的简单SQL语句。问题是我无法使用我当前的jquery过滤器来处理我的SQL结果。

分页功能

   // Pagination Function
   function pagination($query,$per_page=10,$page=1,$url='?'){   
global $conDB; 
$query = "SELECT COUNT(*) as `num` FROM movies";
$row = mysqli_fetch_array(mysqli_query($conDB,$query));
$total = $row['num'];
$adjacents = "2"; 

$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$lastlabel = "Last ››";

$page = ($page == 0 ? 1 : $page);  
$start = ($page - 1) * $per_page;                               

$prev = $page - 1;                          
$next = $page + 1;

$lastpage = round($total/$per_page);

$lpm1 = $lastpage - 1; // //last page minus 1

$pagination = "";
if($lastpage > 1){   
    $pagination .= "<ul class='pagination'>";


        if ($page > 1) $pagination.= "<li><a href='{$url}page={$prev}'>
  {$prevlabel}</a></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}'>{$nextlabel}</a>
     </li>";
    $pagination.= "<li><a href='{$url}page=$lastpage'>{$lastlabel}</a>
      </li>";
      }

    $pagination.= "</ul>";        
       }

return $pagination;
}//

Latest.php

 $page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
 if ($page <= 0) $page = 1;

 $per_page = 2; // Set how many records do you want to display per page.

$startpoint = ($page * $per_page) - $per_page;

$statement = "`movies` ORDER BY `m_id` ASC";  

            $con = mysqli_connect("localhost","root","","blabla");
       $mysqli = new mysqli("localhost", "root", "", "blabla");


         $db_username = 'root'; 
         $db_password = ''; 
         $db_name = 'blabla'; .
         $db_host = 'localhost';

         $conDB = mysqli_connect($db_host, $db_username, 
          $db_password,$db_name)or die('Error: Could not connect to
          database.');


         if (mysqli_connect_errno())
        {

         echo "Failed to Connect to MySQL:" . mysqli_connect_error();
         }


        $results2 = $mysqli->query( "select distinct(m_genre) 
        from movies where movie_status = '1'");  

         while($genre_data = mysqli_fetch_assoc($results2)){

            ?>

                <a href="javascript:void(0);" class="list-group-item"> 
                <label> 
                <input type="checkbox" class="item_filter genre" 
            value="<?php echo $genre_data['m_genre']; ?>" <?php
            if(in_array($genre_data['m_genre'],$genre)){ echo"checked";
             } ?> >
         <span class="check-box-effect"></span>
                &nbsp;&nbsp; <?php echo $genre_data['m_genre']; ?>
       </label></a>

            <?php } ?>  
            </div>



            <div class="col-xs-4">
            <h3>Country</h3>
            <?php 
                $rs = $mysqli->query("select distinct(m_country) 
            from movies where movie_status = '1'");  

                while($country_data = mysqli_fetch_assoc($rs)){

            ?>
                <a href="javascript:void(0);" class="list-group-item"> 
                <input type="checkbox" class="item_filter country"
        value="<?php echo $country_data['m_country']; ?>" <?php 
        if(in_array($country_data['m_country'],$country)){ 
        echo"checked"; } ?> >
                &nbsp;&nbsp; <?php echo $country_data['m_country']; 
             ?>     </a>
            <?php } ?>  
            </div>

            <div class="col-xs-4">
            <h3>Year</h3>
            <?php 
                $rs = $mysqli->query( "select distinct(m_year) 
               from movies where movie_status = '1'");  

                while($year_data = mysqli_fetch_assoc($rs)){

            ?>
                <a href="javascript:void(0);" class="list-group-item"> 
                <input type="checkbox" class="item_filter year" 
                value="     <?php echo $year_data['m_year']; ?>"
              <?php if(in_array($year_data['m_year'],$year))
               { echo"checked"; } ?> >
                &nbsp;&nbsp; <?php echo $year_data['m_year']; ?></a>
            <?php } ?>  
            </div>

            </div>
          </div>
          </div>



            <div class="row">
               <div class="col-xs-12">
              <?php 


                 $query = "SELECT * FROM movies 
          WHERE movie_status = '1'"; 
                  //filter query start 
                  if(!empty($genre)){
                      $genredata =implode("','",$genre);
                      $query  .= " and m_genre in('$genredata') LIMIT {$startpoint} , {$per_page} "; 
                  }

                   if(!empty($country)){
                      $countrydata =implode("','",$country);
                      $query  .= " and m_country in('$countrydata') LIMIT {$startpoint} , {$per_page} "; 
                  }

                  if(!empty($year)){
                      $yeardata =implode("','",$year);
                      $query  .= " and m_year in('$yeardata') LIMIT 
            {$startpoint} , {$per_page} "; 
                  }
                //filter query end 

                 $rs = mysqli_query($mysqli,$query) or die("Error : 
   ".mysqli_error($mysqli));
  while(){

     ............................
            }
   echo '<div class= "wrap">';
   // displaying paginaiton.

   echo pagination($statement,$per_page,$page,$url='?');

   echo "</div>";

当前代码并不是Limit结果和displays all results,如果checkbox is checked,过滤器有效,但分页不是i.e no limit and link to next page gives the same result as the first。是否有更简单的方法来分页我的过滤结果或如何修复我当前的代码。我对新的分页解决方案持开放态度。

2 个答案:

答案 0 :(得分:1)

最明显的问题是您的过滤器永远不会被应用,因为$ gendre,$ country和$ year永远不会传递给该函数。

假设您从请求对象中正确读取了这些值,请查看HTML中的标记,您从不为其中任何一个设置name属性,因此我认为如果您使用常规提交,它们永远不会正确传递给代码。这有点难以说,因为我们看不到jQuery代码,但这就是我认为的问题 - 在jQuery中构建你的请求参数。

要修复分页,在构建这样的查询时:

   $query  .= " and m_country in('$countrydata') LIMIT {$startpoint} , {$per_page} "; 

首先,只添加过滤器:

   $query  .= " and m_country in('$countrydata') "; 

然后在完成所有过滤器之后,在执行查询之前添加:

   $query  .= " LIMIT {$startpoint} , {$per_page} "; 

出于调试目的,请考虑在此行之前打印出$ query的值:

   $rs = mysqli_query($mysqli,$query) or die("Error : ".mysqli_error($mysqli));

并在第1页和第2页比较它们,看看是否有任何东西伸出来作为一个疼痛的拇指。

答案 1 :(得分:0)

Tadasbub的回答应该足够了。我只是根据他的建议写出变化。

              <?php 


             $query = "SELECT * FROM movies 
      WHERE movie_status = '1'"; 
              //filter query start 
              if(!empty($genre)){
                  $genredata =implode("','",$genre);
                  $query  .= " and m_genre in('$genredata')"; 
              }

               if(!empty($country)){
                  $countrydata =implode("','",$country);
                  $query  .= " and m_country in('$countrydata')"; 
              }

              if(!empty($year)){
                  $yeardata =implode("','",$year);
                  $query  .= " and m_year in('$yeardata')"; 
              }
            //filter query end 
             $query .= " LIMIT {$startpoint} , {$per_page} ";
             $rs = mysqli_query($mysqli,$query) or die("Error : ".mysqli_error($mysqli));

只需更改此处写的查询块即可。它应该按照你的预期工作。

顺便说一下,问题只在于php逻辑。它与sql / mysql无关。