【问题标题】:how to add pagination in this code如何在此代码中添加分页
【发布时间】:2012-12-02 11:12:32
【问题描述】:

如何在这段代码中添加分页?

我想在一页中限制数据,我想在底部:

1 2 3 4 5 6 8 9 10

如果我点击page 2,那么我的更多结果会显示在该页面上。

我该怎么做?

<?php

 //connect to database

     mysql_connect('localhost','root','pasword');
     mysql_select_db('root');


/* Get the letter user clicked on and assign it a variable called $sort */
$sort = $_REQUEST['letter'];

/* Let's check if variable $sort is empty. If it is we will create a query to display all customers alphabetically ordered by last name. */
if($sort == ""){


$qry= "SELECT * FROM tree ORDER BY keywords ASC " ;
}else{
/* if varible $sort is not empty we will create a query that sorts out the customers by their last name, and order the selected records ascendingly. */
$qry = "SELECT * FROM tree WHERE keywords LIKE '$sort%' ORDER BY keywords ASC" ;
}
/* Notice the use of '%' wilde card in the above query  "LIKE '$sort%'". */

//next step is to execute the query.
$execute = mysql_query($qry) or die(mysql_error());

/* Before we display results let's create our alphabetical navigation. The easiest way to create the navigation is to use character codes and run them through the "for" loop. */
echo "<p>" ;
for ($i = 65; $i < 91; $i++) {
    printf('<a href="%s?letter=%s">%s</a> | ',
    $PHP_SELF, chr($i), chr($i));
}
echo "</p>" ;

/* now we are ready to display the results. Since out tbl_customers table has only three fileds we will display the results in a paragraphs. In the real world you may need to display the results in a table.
To display the results we will use "do while" loop to fetch the results. If no customers are found we will display an error message. */
if(mysql_num_rows($execute)>0){
do{
echo "<p>" .$result['id']. " " .$result['keywords']. " " .$result['meaning']. "</p>" ;
}while($result = mysql_fetch_assoc($execute));
}else{
echo "<p>No customer found.</p>" ;
} 
?>

【问题讨论】:

  • 或者,你有没有尝试过什么?但是给你一个方向:LIMIT $iOffset, 10for($i = 0; $i &lt; $iResults; $i++){ if($i % 10 == 0) { // stuff here } }
  • ben fransen 我将你的代码放入我的代码中
  • 出于好奇,您是否考虑过使用 jQuery 插件,例如 DataTables(可以进行分页、限制和排序)?

标签: php mysql pagination


【解决方案1】:
$page = intval(abs($_GET['page'])); // get security ONE

if(mysql_num_rows($sql)) // get page number to empty location index.php
{
    header('Location: index.php');
}

【讨论】:

    【解决方案2】:

    你可以使用下面的代码

    $page = $_GET['page'];
    
    if ($page < 1)  
    {       
       $page = 1;
    }
    
    $conc=mysql_connect('localhost','root','');
    mysql_select_db('employer',$conc);
    
    $resultsPerPage = 5; 
    
    $startResults = ($page - 1) * $resultsPerPage;
    
    $query = mysql_query('SELECT * FROM employee ') or die(mysql_error());
    
    $numberOfRows=mysql_num_rows($query);
    
    $totalPages = ceil($numberOfRows / $resultsPerPage);
    
    $query = mysql_query("SELECT * FROM employee LIMIT $startResults,$resultsPerPage");
    
    while ($output = mysql_fetch_assoc($query))
    {
    $result[]=$output;
    }
    
    
    for($i = 1; $i <= $totalPages; $i++)
    {
    
        if($i == $page)
        echo '<strong>'.$i.'</strong>&nbsp';
    else
        echo '<a href="?page='.$i.'">'.$i.'</a>&nbsp';
    }
    

    【讨论】:

      【解决方案3】:

      先选择数据个数按total_number of rows by no page of pages生成页码

      mysql_num_rows($execute);
      

      使用limit命令来限制这样的数据

      $qry= "SELECT * FROM tree ORDER BY keywords ASC limit 0,10 " ;
      

      然后对于每个页码增加限制索引

      【讨论】:

        猜你喜欢
        • 2023-01-24
        • 2019-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-26
        • 2016-06-12
        相关资源
        最近更新 更多