PHP和MySQLI的分页,其中URL包含变量

时间:2017-08-26 15:05:15

标签: php mysqli pagination

我一直在使用脚本来分页一堆数据。我已经开始研究包含变量了,我已经改变了脚本,以便它在URL字符串中包含$ _GET变量,但是,现在我的分页不起作用。当我点击“下一个”链接时,它只保留相同的数据集。我不确定如何最有效地处理这个问题,当我点击分页链接时它会显示下一组数据。

<body>

<?php include '../includes/menu/menu_gallery_nl.php';?>

<center><img class="centerHeader" src="xxx/vg/img/galleryheader.jpg" width="1140px"></center>
<br />
<br />

<div id="indexmap" class="container">
  <div class="row">
    <div class="col-lg-12">
<?php

if (!defined('DB_SERVER')) define('DB_SERVER', 'xxx');
if (!defined('DB_USER')) define('DB_USER', 'xxx');
if (!defined('DB_PASSWORD')) define('DB_PASSWORD', 'xxx');
if (!defined('DB_TABLE')) define('DB_TABLE', 'xxx');

// The procedural way
$mysqli = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_TABLE);
$mysqli->set_charset("utf8");
$mysqli->query("SET NAMES 'utf8'");
if (mysqli_connect_errno($mysqli)) {
    trigger_error('Database connection failed: '  . mysqli_connect_error(), E_USER_ERROR);
}

    $lvmID = (isset($_GET['lvmid']) ? $_GET['lvmid'] : null);

// This first query is just to get the total count of rows
$sql = "SELECT COUNT(*) FROM tbl_photos p INNER JOIN tbl_luchtvaartmaatschappij lvm ON p.img_lvm = lvm.IATACode WHERE lvm.luchtvaartmaatschappijID = '$lvmID' ";
$query = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_row($query);
// Here we have the total row count
$rows = $row[0];
// This is the number of results we want displayed per page
$page_rows = 16;
// 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
$sql = "
    SELECT randimgID, img_location, img_lvm, img_file, img_nmr, img_t, t.toestel, l.luchthavennaam, lvm.luchtvaartmaatschappij, lvm.luchtvaartmaatschappijID

    FROM tbl_photos p

    LEFT JOIN tbl_luchthaven l
    ON p.img_location = l.luchthavenID

    LEFT JOIN tbl_luchtvaartmaatschappij lvm
    ON p.img_lvm = lvm.IATAcode

    LEFT JOIN tbl_toestel t
    ON p.img_t = t.toestelID

    WHERE lvm.luchtvaartmaatschappijID = '$lvmID'
    ORDER BY lvm.luchtvaartmaatschappij ASC $limit ";

$query = mysqli_query($mysqli, $sql);
/* determine number of rows result set */

