I have two arrays, categories and articles. What I'm trying to do is expand the categories array to include all the articles that fall under that category. I have both of the arrays, I am just struggling on the best way to merge them in this manner without a bunch of code. I looked through the arrays section on php.net and didn't find any fuctions that would work in my case and I'm not sure if there is a better way than just nesting loops inside of loops inside of loops, but here's how the arrays are structured (I'll post the first few keys of each array).
The id in the categories array links with cat_id in the articles array.
CATEGORIES array
Array
(
[0] => Array
(
[cnt] => 1
[id] => 4
[cat_title] => Broadband
)
[1] => Array
(
[cnt] => 3
[id] => 6
[cat_title] => Business
)
)
Array
(
[0] => Array
(
[id] => 3
[art_title] => Blogging for Bloggers
[cat_id] => 6
)
[1] => Array
(
[id] => 3
[art_title] => Blogging for Bloggers
[cat_id] => 9
)
[2] => Array
(
[id] => 3
[art_title] => Blogging for Bloggers
[cat_id] => 7
)
[3] => Array
(
[id] => 1
[art_title] => Redesigning your Business
[cat_id] => 6
)
[4] => Array
(
[id] => 2
[art_title] => test
[cat_id] => 6
)
)
I'm actually open to the best way of doing this, if anyone has better ideas. What I'm ultimately going to do is loop through the array to display the results in this manner
BROADBAND (1)
Blogging for Bloggers
BUSINESS (3)
Blogging for Bloggers
Redesigning Your Business
Test
Where each one would be a link based on their id.
Here's the class I'm using...if anyone has any suggestions, and even ways to improve my class (I'm still learning), I'd really appreciate it.
class BaseDisplay {
function GrabCategories($id="0") {
$string = "SELECT COUNT(posted_in.art_id) AS cnt,
categories.id, categories.cat_title FROM categories
LEFT JOIN posted_in ON categories.id = posted_in.cat_id";
if($id != "0") {
$string .= " WHERE cat_id='".$id."'";
}
$string .= " GROUP BY cat_title";
$strSQL = mysql_query($string) or die(mysql_error());
// ADD IN ERROR CHECKING FOR WHEN THERE ARE NO RESULTS
$i = "0";
while($result = mysql_fetch_assoc($strSQL)) {
$categories[$i]["cnt"] = $result["cnt"];
$categories[$i]["id"] = $result["id"];
$categories[$i]["cat_title"] = $result["cat_title"];
$i++;
}
return $categories;
}
function GrabArticlesbyCat() {
$strSQL = mysql_query("SELECT articles.id, articles.art_title, cat_id FROM articles
INNER JOIN posted_in ON articles.id = posted_in.art_id
LEFT JOIN categories ON posted_in.cat_id = categories.id
ORDER BY art_title") or die(mysql_error());
$i = "0";
while($result = mysql_fetch_assoc($strSQL)) {
$articles[$i]["id"] = $result["id"];
$articles[$i]["art_title"] = $result["art_title"];
$articles[$i]["cat_id"] = $result["cat_id"];
$i++;
}
$categories = $this->GrabCategories();
}
}