简体   繁体   中英

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. 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. The next page just shows the same. I'm a PHP beginner so any help would be greatly appreciated.

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

    <?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)).

What you need to do is add something like:

$page = 1;

$results_per_page = 10;

$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());

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

Check Above Link for Code, May it help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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