【问题标题】:How do I add a filter button to this pagination?如何向此分页添加过滤器按钮?
【发布时间】:2010-04-26 11:13:20
【问题描述】:

嘿,我想添加一个按钮(链接),点击它会过滤分页结果。

我是 php 新手(和一般编程),想添加一个按钮,如“汽车”,点击它时会更新我的分页脚本中的 2 个 mysql 查询,见此处:

如您所见,汽车类别是硬编码的,我希望它是动态的,因此当点击链接时,它会将 id 或类放在查询的类别部分中。

1:

$record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='automotive'"));

2:

$get = mysql_query("SELECT * FROM explore WHERE category='automotive' LIMIT $start, $per_page");

这是我正在使用的整个当前 php 分页脚本:

<?php

    //connecting to the database
    $error = "Could not connect to the database";
    mysql_connect('localhost','root','root') or die($error);
    mysql_select_db('ajax_demo') or die($error);

    //max displayed per page
    $per_page = 2;

    //get start variable
    $start = $_GET['start'];

    //count records
    $record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='automotive'"));

    //count max pages
    $max_pages = $record_count / $per_page; //may come out as decimal

    if (!$start)
       $start = 0;

    //display data
    $get = mysql_query("SELECT * FROM explore WHERE category='automotive' LIMIT $start, $per_page");
    while ($row = mysql_fetch_assoc($get))
    {
     // get data
     $name = $row['id'];
     $age = $row['site_name'];

     echo $name." (".$age.")<br />";

    }

    //setup prev and next variables
    $prev = $start - $per_page;
    $next = $start + $per_page;

    //show prev button
    if (!($start<=0))
           echo "<a href='pagi_test.php?start=$prev'>Prev</a> ";

    //show page numbers

    //set variable for first page
    $i=1;

    for ($x=0;$x<$record_count;$x=$x+$per_page)
    {
     if ($start!=$x)
        echo " <a href='pagi_test.php?start=$x'>$i</a> ";
     else
        echo " <a href='pagi_test.php?start=$x'><b>$i</b></a> ";
     $i++;
    }

    //show next button
    if (!($start>=$record_count-$per_page))
           echo " <a href='pagi_test.php?start=$next'>Next</a>";

    ?>

【问题讨论】:

    标签: php pagination


    【解决方案1】:

    带有 2 个链接/类别的示例:

    <a href='script.php?category=automotive'>automotive</a> <a href='script.php?category=sports'>sports</a>
    

    script.php 内:

    $category = mysql_real_escape_string($_GET['category']);
    $record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='$category'"));
    ...
    $get = mysql_query("SELECT * FROM explore WHERE category='$category' LIMIT $start, $per_page");
    

    【讨论】:

    • 这类作品。它转到第一页,但是当我单击 [2] 之类的另一个页码时,什么也没有显示。关于如何解决这个问题的任何想法?
    • nvm,当点击项目时,我在 url 之后添加了 category=$category&。感谢您的帮助!
    【解决方案2】:

    编辑:我只是为了回应 OP 说他是编程新手而发布了这个答案。良好的卫生习惯在开始时很容易学会并成为习惯,或者在很久以后就很难养成习惯。

    http://us2.php.net/manual/en/security.database.sql-injection.php

    还请阅读这篇关于数据库安全性的文章。将您的查询编写为:

    $start = mysql_real_escape_string($_GET['start']);
    settype($start, 'integer');
    $category = mysql_real_escape_string($_GET['category']);
    $record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='%s'", $category));
    ...
    $get = mysql_query("SELECT * FROM explore WHERE category='%s' LIMIT %d, %d", $category, $start, $per_page);
    

    编写额外的安全代码总是值得的,即使只是为了练习。数据库的基本安全规则是:永远不要相信用户输入,总是检查生成的输出。由于输入来自查询字符串并针对数据库运行,因此必须对其进行过滤。

    【讨论】:

    • 总是很高兴分享安全信息,他们多年来一直在用它打败我。如果我能帮助一个人避免头痛,那我就成功了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2011-02-27
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多