简体   繁体   English

PHP分页问题

[英]Problem with PHP Pagination

I am having a problem with my code ( sorry its a lot, but its the only way I knew to show you, its basically just a select statement from a table) that shows a page link but doesnt change the page results. 我的代码有问题(非常抱歉,但这是我知道向您显示的唯一方法,它基本上只是一个表中的select语句),它显示页面链接,但不会更改页面结果。 Basically I set it for example at 1 result per page but it shows all the results but still shows a link at the top to go to the next page. 基本上,我将其设置为每页1个结果,但它显示了所有结果,但仍在顶部显示一个链接,可转到下一页。 The next page just shows the same. 下一页仅显示相同内容。 I'm a PHP beginner so any help would be greatly appreciated. 我是PHP的初学者,所以任何帮助将不胜感激。

Thanks! 谢谢!

<?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-->";

        }

Michael, I tried what you did but I might have done it wrong...Here is my code before the where statement 迈克尔,我尝试了您的操作,但可能做错了...这是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>";
              }
            }

echo""; 回声””;

The problem is that you always fetch the same result set and then just output all of it, no matter which page is currently "active". 问题在于,无论哪个页面当前处于“活动”状态,您总是会获取相同的结果集,然后仅输出所有结果集。 If you want to paginate, you'll probably want to use something like a LIMIT clause in your SQL query (eg LIMIT 20,10 to return 10 records starting from offset 20 (zero-based, ie record number 21)). 如果要分页,则可能要在SQL查询中使用LIMIT子句(例如LIMIT 20,10从偏移量20(从零开始,即记录编号21))返回10条记录。

What you need to do is add something like: 您需要做的是添加如下内容:

$page = 1; $ page = 1;

$results_per_page = 10; $ results_per_page = 10;

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

$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 $record_start,$results_per_page") or trigger_error(mysql_error()); $ result = mysql_query(“ SELECT * FROM gj,其中名称类似'%$ q%'或cat1类似'%$ q%'或cat2类似'%$ q'或cat3类似'%$ q'的名称按名称LIMIT $ record_start ,$ results_per_page“)或trigger_error(mysql_error());

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

Check Above Link for Code, May it help you 检查上方链接以获取代码,可能对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM