I have an oscommerce set up and have decided to add a "most popular" product in each category. I need to query all child categories so that I can then query for the most popular product in the list and display it.
HOWEVER.
I have this function and I just can't seem to get the array of category id's out of it. Is this issue to do with scope??
$children_ids = array();
function fetch_children($parent) {
$result = tep_db_query('SELECT `categories_id` FROM categories 开发者_JS百科WHERE parent_id = "'.(int)$parent.'"');
while($row = tep_db_fetch_array($result)) {
$children_ids[] = (int)$row['categories_id'];
fetch_children($row['categories_id']);
}
}
At this point I am not trying to access the array of data using the $children_ids
variable however this doesn't seem to contain anything!? I've also tried array_push();
Any ideas?
You are trying to modify a global variable from inside a function. You may declare that variable global from inside the function body (but I would strongly recommend against it) or have the function return the array of children_ids, which would be something like this (not tested):
function fetch_children($parent) {
$result = tep_db_query('SELECT categories_id FROM categories WHERE parent_id = "'.(int)$parent.'"');
$list = array();
while($row = tep_db_fetch_array($result)) {
$list[] = (int)$row['categories_id'];
$list = array_merge($list, fetch_children($row['categories_id']));
}
return $list;
}
$children_ids = fetch_children($root_node_id);
精彩评论