简体   繁体   中英

PHP Pagination of MySQL Query not incrementing/decrementing properly

I'm having trouble with the follow PHP which paginates the results of a MySQL query.

When I go to webpagename.php with the first page of the results and click Previous, the browser changes to webpagename.php?page=-1 and shows the first page of results again. If I click Previous again, it changes to webpagename.php?page=-2 and shows Page 1 of the results again, etc.

When I go to webpagename.php with the first page of the results and click Next, the browser changes to webpagename.php?page=1 and shows the first page of results again. I then have to hit Next a second time to move to Page 2.

When I go to the last page of the results - Page 8 - and click Next, the browser changes to webpagename.php?page=9 and shows Page 1 of the results. If I click Next again, it shows webpagename.php?page=10 and shows Page 1 of the results again, etc.

Expected Results:

When on Page 1 and a user hits Previous, I would like the code to do nothing/not decrement. When on Page 8 - the last page of results, I would like the code to do nothing/not increment. Of course, I would also expect that if you hit Next from Page 1 that it doesn't display Page 1 a second time but rather goes to Page 2.

Your exact changes to this code to make it work properly are very much appreciated. Thank you for time.

<?php

mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());


// number of results to show per page
$per_page = 10;


// figure out the total pages in the database
$result = mysql_query("SELECT * FROM uc_users LEFT JOIN ent_dancers ON uc_users.id = ent_dancers.id WHERE ent_dancers.DancerYesNo = '1' AND ent_dancers.DancerEnabledYesNo = '1' ORDER BY uc_users.display_name ASC");

$total_results = mysql_num_rows($result);

$total_pages = ceil($total_results / $per_page);


// check if the 'page' variable is set in the URL (ex: webpagename.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))

{
        $show_page = $_GET['page'];


        // make sure the $show_page value is valid
        if ($show_page > 0 && $show_page <= $total_pages)

        {
                $start = ($show_page -1) * $per_page;

                $end = $start + $per_page; 

        }
        else

        {
                // error - show first set of results

                $start = 0;

                $end = $per_page; 

        }               
}

else
{

        // if page isn't set, show first set of results
        $start = 0;

        $end = $per_page; 

}

// display pagination

// display data in table

    echo "<div class='dancerbio'>";
    echo "<div class='uts-1'>";

// loop through results of database query, displaying them in the table 
for ($i = $start; $i < $end; $i++)

{
        // make sure that PHP doesn't try to show results that don't exist

        if ($i == $total_results) { break; }


        // echo out the contents of each row into a table



        $rowid = mysql_result($result, $i, 'id'); 

        echo "<div class='uts-1-1'><a class='bodytxt5' href='webpagename-details.php?userid=$rowid'>" . mysql_result($result, $i, 'display_name') . "</a></div>";


}

// close table>
    echo "<div class='ugen-1'></div>";
    echo "</div>";

$prev = $_GET['page'] - 1;

echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";

echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a>&nbsp;&nbsp;";

for ($i = 1; $i <= $total_pages; $i++)

{
        echo "<a class='bodytxt5' href='webpagename.php?page=$i'>$i</a>&nbsp;&nbsp;";

}

$next = $_GET['page'] + 1;

echo "&nbsp;<a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a>&nbsp;";
echo "</div>";


// pagination

?>

replace this:

 if (isset($_GET['page']) && is_numeric($_GET['page']))

 {
    $show_page = $_GET['page'];


    // make sure the $show_page value is valid
    if ($show_page > 0 && $show_page <= $total_pages)

    {
            $start = ($show_page -1) * $per_page;

            $end = $start + $per_page; 

    }
    else

    {
            // error - show first set of results

            $start = 0;

            $end = $per_page; 

    }               
  }
  else
  {

    // if page isn't set, show first set of results
    $start = 0;

    $end = $per_page; 

  }

by this:

   if (isset($_GET['page']) && is_numeric($_GET['page']))
   {
       $show_page = $_GET['page'];

       if ($show_page > 0 && $show_page <= $total_pages)
       {
            $start = ($show_page -1) * $per_page;

            $end = $start + $per_page; 
       }
       elseif ($show_page > $total_pages)
       {
            $show_page=$total_pages;
            $start = ($show_page -1) * $per_page;

            $end = $start + $per_page; 

       }
       else {
            $show_page=1;
            $start = 0;

            $end = $per_page; 
       }
 }
 else {
    $show_page=1;
    $start = 0;

    $end = $per_page;
 }

then :

$prev=$show_page-1;
$next=$show_page+1;

if($show_page>1){//this way previsous won't appear if you are at page 1 already
   //show previous div
}

if($show_page<$total_pages){ //this way next won't appear unless you are not at the last page
   //show next div
}

Make a variable $page set it equal to $_GET['page'].

if(isset($_GET['page'])){
    $page = $_GET['page'];
}
else{
    $page = 1;
}

you need to put a condition before echoing previous link to check whether $_GET['page'] is set or not and is greater than 1. Like this:

if($page!=($start+1)){
   $prev = $page - 1;
   echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
   echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a>&nbsp;&nbsp;";
}

Add another condition for next

if($page!=$total_pages)
{
    $next = $page+1
    echo "&nbsp;<a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a>&nbsp;";
    echo "</div>";
}

I hope your issue is solved.

if (isset($_GET['page']) && is_numeric($_GET['page']))
{
    $show_page = $_GET['page'];

    // make sure the $show_page value is valid
    if ($show_page > 0 && $show_page <= $total_pages)
    {
            $start = ($show_page -1) * $per_page;

            $end = $start + $per_page; 
    }
    else
    {
            // error - show first set of results

            $start = 0;
            $end = $per_page; 
            $show_page=1
    }               
 }

 else
 {

    // if page isn't set, show first set of results
    $start = 0;
    $end = $per_page; 
    $show_page=1;
}

// display pagination

// display data in table

echo "<div class='dancerbio'>";
echo "<div class='uts-1'>";

// loop through results of database query, displaying them in the table 
for ($i = $start; $i < $end; $i++)
{
    // make sure that PHP doesn't try to show results that don't exist

    if ($i == $total_results) { break; }


    // echo out the contents of each row into a table



    $rowid = mysql_result($result, $i, 'id'); 

    echo "<div class='uts-1-1'><a class='bodytxt5' href='webpagename-details.php?userid=$rowid'>" . mysql_result($result, $i, 'display_name') . "</a></div>";


 }

 // close table>
echo "<div class='ugen-1'></div>";
echo "</div>";

if($show_page!=($start+1)){
   $prev = $page - 1;
   echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
   echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a>&nbsp;&nbsp;";
}

for ($i = 1; $i <= $total_pages; $i++)
{
        echo "<a class='bodytxt5' href='webpagename.php?page=$i'>$i</a>&nbsp;&nbsp;";
}

if($show_page!=$total_pages)
{
    $next = $page+1
    echo "&nbsp;<a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a>&nbsp;";
    echo "</div>";
}


// pagination

?>

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