如果用递归计算出产品的价格呢?表 pub_pp_bom_wl结构为:
bom InvCode sh sl jldw check_bom
3090106101112 1020209013112 0.0030000 0.7860000000 米 0
3090106101112 1020209011112 0.0030000 0.2795000000 米 0
3090106101112 30505075004 0.0030000 0.0628360000 个 1
3090106101112 10601006005 0.0030000 1.0000000000 张 0
3090106101112 10601005104 0.0030000 1.5000000000 米 0
3090106101112 10601004104 0.0030000 2.0000000000 个 0
3090106101112 10602002053 0.0030000 1.0000000000 个 0如果check_bom为1 说明还有下一级,用InvCode字段的值去查bom字段就可以得到下一级的列表。
但我写了好像不能算出价格来,请大家帮我看看那里写错了,谢谢!
function mydg($bom){
global $db;
global $total;
$sql = "SELECT *,InvCode as yclwlbm, InvCode as wlbm FROM `pub_pp_bom_wl` where `bom` ='".$bom."' and `lock` = '0'";
$_list = $db->getAll($sql);  //得到材料清单
foreach($_list as $inx=>$_rs){
if(intval($_rs['check_bom']) > 0){
$total += = mydg($_rs['InvCode']);
}else{
$sql = "SELECT unit_price FROM `public_co_material_price` where wlbm = '".$_rs['InvCode']."'";
$unit_price = $db->getOne($sql);  //得到单价
if($unit_price==".0000000000") $unit_price = 0;
$total += ($_rs['sl'] * $unit_price) + (($unit_price * $_rs['sh'])*$_rs['sl']);
}
}
return $total;

解决方案 »

  1.   

    好像lz没有用到递归。if(intval($_rs[ 'check_bom '])   >   0)
    {
       //调用本函数+返回值
    }
      

  2.   

    有啊,不过我写多了一个等号
    $total   +=   =   mydg($_rs[ 'InvCode ']);  
    应该是
    $total += mydg($_rs[ 'InvCode ']);  
      

  3.   

    你给出的数据结构与你的代码不符,所以只能假定你的程序中获取数据的代码是正确的在此前提下,你的计算结果应该是翻番的
    global   $total; //这是全局变量
    $total   +=   ($_rs[ 'sl ']   *   $unit_price)   +   (($unit_price   *   $_rs[ 'sh '])*$_rs[ 'sl ']); //这已经是对全局变量赋值了
    return   $total; 
    $total   +=   mydg($_rs[ 'InvCode ']); //这里就相当于 $total+$total 了
      

  4.   

    and   `lock`   =   '0 ' "; 
    没有lock段
      

  5.   

     $db-> getOne($sql); 
    不知道getOne是什么函数,把代码都贴出来看看。
      

  6.   

    $db-> getOne($sql); 就是得到一条记录某个字段的值。
     function getOne($sql, $limited = false)
        {
            if ($limited == true)
            {
                $sql = trim($sql . ' LIMIT 1');
            }        $res = $this->query($sql);
            if ($res !== false)
            {
                $row = mysql_fetch_row($res);            if ($row !== false)
                {
                    return $row[0];
                }
                else
                {
                    return '';
                }
            }
            else
            {
                return false;
            }
        }
      

  7.   

    and `lock` = '0 ' ";  
    lock 这个字段的的意思是是否被锁定,0为不被锁,也就是可用的意思。