如何在分页中添加多个过滤器?

时间:2010-04-26 22:45:41

标签: php pagination

嘿,我正在尝试为我的分页脚本选择多个过滤器。

因此,在我的分页中,当用户点击“营销”按钮(链接)时,它只查询数据库中的=营销类别。其他2个过滤器按钮也是如此,如下面的脚本所示。 (汽车,运动)。

问题是,我希望能够选择多个过滤器,例如市场营销和汽车或汽车和体育,例如,如果我点击营销过滤器,然后点击汽车,它将显示与营销相同的类别,以及汽车

我不知道如何做到这一点,所以我来找专家来帮助我。

这是我正在处理的脚本(我对PHP和编程很新):

<h3>Filter results by:</h3>
<a href='pagi_test.php?category=marketing'>marketing</a>
<a href='pagi_test.php?category=automotive'>automotive</a>
<a href='pagi_test.php?category=sports'>sports</a>
<br />

<h3>Results:</h3>
<?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 = 3;

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

$category = mysql_real_escape_string($_GET['category']);
//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='$category'"));

//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='$category' LIMIT $start, $per_page");
?>
<table width="800px">
<?php
while ($row = mysql_fetch_assoc($get))
{
 // get data
 $id = $row['id'];
 $site_name = $row['site_name'];
 $site_description = $row['site_description'];
?>

<tr>
<td><?php echo $id; ?></td>
<td><?php echo $site_name; ?></td>
<td><?php echo $site_description; ?></td>
</tr>
<?php
}

//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?category=$category&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?category=$category&start=$x'>$i</a> ";
 else
    echo " <a href='pagi_test.php?category=$category&start=$x'><b>$i</b></a> ";
 $i++;
}

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

?>

1 个答案:

答案 0 :(得分:0)

您可以将它们作为一个复选框数组,然后在处理页面中对它们进行预处理,将它们转换为SQL IN子句。