与PAGINATION一起使用ORDER功能。 PHP /库MySQLi

时间:2014-11-14 11:34:26

标签: php mysqli pagination sql-order-by

这是我的订单 sortOrder.php

<?php
$queryMain ="SELECT newsvid.id, newsvid.addName, newsvid.vidTitle, newsvid.vidType, newsvid.size, newsvid.url, newsvid.vidSD, newsvid.published, videoinformation.vidLD, videoinformation.vidYear, videoinformation.vidCity, videoinformation.vidZanr, videoinformation.vidZanr2, videoinformation.vidZanr3, videoinformation.vidQuality, videoinformation.vidTranslated, videoinformation.vidTime  FROM newsvid, videoinformation WHERE newsvid.id = videoinformation.id AND approved='1'";

// Video type
$vType = isset($_GET['vType']) ? $_GET['vType'] : 'ALL';
$goodTypeParam = array("AnyType", "Film", "Serials", "Cartoon", "Anime");

if (in_array($vType, $goodTypeParam)) {
    if($vType == 'AnyType'){}
    else{$queryMain .= " AND newsvid.vidType ='".$_GET['vType']."'";}
}




//Video Genre one
$vGenre = isset($_GET['vGenre']) ? $_GET['vGenre'] : 'ALL';
$goodGenreParam = array("AnyGenre1", "Action", "Adventure", "Comedy", "Crime", "Faction", "Fantasy", "Historical", "Horror", "Mystery", "Paranoid", "Philosophical", "Political", "Realistic", "Romance", "Saga", "Satire", "Science-Fiction", "Slice-Of-Life", "Speculative", "Anime");

if (in_array($vGenre, $goodGenreParam)) {
    if($vGenre == 'AnyGenre1'){}
    else{$queryMain .= " AND ( videoinformation.vidZanr ='".$_GET['vGenre']."' OR videoinformation.vidZanr2 ='".$_GET['vGenre']."' OR videoinformation.vidZanr3 ='".$_GET['vGenre']."')";}
}

//Video Genre two
$vGenre2 = isset($_GET['vGenre2']) ? $_GET['vGenre2'] : 'ALL';
$goodGenre2Param = array("AnyGenre2", "Action2", "Adventure2", "Comedy2", "Crime2", "Faction2", "Fantasy2", "Historical2", "Horror2", "Mystery2", "Paranoid2", "Philosophical2", "Political2", "Realistic2", "Romance2", "Saga2", "Satire2", "Science-Fiction2", "Slice-Of-Life2", "Speculative2", "Anime2");

if (in_array(vGenre2, $goodGenre2Param)) {
    if(vGenre2 == 'AnyGenre2'){}
    else{$queryMain .= " AND ( videoinformation.vidZanr ='".$_GET['vGenre2']."' OR videoinformation.vidZanr2 ='".$_GET['vGenre2']."' OR videoinformation.vidZanr3 ='".$_GET['vGenre2']."')";}
}

//Video Genre three
$vGenre3 = isset($_GET['vGenre3']) ? $_GET['vGenre3'] : 'ALL';
$goodGenre3Param = array("AnyGenre3", "Action", "Adventure", "Comedy", "Crime", "Faction", "Fantasy", "Historical", "Horror", "Mystery", "Paranoid", "Philosophical", "Political", "Realistic", "Romance", "Saga", "Satire", "Science-Fiction", "Slice-Of-Life", "Speculative", "Anime");

if (in_array($vGenre, $goodGenre3Param)) {
    if($vGenre3 == 'AnyGenre3'){}
    else{$queryMain .= " AND ( videoinformation.vidZanr ='".$_GET['vGenre3']."' OR videoinformation.vidZanr2 ='".$_GET['vGenre3']."' OR videoinformation.vidZanr3 ='".$_GET['vGenre3']."')";}
}





// Video Years
$vYear = isset($_GET['vYear']) ? $_GET['vYear'] : 'ALL';
$goodYearParam = array("AnyYear", "2014", "2013", "2012", "2011", "2010", "2009", "2008", "2007", "2006", "2005", "2004", "2003", "2002", "2001", "2000", "1999", "1998", "1997");

if (in_array($vType, $goodYearParam)) {
    if($vYear == 'AnyYear'){}
    else{$queryMain .= " AND newsvid.vidYear ='".$_GET['vYear']."'";}
}


// Video City
$vCity = isset($_GET['vCity']) ? $_GET['vCity'] : 'ALL';
$goodCityParam = array("AnyCity", "Russian", "England");

if (in_array($vCity, $goodCityParam)) {
    if($vCity == 'AnyCity'){}
    else{$queryMain .= " AND newsvid.vidCity ='".$_GET['vCity']."'";}
}



//NEW of OLD
$order = isset($_GET['order']) ? $_GET['order'] : 'ALL';
$goodParam = array("NEW", "OLD");

if (in_array($order, $goodParam)) {

    if($order == 'NEW'){
     $queryMain .= " ORDER BY newsvid.id ASC"; 
    }else if($order == 'OLD'){
     $queryMain .= " ORDER BY newsvid.id DESC"; 
    }else{
     $queryMain .= " AND videoinformation.vidYear = 2014"; 
        }
}

