duanfan5012 2011-06-15 19:26
浏览 67

如何使用PHP Eval()函数打开和关闭循环?

What I am trying to do is, I need to generate a from which will ask for all the attributes of given products. I am having 4 different type of product. One of the product type is group product and when a product is group product we need to navigate through all the sub products of the group product, and ask for the attribute of all sub products. I don't want to have duplication of code based on product type so try to use PHP Eval() function to start and close a loop when ever it is required. But some how it is not working, can some one please hepl me?

Here is my code,

//To Get Product Information we will call getProductInfo function      
$arrProdInfo = getProductInfo($prodId);      
$pName = $arrProdInfo['name'];      
$pCode = $arrProdInfo['code'];      
$pType = $arrProdInfo['producttype'];          
//Define two Empty variable in which we will store the string to evaluate through PHP Eval() function      
$topStr1 = "";      
$botStr1 = "";  

//If the product type is G(Group Product) then we need to loop through all the products within this group      
if ($pType == "G") {        
 //To fetch all the products within a group product we will call getGroupProd function      
    $rsltGroupProd = getGroupProd($prodId);              
    //Set the first string to star the loop      
    $topStr1 = "while($rowGroupProd = $rsltGroupProd->fetchAssoc()){      
                    $prodId = $rowGroupProd['relproductid'];       
                    if(!is_numeric($prodId)) $prodId = 0;      
                    $pName = $rowGroupProd['name'];      
                    $pCode = $rowGroupProd['code'];      
                    $pType = $rowGroupProd['producttype'];  
                ";      
    //Set second string to close the loop       
$botStr1 = "}";      
}      

//Eval() should start the loop if it's a Group Product else will not do noting      
eval($topStr1);  

//A big form to fetch all the attributes of product will be generated here      

//Eval() should end the loop if it's a Group Product else will not do noting      
eval($botStr1);
  • 写回答

2条回答 默认 最新

  • douyouqian8550 2011-06-15 19:38
    关注

    You should encapsulate your logic in a function and pass the product type as a parameter. This will obviate the need for your eval()'ed code.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line