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.

    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?