shane85 Posted April 2, 2010 Share Posted April 2, 2010 currently im using a query to display all my results in ASC order... however, I want it so that if prospect_id is chosen, it will display in DESC order, however all the other ones I want to be displayed in ASC order when chosen. I tried the following statement but it didnt work...is there a better way to do this? or is this the proper way, just I have something wrong?? if ($orderby=prospect_id) { // display this query if prospect_id is chosen $prospectEntries = mysql_query("SELECT * from prospects ORDER BY ".mysql_real_escape_string($orderby)." DESC ".$queryLimit) or trigger_error(mysql_error()); } // if not prospect_id display other results in ASC order $prospectEntries = mysql_query("SELECT * from prospects ORDER BY ".mysql_real_escape_string($orderby)." ASC ".$queryLimit) or trigger_error(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/197419-an-if-statement-for-changing-query/ Share on other sites More sharing options...
ialsoagree Posted April 3, 2010 Share Posted April 3, 2010 You just forgot your else statement is all... if ($orderby=prospect_id) { // display this query if prospect_id is chosen $prospectEntries = mysql_query("SELECT * from prospects ORDER BY ".mysql_real_escape_string($orderby)." DESC ".$queryLimit) or trigger_error(mysql_error()); } else { // if not prospect_id display other results in ASC order $prospectEntries = mysql_query("SELECT * from prospects ORDER BY ".mysql_real_escape_string($orderby)." ASC ".$queryLimit) or trigger_error(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/197419-an-if-statement-for-changing-query/#findComment-1036210 Share on other sites More sharing options...
mikesta707 Posted April 3, 2010 Share Posted April 3, 2010 you also probably want your if to be if ($orderby==prospect_id) im assuming you want to do a comparison there, not an assignment Quote Link to comment https://forums.phpfreaks.com/topic/197419-an-if-statement-for-changing-query/#findComment-1036226 Share on other sites More sharing options...
shane85 Posted April 3, 2010 Author Share Posted April 3, 2010 Mike: your correct, I changed my code to the following if ($orderby==prospect_id) { $prospectEntries = mysql_query("SELECT * from prospects ORDER BY ".mysql_real_escape_string($orderby)." DESC ".$queryLimit) or trigger_error(mysql_error()); } else { $prospectEntries = mysql_query("SELECT * from prospects ORDER BY ".mysql_real_escape_string($orderby)." ASC ".$queryLimit) or trigger_error(mysql_error()); } while ($prospectEntry = mysql_fetch_array($prospectEntries)) { print_prospectEntry($prospectEntry); } however nothing changes...is it all correct? Quote Link to comment https://forums.phpfreaks.com/topic/197419-an-if-statement-for-changing-query/#findComment-1036314 Share on other sites More sharing options...
ialsoagree Posted April 3, 2010 Share Posted April 3, 2010 if ($orderby==prospect_id) Should probably be if ($orderby=='prospect_id') Quote Link to comment https://forums.phpfreaks.com/topic/197419-an-if-statement-for-changing-query/#findComment-1036487 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.