0
$category = get_option('category');

// checks to see if subcategory is enabled in the option page  
if ( get_option('enable_subcategory') == 'y' ):   

  $subcategory = get_option('subcategory');

endif;

$qry = mysql_query("SELECT car,number FROM ".$table." 
                    WHERE category = '$category' AND subcategory = '$subcategory' 
                    ORDER BY RAND() LIMIT 20");

How can i write the $qry so the " AND subcategory = '$subcategory' " will disappear if subcategory option isn't enable?

0

1 Answer 1

2

Added $subSql var:

<?

  $category = get_option('category');

  if ( get_option('enable_subcategory') == 'y' ):   // checks to see if subcategory is enabled in the option page  

  $subcategory = get_option('subcategory');
  $subSql = "AND subcategory = '$subcategory'";

  endif;

  $qry = mysql_query("SELECT car,number FROM ".$table." WHERE category = '$category' $subSql ORDER BY RAND() LIMIT 20");

?>
Sign up to request clarification or add additional context in comments.

3 Comments

This looks interesting, and if there is no value $subSql is empty, cool;)
You can do it without the extra variable if you drop the $subcategory line and change the next line to $subSql = "AND subcategory = '" . get_option('subcategory') . "'";
@webmasters not empty nor cool. To be empty, this variable should be defined first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.