?>

这是主页 view.php

<!DOCTYPE lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<?php include 'BSH.php' ?>
<link rel="stylesheet" type="text/css" href="CSS/sdvid.css">
<title><?php echo $lang['PAGE_TITLE_MAIN'] ?></title>
</head>

<body>
<?php 
include_once 'userPages/check_login_status.php';
include_once 'incIndex/headerTop.php'; 
include 'connect/con.php';
include_once 'inc/sortOrder.php';
?>



<?php
$sqlPages = "SELECT COUNT(id) FROM newsvid WHERE approved='1'";
$queryPages = mysqli_query($con, $sqlPages);
$row = mysqli_fetch_row($queryPages);
// Here we have the total row count
$rows = $row[0];
// This is the number of results we want displayed per page
$page_rows = 1;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
    $last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
}
// This sets the range of rows to query for the chosen $pagenum
$limit = "LIMIT " .($pagenum - 1) * $page_rows ."," .$page_rows;
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
$queryMainList = $queryMain . $limit;
$resultDisplay = mysqli_query($con, $queryMainList);
// This shows the user what page they are on, and the total number of pages
$pagesTitle = "On website <b>$rows</b>";
$pagesOutOf = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
    /* First we check if we are on page one. If we are then we don't need a link to 
       the previous page or the first page so we do nothing. If we aren't then we
       generate links to the first page, and to the previous page. */
    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';
        // Render clickable number links that should appear on the left of the target page number
        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            }
        }
    }
    // Render the target page number, but without it being a link
    $paginationCtrls .= ''.$pagenum.' &nbsp; ';
    // Render clickable number links that should appear on the right of the target page number
    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }
    // This does the same as above, only checking if we are on the last page, and then generating the "Next"
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
    }
}
$list = '';

while($row = mysqli_fetch_array($resultDisplay, MYSQLI_ASSOC)){

$list .= "<div class=\"panel-heading\">
<div><a class=\"panel-title btn-block\" href=\"details.php?id=".$row['id']."\"><h3>".$row['id']." | ".$row['vidTitle']."</h3></a></div>
</div>

<div class=\"panel-body\">
<div class=\"imgCover\"><img class=\"imageCover\"src=\"" . $row['url'] . "\"></div>
<div class=\"vidSD\">" . $row['vidSD'] . "</div>
<div class=\"vidDetails\"> 

<hr class=\"style-two\">
<table>
<tr><td class=\"vidDetailsTD\"><strong>" . $lang['vtYear'] . "</strong></td><td class=\"vidDetailsTD\">" . $row['vidYear'] ."</td></tr>
<tr><td class=\"vidDetailsTD\"><strong>" . $lang['vtCity'] . "</strong></td><td class=\"vidDetailsTD\">". $row['vidCity'] ."</td></tr>
<tr><td class=\"vidDetailsTD\"><strong>" . $lang['vtGenre'] . "</strong></td><td class=\"vidDetailsTD\">". $row['vidZanr'] ." , ". $row['vidZanr2'] ." , ". $row['vidZanr3'] . "</td></tr>
<tr><td class=\"vidDetailsTD\"><strong>" . $lang['vtQuality'] . "</strong></td><td class=\"vidDetailsTD\">". $row['vidQuality'] ."</td></tr>
<tr><td class=\"vidDetailsTD\"><strong>" . $lang['vtTranslatedBy'] . "</strong></td><td class=\"vidDetailsTD\">". $row['vidTranslated'] ."</td></tr>
<tr><td class=\"vidDetailsTD\"><strong>" . $lang['vtVideoTime'] . "</strong></td><td class=\"vidDetailsTD\">". $row['vidTime'] .  "</td></tr>
</table> 
</div></div>

<div class=\"panel-footer\">
<h6><strong>" . $lang['vsdAuthor'] . "</strong><a href=\"../userPages/user.php?u=".$row['addName']."\">".$row['addName']."</a></h6>
<div><h6><strong>" . $lang['vsdPublished'] . "</strong>" . $row['published'] . "</h6></div>
</div>";
}
mysqli_close($con);


?>


<div class="mainLeftCover">


<form action="view.php" method="GET">
<div class="input-group" style="width:180px">
  <span class="input-group-addon" style="width:65px"><?php echo $lang['vidOrderTitleNew'] ?></span>
  <select class="form-control" name = "order">
      <option value="NEW">NEW</option>
      <option value="OLD">OLD</option>
</select>
</div>
<?php 
include_once 'inc/sortInc/sortType.php';
include_once 'inc/sortInc/sortGenre.php';
include_once 'inc/sortInc/sortGenre2.php';
include_once 'inc/sortInc/sortGenre3.php';
include_once 'inc/sortInc/sortYear.php';
include_once 'inc/sortInc/sortCity.php';
?>  
<br><button type="submit" class="btn btn-default" style="width:180px">Submit</button> 
</form>

