【发布时间】:2013-09-05 11:40:47
【问题描述】:
我尝试动态获取产品的子 ID。下面是我的表结构。
parent|child
---------------------
44 | 35,6,47,5,50
---------------------
47 | 8,9
---------------------
50 | 12, 15
我将只传递一个父 ID 并获取子 ID,如果任何一个子 ID 有另一个孩子,那么我也必须获取该记录。示例 44->35,6,47,5,50 在这 47 和 50 有子 ID,所以我的最终输出应该是这样的 44-> 35,6,47,8,9,5,50,12,15。
我在下面试过了,
$sql=mysql_fetch_assoc(mysql_query("select * from chain_product where parent='44'"));
$parent=$sql['parent'];
$child=$sql['child'];
$ex=explode(",",$child);
$count=sizeof($ex);
for($i=0;$i<$count;$i++)
{
$list=add_child($ex[$i],$child);
$check=explode(",",$list);
$chck_count=sizeof($check);
if($chck_count>$count)
{
$exit=add_child($ex[$i],$list);
print_r($exit);
}
}
function add_child($main,$ch)
{
$find=mysql_query("select * from chain_product where parent='$main'");
$res=mysql_fetch_assoc($find);
if($res)
{
$replace=$main.",".$res['child'];
$alter=str_replace($main,$replace,$ch);
echo $alter;
}
}
但我得到这样的结果,
35,6,47,8,9,5,5035,6,47,5,50,12,15
但我需要的输出应该是这样的.. 35,6,47,8,9,5,50,12,15。 谁能帮我做到这一点..
【问题讨论】:
-
您只是想删除重复项?
array_unique应该可以解决问题...如果您有字符串,请先使用explode(',', $string)获取数组。 -
永远不要在一个字段中存储多条信息。它总会给你带来麻烦。