phatgreenbuds Posted December 27, 2006 Share Posted December 27, 2006 Consider the following code:[color=green][i]$ownerq = $_POST[Owner];$statusq = $_POST[Status];$typeq = $_POST[Type];$query = "SELECT * FROM crdmainWHERE reqowner='$ownerq'AND reqstatus='$statusq'AND reqtype='$typeq'";[/i][/color]The variable "$typeq" could be any of five different values selectable from a drop down. This works fine as is, and of course assuming a specific value is chosen. The problem I am having is how to add an "Any" value that will return a query for all five options. I thought I could use functions nested in "If" statements to jump between different query's but either it does not work or my syntax was wrong. Any ideas? Link to comment https://forums.phpfreaks.com/topic/31935-solved-query-troubles/ Share on other sites More sharing options...
bljepp69 Posted December 27, 2006 Share Posted December 27, 2006 Try something like this:[code]<?php$ownerq = $_POST[Owner];$statusq = $_POST[Status];$typeq = ($_POST[Type]=='Any') : '' : "AND reqtype='{$_POST[Type]}'";//this basically says that if $_POST[Type] is equal to the string "Any" then leave this whole thing empty,//otherwise make it equal to the string "AND reqtype='the value of $_POST[Type]'"$query = "SELECT * FROM crdmain WHERE reqowner='$ownerq' AND reqstatus='$statusq' $typeq";//by default, the query is made using only reqowner and reqstatus, the variable $typeq comes from above,//and can modify the query?>[/code] Link to comment https://forums.phpfreaks.com/topic/31935-solved-query-troubles/#findComment-148192 Share on other sites More sharing options...
phatgreenbuds Posted December 27, 2006 Author Share Posted December 27, 2006 It does not seem to like those colons. I am using an older 4.3.3 version of PHP so I am assuming its a version issue. Link to comment https://forums.phpfreaks.com/topic/31935-solved-query-troubles/#findComment-148195 Share on other sites More sharing options...
bljepp69 Posted December 27, 2006 Share Posted December 27, 2006 sorry, my mistake...change that line to:[code]$typeq = ($_POST[Type]=='Any') ? '' : "AND reqtype='{$_POST[Type]}'";[/code] Link to comment https://forums.phpfreaks.com/topic/31935-solved-query-troubles/#findComment-148196 Share on other sites More sharing options...
phatgreenbuds Posted December 27, 2006 Author Share Posted December 27, 2006 Nice! That did it. Now that I see it, it all makes sense. Just a little irritated that I had to ask for help and could not come up with it myself. Regardless thanks for the help. Link to comment https://forums.phpfreaks.com/topic/31935-solved-query-troubles/#findComment-148198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.