I know this has been talked about here before. However, my situation is a bit different and I'm so close.
I would like to explode an array of category id's from a news data table then get the category name from the category table.
Right now I am able to get the id's out and explode them just fine inside the while loop for the news data using:
$post_cats_data = $news_data['cat_id']; // 1,6,7,11
$post_cats_id = explode(",", $post_cats_data);
Now where I'm getting stuck is getting the news categories and echoing out the name.
$cat_count = count($post_cats_id);
$i = 0;
foreach($post_cats_id as $cat_id){
$cat_qry = mysql_query("SELECT * FROM news_categories WHERE `cat_id` = '$cat_id'") or die(mysql_error());
$cat_title_row = mysql_fetch_row($cat_qry) or die(mysql_error());
$i++;
$write_cat .= $cat_title_row['cat_name'] ;
if($i<$cat_count){
$write_cat .= ', ';
}
}
The idea is that this will get the category names from the category tables that were exploded and will add a comma back to the end of everyone but the last one. I am unable to get the category name and when I return the ID it loops though the id for all the news.
I know this is a simple problem, I'm just new to using loops.
Thanks.