dongtui2029 2013-03-01 06:34 采纳率: 0%
浏览 80

PHP分页问题mysql和php在所有页面上显示相同的数据

I am having issues with some pagination with data i get from a MySQL database in PHP.

My code is below. Basically what happens is it creates the right amount of pages however each page shows the same data and doesn't even show only 5 rows per page.

I'm really stuck. This is my first time trying pagination. Any help would be greatly appreciated.

        $pagenum = $_GET['pagenum'];

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

        //Count the number of results.
        $data = mysql_query("SELECT * FROM `articles` WHERE `content` = '' AND `requestedby` != '$id'") or die(mysql_error()); 
        $rows = mysql_num_rows($data);

        //Set the number of results to be displayed per page.
        $page_rows = 5;

        //This tells us the page number of our last page 
        $last = ceil($rows/$page_rows); 

         //this makes sure the page number isn't below one, or more than our maximum pages 
        if ($pagenum < 1) 
        { 
            $pagenum = 1; 
        }   elseif ($pagenum > $last) 
            { 
                $pagenum = $last; 
            }

        //This sets the range to display in our query 
        $max = 'LIMIT ' . ($pagenum - 1) * $page_rows .',' .$page_rows;

        //This is the query again, the same one... the only difference is we add $max into it.
        $data_p = mysql_query("SELECT * FROM `articles` WHERE `content` = '' AND `requestedby` != '$id' '$max'") or die(mysql_error());

        //Work out writers earnings based on prices.
        //100 Words - $1.25
        //300 Words - $2.50
        //500 Words - $4.00
        //700 Words - $5.50
        //1000 Words - $8.00
        $_100earnings = "0.65";
        $_300earnings = "1.25";
        $_500earnings = "2.50";
        $_700earnings = "3.00";
        $_1000earnings = "5.00";
    ?>
    <!-- main -->
    <div id="main">
    <center><h2>Write Articles</h2></center>
    <br />Available Projects:
    <table border="1">
        <tr>
            <td>Title:</td>
            <td>Length:</td>
            <td>Writers Earnings:</td>
        </tr>
        <?php
            //This is where you display your query results
            while($info = mysql_fetch_array($data_p))
            { 
                echo "<tr>";
                echo "<td>" . $info['keywords'] . "</td>";
                echo "<td>" . $info['length'] . "</td>";
                switch ($info['length'])
                {
                    case 100:
                        $writersearnings = $_100earnings;
                        break;
                    case 300:
                        $writersearnings = $_300earnings;
                        break;
                    case 500:
                        $writersearnings = $_500earnings;
                        break;
                    case 700:
                        $writersearnings = $_700earnings;
                        break;
                    case 1000:
                        $writersearnings = $_1000earnings;
                        break;
                }
                echo "<td>$" . $writersearnings . "</td>";
                //echo $info['Name']; 
                echo "</tr>";
            }

        ?>
    </table>
    <br /><br />
    <?php
        // This shows the user what page they are on, and the total number of pages
        echo " --Page $pagenum of $last-- <p>";


        // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.  
        if ($pagenum == 1) 
        {
        }   else 
            {
                 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
                 echo " ";
                 $previous = $pagenum - 1;
                 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";   
            } 

        //just a spacer
        echo " ---- ";

        //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
        if ($pagenum == $last) 
        {

        }   else 
            {
                 $next = $pagenum + 1;

                 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";

                 echo " ";

                 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
            }
  • 写回答

2条回答 默认 最新

  • duanchazhou6779 2013-03-01 06:37
    关注

    You need to specify LIMIT in your query LIMIT offset,count

    $data_p = mysql_query("SELECT * FROMarticlesWHEREcontent= '' ANDrequestedby!= '$id' '$max'") or die(mysql_error());

    Remove the ' quotes from '$max'

    $data_p = mysql_query("SELECT * FROM articles WHERE content= '' AND requestedby!= '".$id."' ".$max) or die(mysql_error());
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?