Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6922813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:27:18+00:00 2026-05-27T10:27:18+00:00

I have been happy just adding an if statement here and there to a

  • 0

I have been happy just adding an if statement here and there to a page on my site to produce a data list but now its in the 000’s of lines of code and its messy.

I’m sure there is a better logic to what I am doing so any help would be good.

This is my logic (or how is should work)

check access lvl and add to sql - `accesslvl` = '1' 
Check if get_status is set - if set add to sql
check if get_product is set - if set add to sql
check if get_compnay is set - if set add to sql
check if get_datefrom is set - if set add to sql
check if get_dateto is set - if set add to sql

run query

now i have used lots of nested divs to work out if the gets are set, and if not dont and them to the sql, also it has to work out weather a WHERE or AND is needed.

I found this post creating a mysql search string dynamically?

And I was thing maybe this could help but not sure.
Example of my code.

if (isset($_GET['product'])&& $_GET['product'] >0){
        $product = $_GET['product'];
        if($a == 1){
            $sql_fields ="WHERE `Status_ID` = '$status' AND `Product` = '$product'";
        }
        else {
                $sql_fields ="WHERE `Product` = '$product'";
        }
        $b++;
    }
if (isset($_GET['company'])&& $_GET['company'] >0){
        $company = $_GET['company'];
        if($a == 0 && $b == 0){
                $sql_fields ="WHERE `Company_ID` = '$company'";
            }
        else if($a == 0 && $b == 1){
            $sql_fields ="WHERE `Product` = '$product' AND `Company_ID` = '$company'";
        }
        else if($a == 1 && $b == 0){
            $sql_fields ="WHERE `Status_ID` = '$status' AND `Company_ID` = '$company'";
        }
        else {
                $sql_fields ="WHERE `Status_ID` = '$status' AND `Product` = '$product' AND `Company_ID` = '$company' ";
        }
        $c++;
    }
if ($a == 0 && $b == 0 && $c == 0){
        $sql_fields =" ";
    }
if (isset($_GET['date_from']) && $_GET['date_from'] >0 && isset($_GET['date_to'])&& $_GET['date_to'] >0){
        if ($access_level == 1){
            if ($a == 0 && $b == 0 && $c == 0){
            $search_date = "WHERE `Date_added` >= '$date_from' AND `Date_added` <= '$date_to'";
            }
            else {
            $search_date = "AND `Date_added` >= '$date_from' AND `Date_added` <= '$date_to'";
            }
    }
if ($access_level ==2 or $access_level ==3){
            if ($a == 0 && $b == 0 && $c == 0){
            $search_date = " `Date_added` >= '$date_from' AND `Date_added` <= '$date_to'";
            $and = "AND";
            }
            else {
            $search_date = " `Date_added` >= '$date_from' AND `Date_added` <= '$date_to'";
            $and = "AND";
            }
        }

    }
else {
        $search_date = "";
        $and = "";
}

Here is my revised code!

$fields = array();

if (isset($_GET['status']) && $_GET['status'] >0){
    $status = $_GET['status'];
    $fields['Status_ID']= $_GET['status'];
    }
if (isset($_GET['product']) && $_GET['product'] >0){
    $product = $_GET['product'];
    $fields['Product_ID']= $_GET['product'];
    }
if (isset( $_GET['company']) && $_GET['company'] >0){
    $company = $_GET['company'];
    $fields['Company_ID']= $_GET['company'];
    }
if (isset( $_GET['closer']) && $_GET['closer'] >0){
    $closer = $_GET['closer'];
    $fields['Closer']= $_GET['closer'];
    }
if (isset( $_GET['date_from']) && $_GET['date_from'] >0 && isset( $_GET['date_to']) && $_GET['product'] >0){
    $date_from = $_GET['date_from'];
    $date_from = $_GET['date_to'];
    }
$field_count = count($fields);
if ($field_count == 0){
    $sql = "";
}
else {
    $field_count --;
    $sql="";
    $i=0;
        foreach($fields as $k => $v) {
            if($i==0){
                $sql = "WHERE `$k`  = '$v'";
                }
            else{
            $sql .=" AND `$k`  = '$v'";
            }
    $i++;

    }
}
 echo $sql;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:27:19+00:00Added an answer on May 27, 2026 at 10:27 am

    Firstly, there’s no need for the conditional branches to determine the right SQL keyword to use (just use the joining keyword as the state variable instead). Also your code is vulnerable to SQL injection and may result in queries not using indexes effectively. Consider:

    $join='WHERE';
    $sql_filter='';
    // potentially te following field meta data could be stored in a DB or file...
    // or even selectively overriden at run time by the request...
    $qbe=array(
       array('get_name'=>'product',
             'db_name'=>'Product',
             'op' => '=',
             'type'=>'string'),
       array('get_name'=>'date_from',
             'db_name'=>'date_added',
             'op' => '>=',
             'type' =>'date'),
       array('get_name'=>'date_to',
             'db_name'=>'date_added',
             'op' => '<=',
             'type' =>'date')
       ....
    );
    foreach ($qbe as $f) {
        $sql_filter=add_filter($sql_filter, $join, $f['get_name'],
          $f['type'], $f['op'], $f['db_name']);
    }
    
    function add_filter($sql_filter, &$join, $get_name, $type, $op, $db_name)
    {
       $mod_filter=$sql_filter;
       if ($_GET[$get_name]) {
          switch(upper($type)) {
             case 'DATE':
                $mod_filter=$mod_filter 
                  . " $join $db_name $op " 
                  . date(strtotime($_GET[$get_name]), 'YmdHis');
                break;
             case 'NUMBER':
                $match=array();
                if (preg_match('/([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)/', $_GET[$get_name], $match) {
                  $mod_filter=$mod_filter 
                  . " $join $db_name $op " . $match[0];
                }
                break;
             default:
                $mod_filter=$mod_filter . " $join $db_name $op '" .
                    mysql_real_escape_string($_GET[$get_name]) . "'";
                break;
          }
          if ($mod_filter != $sql_filter) {
               $join='AND';
          }
          return $mod_filter;
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been a few articles and questions about how to do this but
I've been using RVM for quite some time now and have been very happy
Have been studying the file system related classes of Adobe AIR 1.5, but so
I've been working on creating seo friendly urls for my site and have done
Hi I have been editing my script with some forum help. I just need
I have been working with ASP.NET MVC for a couple of months now and
Most people have been here at some point or another - in your project,
Hi I have been using Emacs23 for some time now and find it a
I've been using Janrain engange for quite a while and have been quite happy
Have been looking at the MVC storefront and see that IQueryable is returned from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.