2013-10-05 58 views
0

我有以下函数可以基于shoppingCart类生成购物车。无法在函数外部正确访问变量()

function render_shopping_cart() 
{ 
    $shopping_cart = get_shopping_cart(); 

    $output = "<table class='shoppingCart'> 
       <tr> 
        <th> 
         Course 
        </th> 
        <th> 
         Valid Until 
        </th> 
        <th> 
         Quantity 
        </th> 
           <th> 
         Price 
        </th> 
       </tr> 
    "; 
    $line_item_counter = 1; 
    foreach ($shopping_cart->GetItems() as $product_id) 
    { 
      $output .= render_shopping_cart_row($shopping_cart , $product_id, $line_item_counter); 
      $line_item_counter++; 
    } 


    //$output .= render_shopping_cart_shipping_row($shopping_cart); 

    $output .= render_shopping_cart_total_row($shopping_cart); 


    $output .="</table>"; 

    return $output; 
} 
function render_shopping_cart_total_row(ShoppingCart $shopping_cart) 
{ 
    return "<tr> 
       <td> 
       </td> 
         <td> 
         </td> 
       <td> 
       Total: 
       </td> 
       <td> 
       <input type='hidden' name='no_shipping' value='0'> 
       $".$shopping_cart->GetTotal()." 
       </td> 
      </tr>";  

} 

function render_shopping_cart_row(ShoppingCart $shopping_cart , $product_id, $line_item_counter) 
{ 
    $quantity = $shopping_cart->GetItemQuantity($product_id); 
    $amount = $shopping_cart->GetItemCost($product_id); 
    $unit_cost = get_item_cost($product_id); 
    $shipping_amount = $shopping_cart->GetItemShippingCost($product_id); 
    $title = get_item_title($product_id); 
    $validUntil = expiration(); 




    return " 
      <tr> 
       <td> 
        $title 
        <input type='hidden' name='item_name_$line_item_counter' value='$product_id' /> 
       </td> 
       <td> 
        $validUntil 
        <input type='hidden' name='quantity_$line_item_counter' value='$validUntil' /> 
       </td> 
        <td> 
        $quantity 
        <input type='hidden' name='quantity_$line_item_counter' value='$quantity' /> 
       </td> 
       <td> 
        $$amount 
        <input type='hidden' name='amount_$line_item_counter' value='$unit_cost' /> 

        </td> 

      </tr> 
    "; 
} 

我想知道的是如何采取为每个项目生成的$product_id;特别是这部分$title <input type='hidden' name='item_name_$line_item_counter' value='$product_id' />,并把它变成$ sqlProducts阵列或将它们插入到每一个领域的MySQL作为product1, product2, product3, etc

我添加

 $sqlProducts = serialize($product_id); 
     echo unserialize($sqlProducts); 

到LINE_ITEM柜台的样子

$line_item_counter = 1; 
     foreach ($shopping_cart->GetItems() as $product_id) 
     { 
       $output .= render_shopping_cart_row($shopping_cart , $product_id, $line_item_counter); 
       $line_item_counter++; 

     $sqlProducts = serialize($product_id); 
     echo unserialize($sqlProducts); // I've made $abc = unserialize($sqlProducts) as well and it hasn't worked. 

     } 

它们在函数内回声良好,但我无法访问函数外部的变量。我需要一些提交表单后如何将$sqlProducts转移到process.php。或者,如果有一种替代方法可以生成$product_id的数组,并在表单发布后将它们插入到mysql表中。

UPDATE

我已经试过

 $sqlProducts = serialize($product_id); 
     $_SESSION['sqlProducts'] = unserialize($sqlProducts); 

其作品,但只有回声出数组,而不是整个事情中的最后一项。

在这些网页,

  $sqlProducts = serialize($product_id); 
      $_SESSION['sqlProducts'] = unserialize($sqlProducts); 
      echo $_SESSION['sqlProducts']; 

工作正常

目前,形式职位,以process.php它有一个简单的线条来echo $_SESSIONecho $sqlProducts不幸的是,当我回声$sqlProducts,该值是不确定的,如果我回显$_SESSION['sqlProducts']我只能得到阵列中的最后一个项目

解决方法建议下面的任何其他人用这个问题

我重写的功能和创建数组:

function createCart() 
{ 

    //Create a new cart as a session variable with the value being an array 
    $_SESSION['paypalCart'] = array(); 

} 

