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 4006748
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:33:06+00:00 2026-05-20T08:33:06+00:00

This method takes search a search keyword and parsed mysql query, and rewrites the

  • 0

This method takes search a search keyword and parsed mysql query, and rewrites the where expression to include LIKE %keyword%.

It works well, but I dont know if its good or bad practice to have a method with this many loops…

private function build_where($query_array, $options)  
{  
    //add WHERE starting point  
    $where = '';        

    if(!empty($query_array['WHERE']))
    {
        //build where array
        $where_array = $query_array['WHERE'];   

        //start the where
        $where .= 'WHERE ';

        //get columns array
        $columns_array = $this->build_columns_array($query_array);

        //if there is a search string           
        if(!empty($options['sSearch']))
        {
            //check for enabled columns
            $i = 0;
            $columns_length = count($columns_array);
            for($i; $i < intval($columns_length); $i++)
            {
                //create the options boolean array
                $searchable_columns['bSearchable_'.$i] = $options['bSearchable_'.$i];
            }

            //loop through searchable_columns for true values
            foreach($searchable_columns as $searchable_column_key => $searchable_column_val)
            {
                if($searchable_column_val == true)
                {
                    //get an integer from the searchable_column key
                    $column_id = preg_replace("/[^0-9]/", '', $searchable_column_key);

                    //lookup column name by index
                    foreach($columns_array as $columns_array_key => $columns_array_val)
                    {
                        //if the $columns_array_key matches the $column_id
                        if($columns_array_key == $column_id)
                        {
                            //loop to build where foreach base expression
                            $i = 0;
                            $where_length = count($where_array);
                            for($i; $i < intval($where_length); $i++)
                            {
                                //append the existing WHERE Expressions
                                $where .= $where_array[$i]['base_expr'];
                            }                               

                            //append the LIKE '%$options['sSearch'])%'
                            $where .= ' AND '.$columns_array_val." LIKE '%".$options['sSearch']."%' OR ";
                        }
                    }   
                }
            }
            //remove the last OR
            $where = substr_replace($where, "", -3);                                    
        }
        else
        {
            //loop to build where
            $i = 0;
            $where_length = count($where_array);
            for($i; $i < intval($where_length); $i++)
            {
                $where .= $where_array[$i]['base_expr'];
            } 
        }            
    }

    //print_r($where_length);
    return $where;
}
  • 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-20T08:33:06+00:00Added an answer on May 20, 2026 at 8:33 am

    Breaking up methods is not primarily about reuse. Doing so can make code easier to read, test, and maintain. Clear method names can also substitute for inline comments. This method does two high-level things which could be separated: building a where clause with options and without options. Another hint for me is that the logic that builds the where clause with options looks meaty enough to warrant its own method.

    private function build_where($query_array, $options) {
        if(!empty($query_array['WHERE'])) {
            $where_array = $query_array['WHERE'];
            $columns_array = $this->build_columns_array($query_array);
            if (empty($options['sSearch'])) {
                return $this->build_where_with_options($where_array, $columns_array, $options);
            }
            else {
                return $this->build_where_without_options($where_array, $columns_array);
            }
        }
        else {
            return '';
        }
    }
    

    Now you can quickly scan build_where() to see that there are three possible forms the where clause may take and when along with the input each form needs to produce its result.

    Here are some minor improvements you can make throughout your code:

    • count() returns an integer and doesn’t need the intval() calls in your for loops. Even if you left those in, it would be better to apply the call outside the loop so its done only once as it yields the same value each time.
    • if($searchable_column_val == true) is equivalent to if($searchable_column_val) since both cast $searchable_column_val to a boolean value and the latter passes when that casted boolean value equals true.
    • $where = substr_replace($where, "", -3) can be replace with $where = substr($where, 0, -3) and is a little clearer.
    • Instead of looping through an array looking for a specific key you can take advantage of PHP’s arrays by simply grabbing the value with that key.

    For the last one, this code

    foreach($columns_array as $columns_array_key => $columns_array_val)
    {
        //if the $columns_array_key matches the $column_id
        if($columns_array_key == $column_id)
        { ... }
    }
    

    can be replaced by this

    $columns_array_val = $columns_array[$column_id];
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this search method: public List<Employeees> AutoSuggestEmployeee(string keyword, long employeeeTypeId, int count) {
For example, this method from NSCalendar takes a bitmask: - (NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate
In this case, the Visual Studio designer generates a method which takes the parameter
I'm wondering how to go about testing this. I have a method that takes
This method works as expected - it creates a JTree with a root node
I need to write a tree search method which takes a type parameter T
I am trying to call a ruby method from erb file. This method takes
I am search for the right signature of a method that takes a function
Take this example Proc: proc = Proc.new {|x,y,&block| block.call(x,y,self.instance_method)} It takes two arguments, x
Take this method /** * @return List of group IDs the person belongs to

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.