PHP分页问题

时间:2011-01-14 23:45:22

标签: php mysql pagination logic

我的代码出了问题(对不起它很多,但它是我知道的唯一方法,它基本上只是表中的一个select语句),它显示了一个页面链接,但没有改变页面结果。基本上我将它设置为例如每页1个结果,但它显示了所有结果,但仍显示顶部的链接转到下一页。下一页只显示相同的内容。我是一个PHP初学者,所以任何帮助都将非常感激。

谢谢!

<?php

            if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;
            $max_results = 1;
            $from = (($page * $max_results) - $max_results);
            REQUIRE('config.php');
            $q = mysql_real_escape_string(ucfirst(trim($_REQUEST['q'])));
            $result = mysql_query("SELECT * FROM gj WHERE name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' ORDER by name") or trigger_error(mysql_error());
            $rows = mysql_num_rows($result);
            if($rows == 0){

            }
            echo " <div id='title'>Search for &quot;$q&quot;<div class='righttitle'>$rows business";if($rows > 1){echo "es";}elseif($rows == "0"){echo "es";}echo" found";
            echo"<div id='pagenumbers'>";
            // (1) get the total number of results for your query
            // modify this to match the total results for the main query
            $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM gj where name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q'"),0); 

            // (2) Calculate total number of pages. Round up using ceil()
            $total_pages = ceil($total_results / $max_results); 

            if($total_results > $max_results)
            {
              // (3) build Previous link
              if($page > 1)
              {
                 $prev = ($page - 1);
                 echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$prev\">&lt;&lt; Prev</a> ";
              } 

              // (4) display page numbers
              for($i = 1; $i <= $total_pages; $i++)
              {
                 if($page == $i)
                 {
                    echo $i . " ";
                 }
                 else
                 {
                    echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$i\">$i</a> ";
                 }
              } 

              // (5) build Next Link
              if($page < $total_pages)
              {
                   $next = ($page + 1);
                   echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$next\">Next &gt;&gt;</a>";
              }
            }

  echo"</div></div></div>";
        while($row = mysql_fetch_array($result))
        {
        $id=$row['id'];
        $name=$row['name'];
        $phone=$row['phone'];
        $website=$row['website'];
        $city=$row['city'];
        $address=$row['address1'];
        $zipcode=$row['zipcode'];
        $sponsored = $row['sponsored'];
        $addressmap = preg_replace('/\s/', '+',$address);
        $citymap = preg_replace('/\s/', '+',$city);
        //Start While Loop
        echo"
        <div id='listing'>
            <div id='mainlisting'>";
            echo"
                <div class='name'>
                    <a href='./more.php?id=$id' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>$name</a> <div class='right'>$phone</div>
                </div>
                <div class='other'>
                    $address, $city, CO $zipcode 
|<a  target='_blank' href='http://maps.google.com/maps?        f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=$addressmap,+$city+CO&amp;&amp;&amp;ie=UTF8&amp;hq=&amp;hnear=$address,+$city,+Colorado+$zipcode&amp;safe=active&amp;&amp;&amp;t=h&amp;z=14&amp;iwloc=A&amp;output=embed' rel='lyteframe' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"' title='$name' rev='width: 500px; height: 500px; scrolling: no;'> See Map</a><br/>
                    <a href='#' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>";if($website != null){ echo "<a target='_blank' href='$website' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>Website</a> |";}echo" <a href='#' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>More Info</a>
                </div>
            </div>
        </div><!--/LISTING-->";

        }
迈克尔,我尝试过你所做的但是我可能做错了......这是我的代码在where语句之前

    <?php
        $page = 1; $total_pages = 9; $record_start = ($page * $total_pages) - $total_pages; 
            REQUIRE('config.php');
            $q = mysql_real_escape_string(ucfirst(trim($_REQUEST['q'])));
            $result = mysql_query("SELECT * FROM gj WHERE name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' ORDER by name  LIMIT 0,9") or trigger_error(mysql_error());
            $rows = mysql_num_rows($result);
            if($rows == 0){

            }
            echo " <div id='title'>Search for &quot;$q&quot;<div class='righttitle'>$rows business";if($rows > 1){echo "es";}elseif($rows == "0"){echo "es";}echo" found";
            echo"<div id='pagenumbers'>";
            // (1) get the total number of results for your query
            // modify this to match the total results for the main query
            $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM gj where name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q '"),0); 

            // (2) Calculate total number of pages. Round up using ceil()
            $alltotal_pages = ceil($total_results / $max_results); 

            if($total_results > $max_results)
            {
              // (3) build Previous link
              if($page > 1)
              {
                 $prev = ($page - 1);
                 echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$prev\">&lt;&lt; Prev</a> ";
              } 

              // (4) display page numbers
              for($i = 1; $i <= $alltotal_pages; $i++)
              {
                 if($page == $i)
                 {
                    echo $i . " ";
                 }
                 else
                 {
                    echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$i\">$i</a> ";
                 }
              } 

              // (5) build Next Link
              if($page < $alltotal_pages)
              {
                   $next = ($page + 1);
                   echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$next\">Next &gt;&gt;</a>";
              }
            }

回声 “”;

3 个答案:

答案 0 :(得分:1)

问题在于,无论哪个页面当前处于“活动状态”,您总是获取相同的结果集,然后只输出所有结果集。如果你想分页,你可能想在SQL查询中使用类似LIMIT子句的东西(例如LIMIT 20,10来返回从偏移量20开始的10条记录(从零开始,即记录号21) ))。

答案 1 :(得分:1)

您需要做的是添加以下内容:

  

$ page = 1;

     

$ results_per_page = 10;

     

$ record_start =($ page *   $ results_per_page) -   $ results_per_page;

     

$ result = mysql_query(“SELECT * FROM   gj WHERE名称LIKE'%$ q%'或cat1 LIKE   '%$ q%'或cat2 LIKE'%$ q'或cat3 LIKE   '%$ q'按名称LIMIT排序   $ record_start,$ results_per_page“)或   trigger_error(mysql_error());

答案 2 :(得分:0)

  

http://www.apnacode.com/php/simple-php-pagination/

检查代码的上方链接,可以帮助您