开发者

How to open and close a loop using PHP Eval() function?

开发者 https://www.devze.com 2023-03-13 10:52 出处:网络
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

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 e开发者_运维百科lse will not do noting      
eval($botStr1);


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.


Thanks all for replying.

For now I am taking all the products in an array. I the given product is not group product the array will contain only one product & if the given product is group product, the array will contain all the sub products for that group product. And then will navigate through array to generate a from which will ask for the attributes of all product in array...

But still I would like to do some R&D with Eval() when the time permits...

Thanks again, Ekta..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号