$row_cnt = mysqli_num_rows($query);
// This shows the user what page they are on, and the total number of pages
$textline1 = "beelden (<b>$rows</b>)";
$textline2 = "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 .= '<li class="page-item"><a class="page-link"  href="'.$_SERVER['PHP_SELF'].'?lvmid='.$lvmID.'?pn='.$previous.'">vorige</a> &nbsp; &nbsp; </li>';
        // 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 .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF'].'?lvmid='.$lvmID.'?pn='.$i.'">'.$i.'</a> &nbsp; </li>';
            }
        }
    }
    // Render the target page number, but without it being a link
    $paginationCtrls .= '<li class="page-item active">
      <a class="page-link" href="#">'.$pagenum.' <span class="sr-only">(current)</span></a>
    </li>';


    // Render clickable number links that should appear on the right of the target page number
    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF'].'?lvmid='.$lvmID.'?pn='.$i.'">'.$i.'</a> </li> ';
        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 .= '<li class="page-item"><a class="page-link"  href="'.$_SERVER['PHP_SELF'].'?lvmid='.$lvmID.'?pn='.$next.'">volgende</a> </li>';
    }
}
$list = '';
$items_in_row = 2 ;
$index = 0 ;



    echo "<table width='100%' cellspacing='1' border='0'>


          <tr>";      

    while($row = mysqli_fetch_assoc($query)) {
        $img_location = $row['luchthavennaam'];
        $img_lvm = $row['img_lvm'];
        $l = htmlspecialchars($row['luchtvaartmaatschappij']);
        $lvmID = $row['luchtvaartmaatschappijID'];
        $img_nmr = $row['img_nmr'];
        $t = $row['toestel'];
        $f = $row['img_file'];
        $rID = $row['randimgID'];
        $img_file = $row['img_file'];

        $image = "xxx/gallery/$img_lvm/$img_file"; 
        $link = "xxx/gallery/details.php?pid=$rID&lvmID=$lvmID&in=$img_nmr"; 



        $index++ ;


    echo "<td width='370' align='left' valign='top' bgcolor='#292960'> ";           

    echo "<table width='540' border='0' cellpadding='0' cellspacing='0'>";
    echo "<tbody>";
    echo "  <tr>";
    echo "  <td height='15' colspan='3' bgcolor='#292960'></td>";
    echo "  </tr>";
    echo "  <tr>";
    echo "    <td height='15' colspan='3' bgcolor='#000033'></td>";
    echo "  </tr>";
    echo "<tr>";
    echo "  <td width='15' bgcolor='#000033'>&nbsp;</td>";
    echo " <td bgcolor='#000033'><a href='" . $link ."'><img src='".$image."' width='510'></a></td>";

    echo "  <td width='15' bgcolor='#000033'></td>";
    echo "</tr>";
    echo "<tr>";
    echo "  <td bgcolor='#000033'>&nbsp;</td>";
    echo "  <td bgcolor='#000033'><table width='510' border='0' align='center' cellpadding='0' cellspacing='0'>";
    echo "    <tr>";
    echo "      <td height='15' colspan='2' valign='top' style='text-align: left'>&nbsp;</td>";
    echo "      </tr>";
    echo "    <tr>";
    echo "      <td width='72%' valign='top' style='text-align: left'><span style='font-size: 18px; color: #DEDEDE'>" . $l . "</span></td>";
    echo "      <td width='28%' style='text-align: left'><span style='font-size: 14.5px; color: #DEDEDE'>Reg: " . $img_nmr . "</span></td>";
    echo "    </tr>";
    echo "    <tr>";
    echo "      <td height='10' colspan='2' valign='top' style='text-align: left'></td>";
    echo "      </tr>";
    echo "    <tr>";
    echo "      <td valign='top' style='text-align: left'><span style='font-size: 14.5px; color: #DEDEDE'>" . $t . "</span></td>";
    echo "      <td style='text-align: left'>&nbsp;</td>";
    echo "    </tr>";
    echo "    <tr>";
    echo "      <td height='10' colspan='2' valign='top' style='text-align: left'></td>";
    echo "      </tr>";
    echo "    <tr>";
    echo "      <td colspan='2' valign='top' style='text-align: left'><span style='font-size: 14.5px; color: #DEDEDE'>" . $img_location . "</span><br>
            <br>
          </td>";
    echo "    </tr>";
    echo "    <tr>";
    echo "      <td style='text-align: left'>&nbsp;</td>";
    echo "      <td style='text-align: left'>&nbsp;</td>";
    echo "    </tr>";
    echo "  </table></td>";
    echo "  <td bgcolor='#000033'>&nbsp;</td>";
    echo "</tr>";
    echo "<tr>";
    echo "  <td>&nbsp;</td>";
    echo "  <td>&nbsp;</td>";
    echo "  <td>&nbsp;</td>";
    echo "</tr>";
    echo "  </tbody>";
    echo "      </table>";
    echo "      </td>";

    if ($index%$items_in_row == 0){ 

    echo "    </tr>";

    echo "    <tr>";
         }
        } 
    echo "  </tr>";
    echo "  </table>";

// Close your database connection
mysqli_close($mysqli);
?>


<br />

<nav aria-label="Page Navigation">
    <ul class="pagination justify-content-center">
<div class="pagination"><?php echo $paginationCtrls; ?></div>
</ul>
</nav>
<br />

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


</div>
<?php include '../includes/menu/footer.php'; ?>
</body>
</html>

0 个答案:

没有答案