Skip to content
Advertisement

Add parameters in function

I have 2 functions which work together. But I can’t add parameters on the second one, I don’t understand how it works.

1st function

if(!function_exists('pixiehuge_select_all')) {
    function pixiehuge_select_all($table, $where = null, $order = null, $limit = null, $andwhere = null, $noteq = false, $or = false) {
        global $wpdb;

        $q = "SELECT * FROM `{$table}`";
        $signWhere = ($or) ? 'OR' : 'AND';
        // Select where
        if(empty($noteq)) {
            if(!empty($where)) {
                $q .= " WHERE `" .  esc_sql($where[0]) . "`='" .  esc_sql($where[1]) . "'";
            }
            if(!empty($where) && !empty($andwhere)) {
                $q .= " " . esc_sql($signWhere) . " `" .  esc_sql($where[0]) . "`='" .  esc_sql($where[1]) . "'";
            }
        } else {
            if(!empty($where)) {
                $q .= " WHERE `" .  esc_sql($where[0]) . "`!='" .  esc_sql($where[1]) . "'";
            }
            if(!empty($where) && !empty($andwhere)) {
                $q .= " " . esc_sql($signWhere) . " `" .  esc_sql($where[0]) . "`!='" .  esc_sql($where[1]) . "'";
            }
        }
        if(!empty($order)) {
            $q .= " ORDER BY `" .  esc_sql($order[0]) . "` " .  esc_sql($order[1]);
        }
        if(!empty($limit)) {
            $q .= " LIMIT 0, {$limit}";
        }
        // Check if plugin exists
        if(!function_exists('huge_app')) {
            return false;
        }
        $result = $wpdb->get_results($q, ARRAY_A);

        return $result;
    }
}

2nd function The one I’m trying to edit

if(!function_exists('pixiehuge_streams')) {
    function pixiehuge_streams($id = false, $streamCat = false, $slug = false) {
        global $tables;

        // Get Stream(s)
        if($id) {
            $streams = pixiehuge_select_all($tables['streams'], ['id', esc_sql($id)]);
        } elseif($streamCat) {
            $streams = pixiehuge_select_all($tables['streams'], ['category', esc_sql($streamCat)]);
        } elseif($slug) {
            $streams = pixiehuge_select_all($tables['streams'], ['slug', esc_sql($slug)]);
        } else {
            $streams = pixiehuge_select_all( $tables['streams'] );
        }

        return $streams;
    }
}

What I’m trying to get $streams = "SELECT * FROM streams ORDER BY id DESC LIMIT 4"; (but if I do this I get error 500)

So how can I rewrite $streams by adding parameters to pixiehuge_select_all() in the second function to get the order by id desc and limit 4 ?

Advertisement

Answer

Just look at your function parameters and complete it.

$streams = pixiehuge_select_all($tables['streams'], [], ['id','DESC'] ,4);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement