PHP MySQL分页

时间:2014-04-11 11:56:32

标签: php html mysql paging

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

这里的一切都很好。但是db中有大约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);
?>

亲切的问候。

4 个答案:

答案 0 :(得分:2)

您可以使用MySQL LIMIT

我曾经这样做过:

获取您正在寻呼的行总数,并在网址中输入一个参数,即&#34; / p / 5&#34;或者?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}");

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

建立你的分页链接......这完全取决于你。您应该从表&#34;中获取&#34;选择计数(PRIMARY_KEY)的总行数。查询优先,这样就可以计算最大页数。

答案 1 :(得分:0)

您正在搜索的是LIMIT的mysql。

使用非常简单。

例如:

"SELECT * FROM tablename LIMIT 3"

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

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

"SELECT * FROM tablename LIMIT offset,results"

可以计算偏移量,具体取决于您希望每页的结果数量。

"SELECT * FROM tablename LIMIT 20,10"

这将显示10个结果,从结果20开始。如果您希望每个站点获得10个结果,则可以是3.站点。

答案 2 :(得分: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

&GT;

答案 3 :(得分: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>