PHP MySQL 分页

问题描述 投票:0回答:4

我尝试了很多不同的解决方案。我刚接触 php 1 周,12 年前使用 ASP,所以我希望能得到一些帮助。

这里一切都运转良好。但是数据库中有大约 1000 行,我需要将它们分成页面。

<?php
$con = mysqli_connect("localhost","test","test","test")or die('could not connect to database');

// Check connection
if (mysqli_connect_errno())
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

echo "<table border='0'>
 <tr>
  <th>Img:</th>
  <th>Text:</th>
 </tr>";

$result = mysqli_query($con,"SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText FROM Jokes LEFT JOIN Categories ON Jokes.CategoryID = Categories.ID ORDER BY  Jokes.JokeText");

while($row = mysqli_fetch_array($result))
  {

   echo "<tr>";
   echo "<td align='center'><img src='webimg/" . $row['CategoryName'] . ".png' height='35' width='35'></td>";
   echo "<td align='left'  width='80%'>" . $row['JokeText'] . "</td>";
   echo "</tr>";

  }
echo "</table>";

mysql_close($con);
?>

亲切的问候。

php html mysql pagination
4个回答
2
投票

您可以使用 MySQL

LIMIT

我曾经这样做过:

获取你正在分页的总行数,并有一个类似URL中的参数,即“/p/5”或?page=5(我将使用这个作为参考,更容易编写代码并让你理解)对于页码5、也做一个故障保护,像这样:

假设每页有 10 条记录:

$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$records_per_page = 10;

而且,在你的 SQL 中你将会有这样的东西

$start = ($page-1) * $records_per_page;
$result = mysql_query("select * from table limit {$start}, {$records_per_page}");

有点粗糙,但你应该明白要点并走在正确的道路上。

要建立分页链接...这完全取决于您。您应该先通过“从表中选择计数(PRIMARY_KEY)”查询获得行总数,以便您可以计算最大页数。


0
投票

你要找的是mysql的

LIMIT

使用方法非常简单。

例如:

"SELECT * FROM tablename LIMIT 3"

这将为您提供前三个结果。

在您的情况下,您需要一个偏移量,取决于当前页面:

"SELECT * FROM tablename LIMIT offset,results"

偏移量是可以计算的,取决于您想要每页有多少结果。

"SELECT * FROM tablename LIMIT 20,10"

这将显示 10 个结果,从结果 20 开始。如果您想要每个站点 10 个结果,这可能适用于 3. 站点。


0
投票
<?php


$per_page = 10; //no. of results to display in one page
$pages_query = mysql_query("SELECT COUNT('id') FROM JOKES");//Or whatever field. this is just to check the number of results.
$pages = ceil(mysql_result($pages_query, 0) / $per_page);//to get the total no. of pages that will be there. For example if u have 60 results than no. og pages will be 60/10=6

$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;// if the page variable in your url is set that means that u have clicked a page number. So it will be taking that page number and displaying those results 
$start = ($page - 1) * $per_page;//to get the starting result of that page. If you are in say 6th page, then the starting element would be 6-1*10=50. This makes sure that the results in the previous pages are not displayed

$query = mysql_query("SELECT * FROM JOKES LIMIT $start, $per_page");//set the limit of results that page

while($query_row = mysql_fetch_assoc($query)){
    echo " ur table names ";
}

$prev = $page - 1;//to set the prev page variable
$next = $page + 1;//to set the next page variable

if(!($page<=1)){
    echo "<a href='Yourpagename.php?page=$prev'>Prev</a> ";
}//does not displays the prev variable if you are already one first page

if($pages>=1 && $page<=$pages){

    for($x=1;$x<=$pages;$x++){
        echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> ';

    }//display all the pages. Display the current page as bold

}

if(!($page>=$pages)){
    echo "<a href='Your page name.php?page=$next'>Next</a>";
}//do not display next vvariable if your are in the last page

?>


0
投票

谢谢所有的答案。我学习了这个教程并且它起作用了:http://www.developphp.com/view.php?tid=1349

<?php
include_once("mysqli_connection.php");

$sql = "SELECT COUNT(id) FROM testimonials WHERE approved='1'";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);

$rows = $row[0];

$page_rows = 10;

$last = ceil($rows/$page_rows);

if($last < 1){
    $last = 1;
}

$pagenum = 1;

if(isset($_GET['pn'])){
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}

if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
}

$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$sql = "SELECT id, firstname, lastname, datemade FROM testimonials WHERE approved='1' ORDER BY id DESC $limit";
$query = mysqli_query($db_conx, $sql);

$textline1 = "Testimonials (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";

$paginationCtrls = '';

if($last != 1){

    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';

        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            }
        }
    }

    $paginationCtrls .= ''.$pagenum.' &nbsp; ';

    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }

    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
    }
}
$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
    $id = $row["id"];
    $firstname = $row["firstname"];
    $lastname = $row["lastname"];
    $datemade = $row["datemade"];
    $datemade = strftime("%b %d, %Y", strtotime($datemade));
    $list .= '<p><a href="testimonial.php?id='.$id.'">'.$firstname.' '.$lastname.' Testimonial</a> - Click the link to view this testimonial<br>Written '.$datemade.'</p>';
}

mysqli_close($db_conx);
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
div#pagination_controls{font-size:21px;}
div#pagination_controls > a{ color:#06F; }
div#pagination_controls > a:visited{ color:#06F; }
</style>
</head>
<body>
<div>
  <h2><?php echo $textline1; ?> Paged</h2>
  <p><?php echo $textline2; ?></p>
  <p><?php echo $list; ?></p>
  <div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.