steveginer Posted October 31, 2006 Share Posted October 31, 2006 Hi,Is there any way you can make your WHERE clause conditional upon which variables are passed from the referring form? Can one embed php tags within a query, if not what is the general approach to a problem like this?It works OK in coldfusion but can't seem to figure out how to recreate this in php. Thanks. Here's the coldfusion code.<cfquery name="xxxx" datasource="xxxx" username="xxxx" password="xxxx">SELECT DISTINCT style.style, style.desc, style.groupFROM styleWHERE 0=0<cfif URL.style IS NOT "">AND style.style = '#URL.style#'</cfif><cfif URL.desc IS NOT "">AND style.desc like '#URL.desc#'</cfif><cfif URL.group IS NOT "">AND style.group = '#URL.group#'</cfif><cfif URL.OTS IS NOT "">AND stktot-ordtot+wiptot>#URL.OTS#</cfif><cfif URL.IMMED IS NOT "">AND stktot-ordtot>#URL.IMMED#</cfif><cfif URL.makebuy IS NOT "">AND style.makebuy = '#URL.makebuy#'</cfif></cfquery> Quote Link to comment https://forums.phpfreaks.com/topic/25733-conditional-select-statement/ Share on other sites More sharing options...
arianhojat Posted October 31, 2006 Share Posted October 31, 2006 u mean soemthing like this where Query string depends on what form values are submitted??$wantsRedItems = $POST['red'];$Query = 'SELECT * FROM shirts';if($wantsRedItems )$Query .= ' WHERE color=red';//...etc Quote Link to comment https://forums.phpfreaks.com/topic/25733-conditional-select-statement/#findComment-117460 Share on other sites More sharing options...
fenway Posted October 31, 2006 Share Posted October 31, 2006 That's the right idea... basically, you have to generate your where clause on the fly. Quote Link to comment https://forums.phpfreaks.com/topic/25733-conditional-select-statement/#findComment-117494 Share on other sites More sharing options...
steveginer Posted October 31, 2006 Author Share Posted October 31, 2006 Is the period after $Query correct syntax?...meaning $Query . or is it the semicolon that tells php that the query is being continued or built on the fly...still not clear $Query = 'SELECT * FROM shirts';if($wantsRedItems )$Query .= ' WHERE color=red'; Quote Link to comment https://forums.phpfreaks.com/topic/25733-conditional-select-statement/#findComment-117539 Share on other sites More sharing options...
fenway Posted October 31, 2006 Share Posted October 31, 2006 . is the concatenation operator. Quote Link to comment https://forums.phpfreaks.com/topic/25733-conditional-select-statement/#findComment-117553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.