Jump to content

[SOLVED] I'm Stuck! Display table from mysql where dropdowns equal...


Alexhoward

Recommended Posts

Hi Guys,

 

This ones really got me...

 

I've got two drop down's populated from mysql; cat and subcat, which both post to the page. e.g www.website.com?cat=cat

 

They are on two seperate forms as subcat is poulated from $_GET['cat']

 

subcat has a hidden input containing cat so that when you post the subcat selection it also posts cat

e.g. www.website.com?cat=cat&subcat=subcat

 

I would then like to pull back the info out of my table based on these variables

 

so, i'm trying something like:

 

<?php
$query2=" SELECT * FROM table ".$_GET['cat']." ".$_GET['subcat']." ORDER BY ref DESC ";
$result2=mysql_query($query2);
?>

 

problem is i have to set the variables if they are not, else you get an error

 

so:

 

<?php
if(!isset($_GET['cat'])) { $_GET['cat'] = " "; }
if(!isset($_GET['subcat'])) { $_GET['subcat'] = " "; }


if(isset($_GET['cat'])) { $_GET['cat'] = "  WHERE cat = '".$_GET['cat']."'"; }
if(isset($_GET['subcat'])) { $_GET['subcat'] = " AND subcat = '".$_GET['subcat']."'"; }


$query2=" SELECT * FROM idea ".$_GET['cat']." ".$_GET['subcat']." ORDER BY ref DESC ";
$result2=mysql_query($query2);
?>

 

What i'm trying to achieve is to set them as nothing if they are not set, so the query will pull back everything from the table, problem is i am setting them by doing this, so when unselected cat will equal "WHERE cat = " and subcat will equal "AND subcat = "

 

Another issue is that i also have to do this for the the subcat dropdown as it has cat as a hidden input, so i'm already setting it there too...!

 

I seem to be stuck in this horrible loop!

 

can anyone help me out, or suggest anything?

 

Thanks is advance for taking the time to read this

Link to comment
Share on other sites

Would something like this work?...

 

$query=""; 
if(isset($_GET['cat'])){
$query=$query."cat='$_GET[cat]', ";
} 
if(isset($_GET['subcat'])){
$query=$query."subcat='$_GET[subcat]', ";
}
if ($query == "") {$query = 1} else {                      
    $query = substr_replace($query," ",-2);
    $sub_query = "SELECT * FROM idea WHERE ".$query. "  ORDER BY ref DESC";
        $sub_results = mysql_query($sub_query);
        if (!$sub_results) {
            $message  = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $sub_query;
            die($message);
        }
}

Link to comment
Share on other sites

Cheers mate,

 

This is how i'm doing it :

 

<?php

$query=""; 
$query=$query."cat=\"$_GET[cat]\" ";

if(isset($_GET['subcat'])){
$query=$query." AND subcat=\"$_GET[subcat]\" ";
}
if ($query == "") { $query = 1; } else {                      
    $sub_query = "SELECT * FROM idea WHERE ".$query. "  ORDER BY ref DESC";
    if(strlen($_GET['cat'])==0){
$sub_query = "SELECT * FROM idea ORDER BY ref DESC";
    } 
        $sub_results = mysql_query($sub_query);
        if (!$sub_results) {
            $message  = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $sub_query;
            die($message);
        }
}

?>

 

Thanks for your help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.