【问题标题】:Pagination Url parsing issue分页网址解析问题
【发布时间】:2013-03-12 08:58:33
【问题描述】:

我有一个看起来相当好用的分页脚本,我将它转换为 PDO,但在解析两个变量时遇到了问题。

页面的网址是

current-events.php?event_id=62

当我转到第 2 页时,网址显示为

urrent-events.php?page=2

但是它仍然显示第 1 页的结果,我缺少 event_id

问题似乎是我无法调用 event_id 和页面。

任何人都可以看到我的方式的错误吗?

谢谢

加里

                <?php 
            require_once("pdoconnect.inc.php");
            $event_id = $_GET['event_id'];
            ?>

            /////////////////////////////////////////////////////////
                $tbl_name="victims";        //your table name
                // How many adjacent pages should be shown on each side?
                $adjacents = 3;

                /* 
                   First get total number of rows in data table. 
                   If you have a WHERE clause in your query, make sure you mirror it here.
                */
                $query = "SELECT event_id  FROM $tbl_name WHERE event_id = '$event_id' ";

                $q = $conn->prepare($query);
                $q->execute();
                $total_pages = $q->rowCount();
                echo $total_pages;

                //$total_pages = mysql_fetch_array(mysql_query($query));
                //$total_pages = $total_pages[num];

              //?event_id=$event_id
                $targetpage = "current-events.php";     //your file name  (the name of this file)
                                    //how many items to show per page
                            $limit = 10;                

              $page = $_GET['page'];




                if($page) 
                    $start = ($page - 1) * $limit;          //first item to display on this page
                else
                    $start = 0;                             //if no page var is given, set start to 0

                /* Get data. */
                $sql = "SELECT * FROM $tbl_name WHERE event_id = '$_GET[event_id]' ORDER BY lname ASC LIMIT $start, $limit ";
                    $result = $conn->prepare($sql);
                    $result->execute();
                //$result = mysql_query($sql);

                /* Setup page vars for display. */
                if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
                $prev = $page - 1;                          //previous page is page - 1
                $next = $page + 1;                          //next page is page + 1
                $lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
                $lpm1 = $lastpage - 1;                      //last page minus 1

                /* 
                    Now we apply our rules and draw the pagination object. 
                    We're actually saving the code to a variable in case we want to draw it more than once.
                */
                $pagination = "";
                if($lastpage > 1)
                {   
                    $pagination .= "<div class=\"pagination\">";
                    //previous button
                    if ($page > 1) 
                        $pagination.= "<a href=\"$targetpage?page=$prev\">� previous</a>";
                    else
                        $pagination.= "<span class=\"disabled\">� previous</span>"; 

                    //pages 
                    if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
                    {   
                        for ($counter = 1; $counter <= $lastpage; $counter++)
                        {
                            if ($counter == $page)
                                $pagination.= "<span class=\"current\">$counter</span>";
                            else
                                $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                        }
                    }
                    elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
                    {
                        //close to beginning; only hide later pages
                        if($page < 1 + ($adjacents * 2))        
                        {
                            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                            {
                                if ($counter == $page)
                                    $pagination.= "<span class=\"current\">$counter</span>";
                                else
                                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                            }
                            $pagination.= "...";
                            $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                            $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
                        }
                        //in middle; hide some front and some back
                        elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
                        {
                            $pagination.= "<a href=\"$targetpage?page=1\"> 1 </a>";
                            $pagination.= "<a href=\"$targetpage?page=2\"> 2 </a>";
                            $pagination.= "...";
                            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                            {
                                if ($counter == $page)
                                    $pagination.= "<span class=\"current\">$counter</span>";
                                else
                                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                            }
                            $pagination.= "...";
                            $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                            $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
                        }
                        //close to end; only hide early pages
                        else
                        {
                            $pagination.= "<a href=\"$targetpage?page=1\"> 1 </a>";
                            $pagination.= "<a href=\"$targetpage?page=2\"> 2 </a>";
                            $pagination.= "...";
                            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                            {
                                if ($counter == $page)
                                    $pagination.= "<span class=\"current\">$counter</span>";
                                else
                                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                            }
                        }

                    }

                    //next button
                    if ($page < $counter - 1) 
                        $pagination.= "<a href=\"$targetpage?page=$next\"> next  � </a>";
                    else
                        $pagination.= "<span class=\"disabled\"> next � </span>";
                    $pagination.= "</div>\n";       
                }

            ?>
            <table>

【问题讨论】:

  • 请注意:您仍然容易受到 sql 注入的影响。使用 pdo 不会神奇地使您免受注射。了解如何进行适当的准备查询。

标签: php pdo pagination


【解决方案1】:

我认为第一个$query 中的$event_id 是一个空变量。
您可以通过以下方式解决问题:

$event_id = $_GET['event_id'];

(那么您必须更改第二个查询并将$event_id而不是$_GET[event_id]

但请注意@itachi 所说的 SQL INJECTION


我认为当您单击分页链接时会丢失一个参数,您必须在网址中添加event_id
分页代码比较长,但是可以通过搜索$targetpage?page替换成$targetpage?event_id=$event_id&amp;page解决问题

【讨论】:

  • Guglielmo - 感谢您的回复。它没有包含在我的示例代码中,但是我有 $event_id = $_GET['event_id'];在文件中,我阅读了您从 php.net 引用的页面,他们建议使用 prepare() 与 quote() “如果您使用此函数构建 SQL 语句,强烈建议您使用 PDO::prepare( ) 来准备带绑定参数的 SQL 语句,而不是使用 PDO::quote() 将用户输入插入到 SQL 语句中。带绑定参数的准备好的语句不仅更便携,更方便,不受 SQL 注入的影响,"
  • @Gary 抱歉,我删除了链接(而且我从未使用过引用,但谢谢)。能贴出完整的代码吗?
  • 我已经编辑了上面的代码,如果不是全部的话,大部分都是无关的 html/css。谢谢您的帮助。加里
  • @Gary 好的,完整的代码我的意思是完整的 PHP 代码。也许您可以重新编辑您的问题,删除 HTML 以使其更具可读性。顺便说一句,我已经编辑了答案。
  • 非常感谢,就是这样。我上去为将来可能会遇到这篇文章的人编辑了额外的代码。谢谢您的帮助。加里
猜你喜欢
  • 2016-12-01
  • 1970-01-01
  • 2013-02-02
  • 2011-04-05
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多