Making a dynamic query based on a cookie change I have a PHP function which sets/unsets something (category and tags) in the cookie and then makes a dynamic query based on it:

public function tagged(){

    $this-&gt;specific_tag  =  true;

    function unset_cookie($cookie_name) {
        setcookie($cookie_name, "", time() - 3600, '/');
        unset($_COOKIE[$cookie_name]);
    }

    function set_cookie($cookie_name, $cookie_value) {
        setcookie($cookie_name, $cookie_value, 2147483647, '/');
        $_COOKIE[$cookie_name] = $cookie_value;
    }

    if ( isset($_GET['c']) ) {
        if ( empty($_GET['c']) ) {
            unset_cookie('qanda_questions_category');
        } else {
            set_cookie('qanda_questions_category', $_GET['c']);
        }
    }

    if ( isset($_GET['t']) ) {
        if ( empty($_GET['t']) ) {
            unset_cookie('qanda_questions_tag');
        } else {
            set_cookie('qanda_questions_tag', $_GET['t']);
        }
    }

    $this-&gt;consider_category_tag_cookies();

    if ( $this-&gt;category_cookie || $this-&gt;tag_cookie ) {

        $query_where = $query_join = '';

        if ( $this-&gt;category_cookie ) {
            $query_where .= "AND qa.category = :c";
            $this-&gt;parameters[":c"] = $_COOKIE['qanda_questions_category'];
        }

        if ( $this-&gt;tag_cookie ) {
            $query_join  .= " INNER JOIN qanda_tags qt ON qt.qanda_id = qa.id
                              INNER JOIN tags ON tags.id = qt.tag_id";
            $query_where .= " AND tags.name = :t";
            $this-&gt;parameters[":t"] = $_COOKIE['qanda_questions_tag'];
        }

        return $this-&gt;index($query_where, $query_join, __FUNCTION__);
    } else {
        return header("Location: /myweb/questions".make_url_query($_GET, [],['c','t']));
    }
}


I've tried so much to write it clean and functional, but it contains lots of if statements. It bothers me and I think it should be possible to reduce some of them. Do you have any idea?

Do I need to add more function to the code? Or should I chop the code into multiple separated functions? Anyway, how can I write it more cleanly?
First suggestion is that you shouldn't write the same code twice.  

Your $_GET-if-conditions can be put into a separate function.

$this-&gt;handleCookieByGetParameter('c', 'qanda_questions_category');
$this-&gt;handleCookieByGetParameter('t', 'qanda_questions_tag');

public function handleCookieByGetParameter($parameter, $name)
{
    if ( isset($_GET[$parameter]) ) {
        if ( empty($_GET[$parameter]) ) {
            unset_cookie($name);
        } else {
            set_cookie($name, $_GET[$parameter]);
        }
    }
}


and then I recommend that you check your error before you handle your other stuff:

if ( !$this-&gt;category_cookie &amp;&amp; !$this-&gt;tag_cookie ) {
    return header("Location: /myweb/questions".make_url_query($_GET, [],['c','t']));
    // better an RedirectException
}

$query_where = $query_join = '';

if ( $this-&gt;category_cookie ) {
    $query_where .= "AND qa.category = :c";
    $this-&gt;parameters[":c"] = $_COOKIE['qanda_questions_category'];
}

if ( $this-&gt;tag_cookie ) {
    $query_join  .= " INNER JOIN qanda_tags qt ON qt.qanda_id = qa.id
                  INNER JOIN tags ON tags.id = qt.tag_id";
    $query_where .= " AND tags.name = :t";
    $this-&gt;parameters[":t"] = $_COOKIE['qanda_questions_tag'];
}

return $this-&gt;index($query_where, $query_join, __FUNCTION__);


my next step would be to look to extract the query code into a separate function, so I could use it for other methods too.