I've got a page for browsing db records. The viewer can filter records by category, author, and tags. I am utilizing a form rather than url segments for blocking records (it feels safer because I'm able to validate inputs.)

For example, when all form inputs are populated the query appears like this:

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

If however one input or even more is empty, I recieve no results. For instance:

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

I would like the inputs to become optional so for instance if just author name is joined, I'm able to return records produced by that author no matter category and tags. How do i omit the and where clause if empty?

Note: or_where clause isn't the solution since it does not return an exact 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 definitely an array with input values from the 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 understand that in Codeigniter if $_POST' are empty they are going to FALSE. Can that be employed to achieve what I am trying? I am unsure if I am on course

You're factual that $this->input->post() will return FALSE when the $_POST value isn't set. Unless of course you particularly want IS NULL to participate the query (that we believe may happen by passing FALSE to param 2 of where(), not 100% sure), just remove 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 key part to notice on array_filter():

If no callback is provided, all records of input comparable to FALSE (see transforming to boolean) is going to be removed.