how to add multiple filters to pagination?

Posted by ClarkSKent on Stack Overflow See other posts from Stack Overflow or by ClarkSKent
Published on 2010-04-26T22:45:40Z Indexed on 2010/04/26 22:53 UTC
Read the original article Hit count: 227

Filed under:
|

Hey, I am trying to allow multiple filters to be selected for my pagination script.

So in my pagination, when a user clicks the 'marketing' button(link) it queries the database just for the category that = marketing. The same goes for the other 2 filter buttons as seen in the script below. (automotive, sports).

The problem is, I want to be able to select multiple filters like only marketing and auomotive or automotive and sports, for example if I click the marketing filter and then the automotive, it would display the categories that equal marketing, and automotive.

I have no idea how to accomplish this, so I have come to the experts to help me out.

This is the script I am working on (I am very new to PHP and programming in general):

<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>";

?>

© Stack Overflow or respective owner

Related posts about php

Related posts about pagination