在动态SQL筛选结果

时间:2017-11-09 15:39:49

标签: php jquery html mysql pagination

我在sql查询中添加了一个复选框过滤器来过滤一组视频。我曾经尝试过分页,我必须对像SELECT * FROM movies之类的简单直接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";  name.

            $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));
      echo '<div class= "wrap">';
      // displaying paginaiton.

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

     echo "</div>";

问题是我尝试了不同的方法,但我不能让分页功能与过滤器SQL查询一起正常工作。我如何在latest.php中实现分页,任何有效的分页功能都会非常有用。

0 个答案:

没有答案