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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.