dtwxmn8741 2013-08-25 05:45
浏览 65
已采纳

优化PHP代码 - 函数显示和PHP未定义变量的通知

Any suggestions, below is function of toy variant - function loadProductVarients

At present its displaying :-

a) Toy Variant Name Only, wherein i intend to achieve product name and variant name. For example if Hotwheels is company, Ferrari is Product and Zsi is its Variant.

Now below function is displaying only Zsi as variant name, i intend to achieve Ferrari Zsi as variant name

I think the variable of product needs to be added in $temp1, but despite using many combinations - i am unable to achieve it.

b) Second is getting PHP Notice: Undefined variable: temp1 & temp2 Notice in function loadproductvarients

I am on learning PHP - your help and advise will be much appreciated !!

Edit

Hello Lodder,

Original Function

   function loadProductVarients($id,$minprice,$maxprice,$fuel_type){
        $mainframe =& JFactory::getApplication();
        $option = JRequest::getCmd('option');
        $database =& JFactory::getDBO();
        global $Itemid; 
        $Vcond="";
        if($minprice!="" and $maxprice!=""){
        $Vcond.=" and (v_price between ".$minprice." and ".$maxprice.")";
        }elseif($minprice){ $Vcond.=" and v_price >= ".$minprice."";
        }elseif($maxprice){ $Vcond.=" and v_price <= ".$maxprice."";}

        if($fuel_type!="")
            $Vcond.=" and v_fuel_type='$fuel_type' ";

        $sql = "Select *  from #__newcar_variants  Where v_prod_id='".$id."' $Vcond and v_status='1'";

        $database->setQuery($sql);
        $rows = $database->loadObjectList();
    $list="";
        if($rows){
            foreach($rows as $row){
                if($row->v_small_img!=""){
                    $img = "uploads/variants/".$row->v_big_img ;
                }else{
                    $img="templates/pioneer_home/images/dvd1.jpg";
                }       

    $temp1.='<li><a href="index.php?newcar&id='.$row->v_prod_id.'&vid='.$row->v_id.'">'.$row->v_name.'</a></li>';
    $temp2.='<li>Rs. '.$row->v_price.'</li>';
            }
            $list.='<div class="sliding-box-middle"><ul>'.$temp1.'</ul></div>';
            $list.='<div class="sliding-box-right"><ul>'.$temp2.'</ul></div>';

        }else{
            $list.='<p>No Variants.</p>';
        }
        return $list;
    }


  $sql = "Select *  from #__newcar_variants  Where v_prod_id='".$id."' $Vcond and v_status='1'";

Below is the modified code as advised written with changes :-

       function loadProductVarients($id,$minprice,$maxprice,$fuel_type){
        $db = JFactory::getDBO();
        $Vcond="";
        if($minprice!="" and $maxprice!=""){
        $Vcond.=" and (v_price between ".$minprice." and ".$maxprice.")";
        }elseif($minprice){ $Vcond.=" and v_price >= ".$minprice."";
        }elseif($maxprice){ $Vcond.=" and v_price <= ".$maxprice."";}

        if($fuel_type!="")
            $Vcond.=" and v_fuel_type='$fuel_type' ";

        $query = $db->getQuery(true);
        $query->select('*');
        $query->from('#__newcar_variants');      
        $query->where($db->quote($Vcond), $db->quote($id), $db->quote('v_status=1'));    

        $db->setQuery($query);
        $rows = $db->loadObjectList();

        $list="";
        if($rows){
            foreach($rows as $row){
                if($row->v_small_img!=""){
                    $img = "uploads/variants/".$row->v_big_img ;
                }else{
                    $img="templates/pioneer_home/images/dvd1.jpg";
                }       

    $temp1.='<li><a href="index.php?newcar&id='.$row->v_prod_id.'&vid='.$row->v_id.'">'.$row->v_name.'</a></li>';
    $temp2.='<li>Rs. '.$row->v_price.'</li>';
            }
            $list.='<div class="sliding-box-middle"><ul>'.$temp1.'</ul></div>';
            $list.='<div class="sliding-box-right"><ul>'.$temp2.'</ul></div>';

        }else{
            $list.='<p>No Variants.</p>';
        }
        return $list;
    }
  • 写回答

1条回答 默认 最新

  • doqp87012 2013-08-25 05:49
    关注

    You're using a concatenation on variables that are not set.

    Replace

     $temp1.='<li><a href="index.php?new&id='.$row->v_prod_id.'&vid='.$row->v_id.'">'.$row->v_name.'</a></li>';
     $temp2.='<li>Rs. '.$row->v_price.'</li>';
    

    By

    $temp1='<li><a href="index.php?new&id='.$row->v_prod_id.'&vid='.$row->v_id.'">'.$row->v_name.'</a></li>';
    $temp2='<li>Rs. '.$row->v_price.'</li>';
    

    Or

    $temp1 = '';
    $temp2 = '';
    // CODE
    $temp1.='<li><a href="index.php?new&id='.$row->v_prod_id.'&vid='.$row->v_id.'">'.$row->v_name.'</a></li>';
    $temp2.='<li>Rs. '.$row->v_price.'</li>';
    

    I didn't get your first problem though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 滑块验证码拖动问题悬赏
  • ¥15 Wanted but not invoked:Actually, there were zero interactions with this moc
  • ¥20 怎么驱动tb6612
  • ¥15 Arcgis 3D效果点要素
  • ¥15 在执行yolo训练脚本的时候,没有报错,在扫描val这一步后就推出执行了
  • ¥15 delphi开发的WEBSERVER改用HTTPS协议
  • ¥15 pic16f877A单片机的计数器proteus仿真失效
  • ¥100 调查 Vitis AI 中验证 .xmodel 量化后的正确性
  • ¥30 codelite全屏时file、setting那一行消失了
  • ¥15 gazebo-rviz教程