function insertToCart($productID, $productName, $price, $qty = 1) 
{ 

    //Function is run when a user presses an add to cart button 

    //Check if the product ID exists in the paypal cart array 
    if(array_key_exists($productID, $_SESSION['paypalCart'])) 
    { 

     //Calculate new total based on current quantity 
     $newTotal = $_SESSION['paypalCart'][$productID]['qty'] + $qty; 

     //Update the product quantity with the new total of products 
     $_SESSION['paypalCart'][$productID]['qty'] = $newTotal; 

    } 
    else 
    { 

     //If the product doesn't exist in the cart array then add the product 
     $_SESSION['paypalCart'][$productID]['ID'] = $productID; 
     $_SESSION['paypalCart'][$productID]['name'] = $productName; 
     $_SESSION['paypalCart'][$productID]['price'] = $price; 
     $_SESSION['paypalCart'][$productID]['qty'] = $qty; 

    } 

} 

现在我可以在页面之前使用

<?php 
      if (isset ($_SESSION['paypalCart'])) 
      { 
       foreach($_SESSION['paypalCart'] as $product) 
        { 
        $custom .= $product['name'].", "; 


        } 
      } 
      ?> 
      <input type="hidden" name="custom" value="<?php echo $custom?>"> 

只记得初始化$custom(或任何你正在使用)某处你打电话给变量,例如:$custom:"";

+0

其中是您尝试的插入查询吗? – user2092317

+0

现在我试图只是将它回显出来,看看我是否可以先将数据传递给变量。然后我将处理插入查询。 – Fahad

回答

1

你自己说过,产品id已经在隐藏的输入中,这意味着当表单提交时,它们可以通过process.php中的$_POST数组访问,我只需重命名输入,以便创建一个多维阵列更容易处理,是这样的:

return " 
     <tr> 
      <td> 
       $title 
       <input type='hidden' name='items[$line_item_counter][id]' value='$product_id' /> 
      </td> 
      <td> 
       $validUntil 
       <input type='hidden' name='items[$line_item_counter][valid_until]' value='$validUntil' /> 
      </td> 
       <td> 
       $quantity 
       <input type='hidden' name='items[$line_item_counter][quantity]' value='$quantity' /> 
      </td> 
      <td> 
       $$amount 
       <input type='hidden' name='items[$line_item_counter][amount]' value='$unit_cost' /> 

       </td> 

     </tr> 
"; 

然后在process.php,您可以访问项目的数组,如:

$items = isset($_POST['items']) ? $_POST['items'] : array(); 

而且让你可以做的ID:

现在
if(count($items)){//Check if array is not empty 
    $ids = array_map(function($item){return $item['id'];},$items); //get ids array 
    $sql_ids = implode(',',$ids); //format a string like id1,id2,id3... 
} 
//*Note that anonymous functions are only supported starting from PHP 5.3 

,如果你不想重命名输入,你可以只创建另一个隐藏字段与IDS:

$line_item_counter = 1; 
$product_ids = $shopping_cart->GetItems(); 
foreach ($product_ids as $product_id) 
{ 
     $output .= render_shopping_cart_row($shopping_cart , $product_id, $line_item_counter); 
     $line_item_counter++; 
} 
$sqlProducts = implode(',',$ids); 
$output.= "<input type='hidden' name='product_ids' value='$sqlProducts' />"; 

而且随着$_POST['product_ids']

+0

啊谢谢!我完全错过了......它按照你说的方式工作,但我做了一些修改'$ _SESSION ['paypalCart'] [$ productID] ['ID'] = $ productID; \t \t $ _SESSION ['paypalCart'] [$ productID] ['name'] = $ productName; \t \t $ _SESSION ['paypalCart'] [$ productID] ['price'] = $ price; \t \t $ _SESSION [ 'paypalCart'] [$的productID] [ '数量'] = $数量;'现在我可以做' ”>'完美的工作! – Fahad

+0

参数在'$ _POST ['custom']'中整齐地拾取,我可以很容易地将它插入到数据库中。谢谢 – Fahad

1

你可以尝试从你的“ShoppingCart”类名中删除功能render_shopping_cart_rowrender_shopping_cart_total_row参数?我不知道为什么,但有时在函数参数中指定类名后,我的程序无法工作。

并检查数据是否存在于传递给这些函数render_shopping_cart_row和`render_shopping_cart_total_row的值中。在函数内部调试可能会帮助您找出问题。

+0

是的,课堂给我的问题我想。我最终重写了它,并使用了koala_dev的一些建议。事实证明,比以前更好,但我敢肯定,这两个函数给我的问题,因为我甚至无法初始化变量,无论我做了什么 – Fahad

0

也许你需要设置访问将全局注册为ON或使用会话或cookie来访问全局变量,或者可以使用隐藏字段或可以使用get或post方法或其他方式发送参数