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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:56:38+00:00 2026-05-23T10:56:38+00:00

I have a page for browsing db records. The viewer can filter records by

  • 0

I have a page for browsing db records. The viewer can filter records by category, author, and tags. I’m using a form instead of url segments for filtering records (it feels more secure because I can validate inputs.)

For instance, when all form inputs are populated the query looks like this:

SELECT * FROM (`posts`) WHERE `category` = 'technolgy' AND `author` = 'lila' AND `tags` = 'ebook'

However if one input or more is empty, I get no results. For example:

SELECT * FROM (`posts`) WHERE `category` = '' AND `author` = 'lila' AND `tags` = '' 

I want the inputs to be optional so for example if just author name is entered, I can return records made by that author regardless of category and tags. How can I omit the and where clause if empty?

Note: or_where clause is not the solution because it doesn’t return a precise query if all filter inputs all filled.

My Model

function filter($form_values)
    {                       
        $query = $this->db->get('posts');           
        $this->db->where($form_values); //adds clause to all array items
        return $query->result();        
    }

The function parameter is an array with input values from my view. Example,

$form_values = array('category' => $category, 'author' => $author, 'tags' => $tags);

and my View

$form_values = array (                  
                'category' => $this->input->post('category'),
                'author'   => $this->input->post('author'),
                'tags'     => $this->input->post('tags')
            );

$this->Records_model->filter($form_values);    

I know that in Codeigniter if $_POST' are empty they are set to FALSE. Can that be used to achieve what I’m trying? I’m not sure if I’m on the right track

  • 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-23T10:56:38+00:00Added an answer on May 23, 2026 at 10:56 am

    You are correct that $this->input->post() will return FALSE if the $_POST value is not set. Unless you specifically want IS NULL to be part of the query (which I believe will happen by passing FALSE to param 2 of where(), not 100% sure), just filter out the empty values:

    function filter($form_values)
    {              
        $form_values = array_filter($form_values);
    
        // NOTE:
        // where() needs to be called first, or your query won't use the WHERE clause
    
        // You may need to make sure there is at least one value in $form_values
        if ( ! empty($form_values)) // however you wish to check for a value
        {
            $this->db->where($form_values); //adds clause to all array items
        }
    
        $query = $this->db->get('posts');           
    
        return $query->result();        
    }
    

    http://php.net/manual/en/function.array-filter.php

    The important part to note on array_filter():

    If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have page which is redirected from htaccess. now I can pass the German
I need something simple; I have page where a user clicks an author to
I have a page using <ul> lists for navigation (Javascript changes the styling to
I have a page upon which a user can choose up to many different
I have page which is desgined for map point browsing. I want to show
On my page I have a textarea where users can enter a list of
I have a custom .aspx web page that I use for browsing files on
I have a page designed for tabbed browsing. The tabs are <li> s, which
I have a link on the page that opens a contact form in a
I have a web page where you can customize your game character. In order

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.