我有一个类别表
(id = 1,name = Hand made,parent = 0)
(id = 2,name = Factory made,parent = 0)
(id = 3,name = chairs,parent = 1)
(id = 4,name = tabels,parent = 1)
(id = 5,name = old chairs,parent = 3)
如果访客点击类别OLD CHAIRS,则会在菜单中进入页面
products.php?category_id = 5
然后在这个页面我需要知道什么是主要的category_id,应该是使用category_id = 1进行手工制作
所以在这个页面我想说
if isset($_REQUEST['category_id']){
do the function till find the main parent,
$mainparentid = main parent category_id
}else { $mainparentid = '';
} echo $mainparentid;
这是我的PHP代码
if (isset($_REQUEST['category_id'])) {
function getParent($id) {
global $connection;
$query_rsCategoryId = "SELECT * FROM categories
WHERE category_id = '".$_REQUEST['category_id']."'";
$rsCategoryId = mysql_query($query_rsCategoryId, $connection);
$row_rsCategoryId = mysql_fetch_assoc($rsCategoryId);
$parent = $row_rsCategoryId['category_parent'];
if (mysql_num_rows($rsCategoryId) < 1) {
// Error handling, entry with id $id not found
return null;
}
if ($parent == 0) {
return $id;
} else {
return getParent($parent);
}
}
$mainparentid = getParent($id);
}else {
$mainparentid ='none';
}
echo $mainparentid ;
答案 0 :(得分:0)
您始终从$ _REQUEST输入类别的ID。更改行:
WHERE category_id = '".$_REQUEST['category_id']."'";
为:
WHERE category_id = '".$id."'";
答案 1 :(得分:0)
您正在使用来自请求的父ID,而不是通过函数参数
给出 // notice $id instead of $_REQUEST['category_id']
$query_rsCategoryId = "SELECT * FROM categories WHERE category_id = '{$id}'";