doucheng7808 2012-10-17 16:49
浏览 32
已采纳

需要一个Pagination PHP代码来集成我的SEARCH php脚本

It's been a month and am really messed up trying to integrate a php pagination code to my search script. Referred to most of the tutorials Googling, but in vain. Any help would be much appreciated. Here I go...

<?php
 ob_start();
 session_start();
 $_GET['term'] = trim($_GET['term']);
 $output = preg_replace('!\s+!', ' ', $_GET['term']);
 if(empty($_GET['term'])|| preg_match("/^[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\?\ _ ]+$/i", $_GET['term']) || $output== ' ' || $_GET['term']== "%24_GET%5B%27term%27%5D")
 {
 echo "<BR>";
 echo "<BR>";
 echo("Please enter a Valid Search term");
 }
 else
 {
    mysql_connect("localhost", "root", "root");
    mysql_select_db("search");
    $_GET['term'] = explode(' ', $_GET['term']);
    foreach($_GET['term'] AS $_GET['term'])
     {
     $_GET['term'] = trim($_GET['term']);
 $sql = mysql_query("SELECT DISTINCT * FROM searchengine WHERE pagecontent LIKE '%" . str_replace(' ', "%' AND pagecontent LIKE '%", $_GET['term'])."%' LIMIT 0,10");
   while($ser = mysql_fetch_array($sql)) {
       echo "<BR>";
        echo "<b><u><a href='$ser[pageurl]'>$ser[title]</a></u></b>";
        echo "<BR>";
        echo("<span class='style_block'>{$ser['pagecontent']}</span>");
        echo "<BR>";
        echo ("<a href='$ser[pageurl]'>$ser[pageurl]</a>");
        echo "<BR>";
        echo "<BR>";
       } 
    }
$count=mysql_num_rows($sql);
if($count==0)
{
 echo "<BR>";
 echo "<BR>";
echo "Sorry, No News material was found... Please refine your search criteria and try again.";
}
    }
?>
  • 写回答

1条回答 默认 最新

  • duanhuang2150 2012-10-17 17:38
    关注

    Apart from the problems Luc M has mentioned in his comment (which you should certainly resolve before moving forward), you are almost there.

    You need to consider a couple of points, really: How many records to display per page, and what page you are on. These will dictate the records you need to retrieve and display. So, how do you go about this?

    The first point is covered in your code already through use of the LIMIT clause in your SQL query. The second point is a tiny bit more complex to start with. You need a way of identifying the page you are on. This is probably easiest to identify through a GET variable, for example http://site.com/search.php?page=2. Now, for implementing this, you want something along these lines:

    $recordsPerPage = 10; // although you may want to have this as a GET or POST variable as well, so the user can decide
    if(isset($_GET['page']) // this ensures a default value
    {
        $currentPage = $_GET['page'];
    }
    else
    {
        $currentPage = 1;
    }
    

    Then, for your SQL query, you want to build something like this:

    $query = "SELECT * FROM table_name LIMIT " . $recordsPerPage . " OFFSET " . ($currentPage - 1)*$recordsPerpage . ";";
    

    The OFFSET clause of SQL along with LIMIT basically says "Select this many records, starting from result number x". You offset on $currentPage - 1 because the first page doesn't want an offset, and the second page only wants an offset of however many records were shown on the first page, so on and so forth.

    To create navigation for the paginated data, you want to find out how many records are in your result set, which can be done through the count($array) function of PHP. Then, to find the number of pages, simply use something like:

    $numPages = ceil(count($array)/$recordsPerPage);

    Where $array is your dataset from the SQL query. The ceil() function rounds the result up to the next integer.

    Once you have this result, you simply need to output links to each page, which can be done simply with a for loop:

    for($i = 0; i < $numPages; i++)
    {
        echo '<a href="/search.php?page="' . $i+1 . '>' . $i+1 . '</a>';
    }
    

    To create first, previous, next and last page links, you need to do something like:

    $firstPage = 1;
    $previousPage = $currentPage - 1; // you may want to check here or elsewhere to make sure you have no page zero
    $nextPage = $currentPage + 1; // may also want to make sure you don't go past the last page
    $lastPage = $numPages;
    

    These values can then be put into your generated links.

    Again, I will refer you to Luc M's comment... These need to be fixed, take a look at mysqli functions instead of the now-deprecated mysql_*() functions you're currently using, make sure you clean any user-inputted data before using it, and consider looking at the MVC design pattern.

    Hopefully, this will help you out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。