array_push question

Hi,

I’m developing a bespoke blog system in PHP and am having some issues pulling through the categories into the array. The id for the categories are stored in the blog_categories column in the blog table in the following format

1,5,6

These reference ids for category values in the categories table.

The issue I have is I’m pulling back the latest 5 posts data and want the category name and category slug values in the array to output but can’t seem to use array push to push those values into the array. Any ideas? Code is below and thanks for looking.

I get the following output:


Array
(
    [0] => Array
        (
            [blog_id] => 31
            [blog_title] => Warwick Town Council Web Pitch
            [blog_summary_content] => Consectetur adipiscing elit. Mauris erat ante, rhoncus quis elementum et, tincidunt id massa. Sed eu erat risus, nec accumsan urna. Proin frin gilla eleifend turpis. Curabitur accumsan tincidunt erat, in egestas augue molestie varius.
        						
            [blog_key] => 
            [blog_content] => jkjk
            [blog_date_published] => 2012-01-05 18:49:53
            [blog_categories] => 1
            [blog_tags] => 
            [blog_show_live] => 1
        )

    [1] => Array
        (
            [blog_id] => 21
            [blog_title] => FM Sudafix launch Facebook Application
            [blog_summary_content] => Consectetur adipiscing elit. Mauris erat ante, rhoncus quis elementum et, tincidunt id massa. Sed eu erat risus, nec accumsan urna. Proin frin gilla eleifend turpis. Curabitur accumsan tincidunt erat, in egestas augue molestie varius.
        						
            [blog_key] => 
            [blog_content] => fdsf
            [blog_date_published] => 2011-12-09 18:40:04
            [blog_categories] => 1,7
            [blog_tags] => 5,6,7,8
            [blog_show_live] => 1
        )

    [2] => Array
        (
            [blog_id] => 19
            [blog_title] => Whitebox choose Roller for their E-Commerce website
            [blog_summary_content] => Consectetur adipiscing elit. Mauris erat ante, rhoncus quis elementum et, tincidunt id massa. Sed eu erat risus, nec accumsan urna. Proin frin gilla eleifend turpis. Curabitur accumsan tincidunt erat, in egestas augue molestie varius.
        						
            [blog_key] => 
            [blog_content] => fdfssd
            [blog_date_published] => 2011-12-01 18:36:36
            [blog_categories] => 5
            [blog_tags] => 1,2
            [blog_show_live] => 1
        )

    [3] => Array
        (
            [blog_id] => 20
            [blog_title] => Toad Footwear Launch new email marketing campaign
            [blog_summary_content] => Consectetur adipiscing elit. Mauris erat ante, rhoncus quis elementum et, tincidunt id massa. Sed eu erat risus, nec accumsan urna. Proin frin gilla eleifend turpis. Curabitur accumsan tincidunt erat, in egestas augue molestie varius.
        						
            [blog_key] => 
            [blog_content] => fdsf
            [blog_date_published] => 2011-11-26 18:38:38
            [blog_categories] => 6
            [blog_tags] => 3,2,1
            [blog_show_live] => 1
        )

    [4] => Array
        (
            [blog_id] => 22
            [blog_title] => Toad launch new E-commerce website
            [blog_summary_content] => Consectetur adipiscing elit. Mauris erat ante, rhoncus quis elementum et, tincidunt id massa. Sed eu erat risus, nec accumsan urna. Proin frin gilla eleifend turpis. Curabitur accumsan tincidunt erat, in egestas augue molestie varius.
        						
            [blog_key] => 
            [blog_content] => fdsf
            [blog_date_published] => 2011-10-20 18:43:15
            [blog_categories] => 1
            [blog_tags] => 
            [blog_show_live] => 1
        )

)
public function getLatestPosts()	{
		$db = db::getInstance();
		if ($result = $db->query("SELECT * FROM `blogs` WHERE `blog_show_live` = 1 ORDER BY `blog_date_published` DESC LIMIT 5")) {
			if ($rows = $db->getrows($result)) {
				foreach ($rows as $row){
					$output = $this->getCategoryNamesFromId($row['blog_categories']);
					array_push($row, $output);
				}
				return $rows;				
			}
		}
		return false;
	}
	
	public function getCategoryNamesFromId($categories) {
		$db = db::getInstance();
		//for each entry check and pull back name and output into array
		$list = explode(",",substr($categories,0));
		foreach($list as $item) {
			if($result = $db->query("SELECT `category_name`, `category_slug` FROM `categories` WHERE `category_id`=".intval($item)." LIMIT 1")) {
				if ($row = $db->getrow($result)) {
                return $row;
            	}	
			}
		}
	}

Try the following code in getLatestPosts method

foreach ($rows as &$row) {
        $row['blog_categories_names'] = $this->getCategoryNamesFromId($row['blog_categories']);
}       
return $rows;

Thanks. How simple was that!

Only issue or question I have is some of the blog_categories have more then one value so it’s only returning back in some instances the first value and not the rest?

Example Blog_id 21 which has category id 1 & 7?

Thanks

If I have understood this code, what you actually want is a single query which brings back the category name via a JOIN of the two tables blogs and categories - in a method called:

getLatestPostsWithCategoryNames();

Have you tried that?

Try the following method

public function getCategoryNamesFromId($categories) {
        $db = db::getInstance();
        $sql = "SELECT category_name, category_slug FROM categories WHERE category_id IN ($categories)";
        if ($result = $db->query($sql)) {
                // case 1 if db layer supports getAll method
                return $db->getAll($result);

                // case 2
                $res = array();
                while ($row = $db->getrow($result)) 
                        $res[] = $row;
                return $res;
        }
}  

Thanks again that’s resolved it and makes sense.