gpt4 book ai didi

php - 如何将购物车中的所有商品回显到paypal

转载 作者:行者123 更新时间:2023-11-30 23:34:31 24 4
gpt4 key购买 nike

我只是在学习 php 的基本知识,正在为一个 uni 项目构建一个电子商务网站,我已经通过 paypal 构建和设置了整个东西,我已经通过一些教程完成了。

当我转账到paypal时我想显示;订单摘要中购物车中每件商品的商品名称、价格和数量。

当前状态http://pastie.org/3127790 ,代码只在 paypal 的订单摘要中显示购物车中的最上面的项目,我想我需要实现一个 foreach 循环来告诉代码回显购物车中的所有行,但我不确定究竟要循环什么以及如何循环。

如有任何帮助,我们将不胜感激!

提前致谢,迈克尔

代码片段

      <?php

$sql = "SELECT * FROM podcasts WHERE id_podcasts IN (";
foreach ($_SESSION['cart'] as $id => $value){
$sql .= $id . ",";
}
$sql = substr($sql,0,-1).") ORDER BY name ASC";
$query = mysql_query($sql);
$total_price = 0;
if(!empty($query)){
while($row = mysql_fetch_array($query)){
$subtotal = $_SESSION['cart'][$row['id_podcasts']]['quantity']*$row['price'];
$total_price += $subtotal;
?>

<?php
$ppname = $row['name'];
$_SESSION['cart'][$row['id_podcasts']]['name'] = $row['name'];
$ppquantity = $_SESSION['cart'][$row['id_podcasts']]['quantity'];
$ppprice= $row['price'];
?>
<tr>
<td><?php echo $row['name'];?></td>
<td><?php echo $_SESSION['cart'][$row['id_podcasts']]['quantity'];?></td>
<td><?php echo "&pound;" . $row['price'];?></td>
<td><?php echo"&pound;" . $_SESSION['cart'][$row['id_podcasts']]['quantity']*$row['price'];?></td>
</tr>
<?php
} }
?>


<tr>
<td></td>
<td></td>
<td><span>Total Price:</td></span>
<td><span><?php echo"&pound;" . $total_price;?></td></span>
</tr>
</table>
</div>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="payme_1321908135_biz@immbudden.com"> <!-- change to your paypal address -->
<input type="hidden" name="quantity" value="<?php echo $ppquantity;?>"> <!-- do not change, since you refer to it all via the database -->
<input type="hidden" name="item_name" value="<?php echo $ppname;?>">
<input type="hidden" name="amount" value="<?php echo $ppprice;?>"> <!-- change here -->
<input type="hidden" name="shipping" value="0.00">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="custom" value="<?php echo $_SESSION['emailaddress'];?>"> <!-- if you store their purchase in a database, reference the database number here -->
<input type="hidden" name="return" value="http://shop.residencyradio.com/success.php">
<span class="input_btn"><input type="submit" name="purchase" value="Purchase" ></span>
</form>

最佳答案

我只回答你的问题。我不确定您所有代码的有效性。
我对您粘贴的一些代码进行了一些更改:

<?php
$output_paypal = ''; // container for output for paypal form
$total_items = 0;

if ( ! empty($query))
{
while ($row = mysql_fetch_array($query))
{
$subtotal = $_SESSION['cart'][$row['id_podcasts']]['quantity'] * $row['price'];
$total_price += $subtotal;

$ppname = htmlspecialchars($row['name']);
$_SESSION['cart'][$row['id_podcasts']]['name'] = $row['name']; // this assigment seems not useful
$ppquantity = $_SESSION['cart'][$row['id_podcasts']]['quantity'];
$ppprice = $row['price'];

$total_items ++;

$output_paypal .= <<<EOM
<input type="hidden" name="item_name_{$total_items}" value="$ppname">
<input type="hidden" name="quantity_{$total_items}" value="$ppquantity">
<input type="hidden" name="amount_{$total_items}" value="$ppprice">
EOM; // heredoc syntax (may not be any spaces or tabs before or after the semicolon.)
?>
<tr>
<td><?php echo $ppname; ?></td>
<td><?php echo $ppquantity; ?></td>
<td><?php echo "&pound;" . $ppprice; ?></td>
<td><?php echo"&pound;" . $ppquantity * $ppprice; ?></td>
</tr>
<?php
}
}
?>


<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="payme_1321908135_biz@immbudden.com">

<?php echo $output_paypal; ?>

<input type="hidden" name="shipping" value="0.00">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="custom" value="<?php echo $_SESSION['emailaddress']; ?>">
<input type="hidden" name="return" value="http://shop.residencyradio.com/success.php">
<span class="input_btn"><input type="submit" name="purchase" value="Purchase" ></span>
</form>

关于php - 如何将购物车中的所有商品回显到paypal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8736516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com