0
votes

I am retrieving post in CI using get_where query, my where variable is chaking for category is this.

I had pass the array of category to get_where query in foreach loop, but if there is no post under category, query gives me blank array in return.

My category array:

Array ( 
    [0] => Array ( 
           [0] => 341 
           [1] => Email Templates 
           [2] => email-templates 
           [3] => marketing 
           [4] => Email Templates 
           [5] => 340 ) 
    [1] => Array ( 
           [0] => 342 
           [1] => Newsletters 
           [2] => newsletters 
           [3] => marketing 
           [4] => Newsletters 
           [5] => 341 ) 
    [2] => Array ( 
           [0] => 343 
           [1] => e-Flyers 
           [2] => e-flyers 
           [3] => marketing 
           [4] => e-Flyers 
           [5] => 341 ) 
    [3] => Array ( 
           [0] => 344 
           [1] => Catalogs 
           [2] => catalogs 
           [3] => marketing 
           [4] => Catalogs 
           [5] => 341 ) 
    [4] => Array ( 
           [0] => 345 
           [1] => Email Stationery 
           [2] => email-stationery 
           [3] => marketing 
           [4] => Email Stationery 
           [5] => 341 ) )

above array is my $subcatid;

my controller:

public function index()
{
    $data['post'] = $this->post->get_category_post($subcatid);
    print_r($data['post']);
    $this->load->view('frontend/post/category_post', $data);
}

in get_category_post; post function

function get_category_post($up_cat)
    {
        foreach($up_cat as $cat){
            $query = $this->db->get_where('accmark_posts', array('up_sub_cat' => $cat[0]));
            $result = $query->result();
            print_r($result);
        }
    }

this function give me following array

Array ( ) 
Array ( )
Array ( ) 
Array ( ) 
Array ( ) 
Array ( ) 
Array ( )
Array ( ) 
Array ( ) 
Array ( ) 
Array ( ) 
Array ( ) 
Array ( ) 
Array ( 
   [0] => stdClass Object ( 
              [up_id] => 1 
              [user_id] => mediaexhibitor 
              [up_time] => 2015-02-15 16:24:11 
              [up_cat] => 340 
              [up_name] => RealEsta - Real Estate Email Marketing      
              [up_name_slug] => realesta-real-estate-email-marketing )

I want to remove this blank array() from result.

2
Sort your formatting out, its not hard to present the question in a readable way and helps people help ypu - Mike Miller

2 Answers

0
votes

Use array_filter($result). If you don't provide a callback function as the second parameter, the function will filter out all array items equal to false, e.g., empty arrays.

0
votes

Add an if condition in there.

For example:

$errors = array_filter($_POST);

if (empty($errors)) {

 echo "array is empty.";
}
else
{
 echo "It is not empty.";
}

Or

There are two elements in array and this definitely doesn't mean that array is empty. As a quick workaround you can do following:

$errors = array_filter($errors);

if (!empty($errors)) {
}

array_filter() function's default behavior will remove all values from array which are equal to null, 0, '' or false

Otherwise, in your particular case empty() construct will always return true if there is at least one element even with "empty" value.