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 テーブルに挿入する別の方法がある場合。
アップデート
私が試してみました
$sqlProducts = serialize($product_id);
$_SESSION['sqlProducts'] = unserialize($sqlProducts);
これは機能しますが、配列全体ではなく最後の項目のみをエコーアウトします。
ページ内ですが、
$sqlProducts = serialize($product_id);
$_SESSION['sqlProducts'] = unserialize($sqlProducts);
echo $_SESSION['sqlProducts'];
正常に動作します
現在、フォームは process.php に投稿されます。これには、1行の単純な行がありecho $_SESSION
、echo $sqlProducts
残念ながら echo $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:"";