Alexhoward Posted June 10, 2008 Share Posted June 10, 2008 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 https://forums.phpfreaks.com/topic/109589-solved-im-stuck-display-table-from-mysql-where-dropdowns-equal/ Share on other sites More sharing options...
webent Posted June 10, 2008 Share Posted June 10, 2008 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 https://forums.phpfreaks.com/topic/109589-solved-im-stuck-display-table-from-mysql-where-dropdowns-equal/#findComment-562192 Share on other sites More sharing options...
Alexhoward Posted June 11, 2008 Author Share Posted June 11, 2008 Hi thanks for the reply! however i'm getting an unextected } any ideas...? cheers Link to comment https://forums.phpfreaks.com/topic/109589-solved-im-stuck-display-table-from-mysql-where-dropdowns-equal/#findComment-562950 Share on other sites More sharing options...
Alexhoward Posted June 11, 2008 Author Share Posted June 11, 2008 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 https://forums.phpfreaks.com/topic/109589-solved-im-stuck-display-table-from-mysql-where-dropdowns-equal/#findComment-563015 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.