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()); 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()); } 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 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? 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') 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
Archived
This topic is now archived and is closed to further replies.