Jump to content

need help with filtering


nbbcj

Recommended Posts

hay all

 

here is my prob i have a page that show images and info from a db shows ok in a 3x3 grid but now i want to have a list at the side to filter the query to only show the enters with the right cat filter

 

ie index.php?cat=cars to only show the images of cars from the db

 

i have a field called cat in my db i have tryed this

 

$query="SELECT * FROM `shaun` WHERE `cat` ='".$_GET['cat']."'";

as this worked with an other page i have but errors this time  and no display on page

 

or can i do it where if the val is set in the url (ie. ?cat=filter) then run the above query if no val is sent via the url then to run a basic query

$query="SELECT * FROM `shaun`"

 

here is my page code

<table border="1" ALIGN="center">
<tr>
	<td>Filters<p><a href="?cat=filter1">•Filter1</a><br>•	Filter2<br>•Filter3</td>
	<td><table cellspacing="3" cellpadding="3">
<?php
error_reporting(E_ALL);

include ("includes/db_config.php");

mysql_connect($db_hostname,$db_username,$db_password);
@mysql_select_db($db_database) or die( "Unable to select database");
$query="SELECT * FROM `shaun`";

$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); 
if($result && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($result))        
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // make sure we have a valid product
       if($pro_name != "" && $pro_name != null)
          echo "<td><a href=\"detalis.php?id=$id\"><img src=\"/project/thum/$thumbnail\" alt=\"$thumbnail\" height=\"150\" width=\"150\" /><br>$pro_name<br></a>$short_details $cat</td>";
    
       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i > 0){    for($j=$i; $j<$max_columns;$j++) echo "<td> </td>";   echo '</tr>';} ?></table></td>
</tr>
</table>

 

Any help i would be greatful

 

 

nbbcj

 

>>>>>>>. edit the address in question is http://nbbcj.co.uk/project/

Link to comment
https://forums.phpfreaks.com/topic/253858-need-help-with-filtering/
Share on other sites

<table border="1" ALIGN="center">
<tr>
	<td>Filters<p><a href="?cat=filter1">•Filter1</a><br>•	Filter2<br>•Filter3</td>
	<td><table cellspacing="3" cellpadding="3">
<?php
error_reporting(E_ALL);

include ("includes/db_config.php");

mysql_connect($db_hostname,$db_username,$db_password);
@mysql_select_db($db_database) or die( "Unable to select database");

if(isset($_GET['cat']) && !empty($_GET['cat'])){
$escape_cat = mysql_real_escape_string($_GET['cat']);
$query="SELECT * FROM `shaun` WHERE `cat` ='".$escape_cat."'";
}else{
$query="SELECT * FROM `shaun`";
}

$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); 
if($result && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($result))        
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // make sure we have a valid product
       if($pro_name != "" && $pro_name != null)
          echo "<td><a href=\"detalis.php?id=$id\"><img src=\"/project/thum/$thumbnail\" alt=\"$thumbnail\" height=\"150\" width=\"150\" /><br>$pro_name<br></a>$short_details $cat</td>";
    
       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i > 0){    for($j=$i; $j<$max_columns;$j++) echo "<td> </td>";   echo '</tr>';} ?></table></td>
</tr>
</table>

 

you can also set up a default category if none is in the url

$escape_cat = mysql_real_escape_string($_GET['cat']);

if(!isset($_GET['cat']) || empty($_GET['cat'])){
$escape_cat = "cars";
}

$query="SELECT * FROM `shaun` WHERE `cat` ='".$escape_cat."'";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.