2011-06-15 38 views
3

我想要做的是,我需要生成一個從哪個將要求給定產品的所有屬性。我有4種不同類型的產品。其中一種產品類型是組產品,當產品是組產品時,我們需要瀏覽組產品的所有子產品,並詢問所有子產品的屬性。我不想根據產品類型重複代碼,所以嘗試使用PHP Eval()函數在需要時啓動和關閉循環。但有些如何不起作用,有人可以幫我嗎?如何使用PHP Eval()函數打開和關閉循環?

這裏是我的代碼,

//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); 
+4

絕對不()'這個使用'EVAL。此外,你的代碼丟失了。你可以再試一次嗎?請參閱這裏的常見問題解答:http://stackoverflow.com/faq – Brad 2011-06-15 19:31:09

+1

如果'eval()'是答案,那麼你問的是錯誤的問題。 – 2011-06-15 19:36:45

+1

'eval()'不是一個類似C的預處理器。傳遞給'eval()'的代碼必須在語法上獨立有效。 http://php.net/manual/en/function.eval.php – 2011-06-15 19:39:00

回答

1

你應該自己的邏輯封裝在一個函數,並通過產品類型作爲參數。這樣可以避免使用eval()編輯的代碼。

0

謝謝大家的回覆。

現在我正在把所有的產品都放在一個數組中。如果給定產品是組產品,則該陣列將包含該組產品的所有子產品。如果給定產品是組產品,則該陣列將僅包含一個產品&。然後將通過陣列導航生成一個將要求所有產品的屬性列陣...

但仍然我想做一些R & D與Eval(),當時間允許...再次

感謝, ..的Ekta