duanhe1903 2014-02-08 20:41
浏览 41
已采纳

PHP查询MySQL查询没有正确递增/递减

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

?>
  • 写回答

2条回答 默认 最新

  • doubipeng1336 2014-02-08 22:41
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输