<?php echo" <div id=\"pagination_controls\">" .$paginationCtrls. "</div>"; ?>

</div>








<?php
echo "<div class=\"maincover \" data-role=\"scrollbox\" data-scroll=\"vertical\">";
echo "<div class=\"panel panel-default\">";
echo" <div style=\"background-color:#fff\">" .$list. "</div>";
echo "</div></div>";


?>

仅仅为了包装,我提供了完整的代码。问题是, PAGINATION 对它自己的工作正常......并且 ORDER 功能也可单独运行。但他们一起不想工作。结果我有工作分页,如果尝试使用排序它只是成为空页。

我需要什么..以某种方式使排序方法与分页一起工作,但我坚持如何做到这一点。

当我使用分页时,我有这个URL:

http://example.net/view.php?pn=3

当我使用订单时这个:

http://example.net/view.php?order=NEW&vType=Film&vGenre=AnyGenre1&vGenre2=AnyGenre2&vGenre3=AnyGenre3&vYear=AnyYear&vCity=AnyCity

AND 我需要的网址如下:

http://example.net/view.php?pn=3&order=NEW&vType=Film&vGenre=AnyGenre1&vGenre2=AnyGenre2&vGenre3=AnyGenre3&vYear=AnyYear&vCity=AnyCity

如果您对页面进行排序...它将记住用户对列表进行排序的方式,并且打开页面(pn = 1,pn = 2)将会更改。

可能是一些如何保存排序结果,然后在分页中使用它...需要保存它可能是按下提交按钮?但是如何保存用户???

的结果

1 个答案:

答案 0 :(得分:0)

<?php
session_start();
include_once 'dbconnect.php';
?>
<?php include ('head.php')  ; ?>
<?php include ('menu.php')  ; ?>
<?php if (isset($_SESSION['usr_id'])) { ?>

<?
$per_page = 15;
if (isset($_GET["page"])) {$page = $_GET["page"];}else {$page = 1;}
if (isset($_GET["order"])) {$order = $_GET["order"];}else {$order = username;}

$start_from = ($page-1) * $per_page;

if(isset($_GET['order']) && $_GET['order'] == 'user_id'){
$query = "select * from users order by user_id DESC limit $start_from, $per_page";}
if(isset($_GET['order']) && $_GET['order'] == 'username'){
$query = "select * from users order by username ASC limit $start_from, $per_page";}
if(isset($_GET['order']) && $_GET['order'] == 'posts'){
$query = "select * from users order by posts DESC limit $start_from, $per_page";}

$result = mysqli_query ($DBcon, $query);

$user_list_result = $DBcon->query("select * from users order by $order ASC limit 100");
    $session = $_SESSION['usr_name'];     
    $query = $DBcon->query("SELECT * FROM users WHERE username='$session'");
    $userRow=$query->fetch_array();

 if ($userRow['userlevel'] > 0) { 

?>

<div class="container">
 <div class="row">
  <div class="col-lg-6 col-sm-12">
   <div class="panel panel-default panel-compact panel-wallet">
    <div class="panel-body">
     <center><h1>Userlist&nbsp;</h1></center>
       <center>
          <table >
           <tr>  <td width="20%"><a href="user_list.php?page=<?php echo $_GET["page"]; ?>&order=user_id"><b>ID&nbsp;</b></a></td>
           <td width="20%"><a href="user_list.php?page=<?php echo $_GET["page"]; ?>&order=username"><b>Nickname&nbsp;</b></a></td>
           <td width="20%"><a href="user_list.php?page=<?php echo $_GET["page"]; ?>&order=posts"><b>Posts&nbsp;</b></a></td>
             <td width="20%"><b>Message&nbsp;</b></td> 
             <td width="20%"><b>Click&nbsp;</b></td> </tr>
<?php

while($UserlistRow = $result->fetch_array())
    {
    echo "
      <tr>
            <td>$UserlistRow[user_id]</td>
        <td><a href=user_profile.php?user=$UserlistRow[username]>$UserlistRow[username]</a></td>
            <td>$UserlistRow[posts]</td>
        </center>
            <td><a href=msg_send.php?to=$UserlistRow[username]>Send Message</a></td>
            <td><a href=user_click.php?to=$UserlistRow[username]>Click</a></td> ";?>
      </tr>
        <?php } ?>  </table>
        </center>
<?
$query = "select * from users order by $order";
$result = mysqli_query($DBcon, $query);
$total_records = mysqli_num_rows($result);
$total_pages = ceil($total_records / $per_page);

echo "<a href='user_list.php?page=" . ($_GET['page']+1) . "&order=" . ($_GET['order']) . "'>Next Page</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='user_list.php?page=".$i."&order=" . ($_GET['order']) . "'>".$i."</a> ";
};
echo "<a href='user_list.php?page=" . ($_GET['page']-1) . "&order=" . ($_GET['order']) . "'>Previous Page</a>";
?>
    </div>
   </div>
  </div>
 </div>
</div>
<?php } ?>
<?php } else {  include('login_frame.php');  } ?>
<?php include ('footer.php')  ; ?>