Jump to content

an IF statement for changing query???


shane85

Recommended Posts

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

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());
}

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.