need help: list articles by multi categories and subcategories

I have tried this one but its only displaying the top category not subcategory and child category

[php]
function get_subcategories($catid=0){
$result = GetList("SELECT * FROM ".DB_PREFIX.“mediaCategory WHERE category_parent=$catid”);
$children = array();
foreach($result as $subcat){
$current_id = $subcat[‘category_id’];
$has_sub = GetRow(“SELECT * FROM “.DB_PREFIX.“mediaCategory WHERE category_parent=$current_id”);
//echo $subcat[‘category_name’].’-’.has_subcategories($subcat[‘category_id’]).”
”;
if($has_sub > 0) {
$children[] = get_categories($catid);
} else {
$children[] = $subcat[‘category_id’];
//$children[$current_id] = array_merge($children, get_categories($current_id));
}
}

return $children;

}

//echo print_p(get_subcategories(0));
$cat = get_subcategories(1);
print_p($cat);
foreach($cat as $row){
if($result = GetList(“SELECT p.,c. FROM “.DB_PREFIX.“mediaPost p JOIN “.DB_PREFIX.“mediaCategory c ON p.post_category=c.category_id WHERE c.category_id=’”.$row.”’ ORDER BY p.post_created DESC”)){
foreach($result as $data){
echo $data[‘post_name’].” - “.$data[‘category_id’].”
”;
}
}
}
[/php]

Well, when you get the result from a query such as your $result variable,
it is in an array format. Normally then you use a command to format it into a
useable layout. Like with mysql_associate() or mysql_fetch() or whatever.
I suspect this is the problem.

Normally to parse thru data, you would do something like:
if ($result) {
while($row = mysql_fetch_array($result)) {
// do something with the $row
}
Or, use the associated version instead of fetch…

But, you are using the “Getlist() Method” instead with only the query clause.
Since you did not show the code for that routine, we can not be sure what is in the array.

So, your problem is that you are trying to pull the field-name of “category_id” from your array,
but, it is not in that format yet. To debug this, print your array in your foreach loop and see what is
in it. Just after the “foreach($result as $subcat) line, add an array print command and end there.
Something like:
print_f($subcat); die(”***");

This will print what is inside of $subcat and stop. You can also place this just after the second line
where you call the Getlist routine. This would allow you to see all of the data inside the full $result
array. I am very sure either your “Getlist()” function is not pulling all of the needed fields OR the
$subcat variable is not in the correct format to read out the “category_id” field.

Hope that helps. Good luck, let us know what you find…

Sponsor our Newsletter | Privacy Policy | Terms of Service