在phpmailer中列出购物车

在phpmailer中列出购物车,php,session,cart,shopping-cart,Php,Session,Cart,Shopping Cart,我试着在“下单”之后用phpmailer发送电子邮件。 问题是我无法将购物车列为电子邮件(产品、数量、每种产品的价格和总价) 这向我表明,一切似乎都在运转 cart.php: <tr> <?php $total = 0; if(!empty($_SESSION['cart'])){ include 'config.php'; $index = 0; if(!isset

我试着在“下单”之后用phpmailer发送电子邮件。 问题是我无法将购物车列为电子邮件(产品、数量、每种产品的价格和总价)

这向我表明,一切似乎都在运转

cart.php

<tr>
    <?php
        $total = 0;

        if(!empty($_SESSION['cart'])){
            include 'config.php';

            $index = 0;

        if(!isset($_SESSION['qty_array'])){
            $_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
        }

        $sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
        $query = $conn->query($sql);
        while($row = $query->fetch_assoc()){
    ?>
</tr>
<tr>
    <td>
        <img src="<?= $row['photo'] ?>" width="150px"><br />
        <?= $row['name'] ?>
    </td>
        <input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
    <td>
        <?php echo $_SESSION['qty_array'][$index]; ?>
    </td>
    <td>
        <b><i class="fas fa-dollar-sign"></i> <?php echo number_format($_SESSION['qty_array'][$index]*$row['price'], 2); ?></b>
    </td>
        <?php $total += $_SESSION['qty_array'][$index]*$row['price']; ?>
</tr>
<?php
    $index ++;
    }
    }
?>
    echo var_dump($_SESSION['cart']);
    echo var_dump($_SESSION['qty_array']);

    foreach($_SESSION['cart'] as $key => $product) {
        $name = $product['name'];
        $price = $product['price'];
        $qty = $product['qty'];
        $tprice = $product['totalPrice'];
    }

    $mail->Body  = nl2br("$name\r\n$\r\n$qty\r\n$tprice"); 
    $total = 0;
    if(!empty($_SESSION['cart'])){
    include 'config.php';
    $index = 0;
    if(!isset($_SESSION['qty_array'])){
    $_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
    }
    $sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
    $query = $conn->query($sql);
    while($row = $query->fetch_assoc()){
        $service = $row['name'];
        $qty = $_SESSION['qty_array'][$index];
        $qtyPrice = number_format($row['price'], 2);
        $qtyTotalprice = number_format($_SESSION['qty_array'][$index]*$row['price'], 2);
        $total += $_SESSION['qty_array'][$index]*$row['price'];

    $mail->Body = nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n \r\nTOTAL: $ <u>$total</u>");
    $index ++;
    }
    }
这根本不起作用。我尝试了一些方法,大多数有效的方法只是在购物车列表中列出最后一种产品。但是只有身份证

编辑:

我尝试了另一种方法,但仍然只列出了最后一项

phpmailer.php

<tr>
    <?php
        $total = 0;

        if(!empty($_SESSION['cart'])){
            include 'config.php';

            $index = 0;

        if(!isset($_SESSION['qty_array'])){
            $_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
        }

        $sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
        $query = $conn->query($sql);
        while($row = $query->fetch_assoc()){
    ?>
</tr>
<tr>
    <td>
        <img src="<?= $row['photo'] ?>" width="150px"><br />
        <?= $row['name'] ?>
    </td>
        <input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
    <td>
        <?php echo $_SESSION['qty_array'][$index]; ?>
    </td>
    <td>
        <b><i class="fas fa-dollar-sign"></i> <?php echo number_format($_SESSION['qty_array'][$index]*$row['price'], 2); ?></b>
    </td>
        <?php $total += $_SESSION['qty_array'][$index]*$row['price']; ?>
</tr>
<?php
    $index ++;
    }
    }
?>
    echo var_dump($_SESSION['cart']);
    echo var_dump($_SESSION['qty_array']);

    foreach($_SESSION['cart'] as $key => $product) {
        $name = $product['name'];
        $price = $product['price'];
        $qty = $product['qty'];
        $tprice = $product['totalPrice'];
    }

    $mail->Body  = nl2br("$name\r\n$\r\n$qty\r\n$tprice"); 
    $total = 0;
    if(!empty($_SESSION['cart'])){
    include 'config.php';
    $index = 0;
    if(!isset($_SESSION['qty_array'])){
    $_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
    }
    $sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
    $query = $conn->query($sql);
    while($row = $query->fetch_assoc()){
        $service = $row['name'];
        $qty = $_SESSION['qty_array'][$index];
        $qtyPrice = number_format($row['price'], 2);
        $qtyTotalprice = number_format($_SESSION['qty_array'][$index]*$row['price'], 2);
        $total += $_SESSION['qty_array'][$index]*$row['price'];

    $mail->Body = nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n \r\nTOTAL: $ <u>$total</u>");
    $index ++;
    }
    }
$total=0;
如果(!empty($\u会话['cart'])){
包括'config.php';
$index=0;
如果(!isset($\u会话['qty\u数组]])){
$\会话['qty\ U数组]]=数组填充(0,计数($\会话['cart']),1);
}
$sql=“从id位于(“.infrade(',',$”会话['cart'])中的产品中选择*”;
$query=$conn->query($sql);
而($row=$query->fetch\u assoc()){
$service=$row['name'];
$qty=$\u会话['qty\u数组][$index];
$qtyPrice=number_格式($row['price'],2);
$qtyTotalprice=数字\格式($\会话['qty\数组'][$index]*$row['price'],2);
$total+=$\会话['qty\U数组][$index]*$行['price'];
$mail->Body=nl2br(“$service:($qty)x($$qtyPrice)=$$qtyTotalprice\r\n\r\n总计:$$total”);
$index++;
}
}
我找到了一个解决方案:

phpmailer.php

    ...

    $mail->Body  = nl2br("Hi {$_POST['name']} \r\n");

    /// List Cart Item(s) Start
    $total = 0;
    if(!empty($_SESSION['cart'])){
        include 'config.php';
        $index = 0;
        if(!isset($_SESSION['cart'])){
            $_SESSION['cart'] = array_fill(0, count($_SESSION['cart']), 1);
        }
        $sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
        $query = $conn->query($sql);
        while($row = $query->fetch_assoc()){
            $index;
            $service = $row['name'];
            $qty = $_SESSION['cart'][$index];
            $qtyPrice = $row['price'];
            $qtyTotalprice = number_format($_SESSION['cart'][$index]*$row['price'], 2);
            $total += $_SESSION['cart'][$index]*$row['price'];
        
        $mail->Body .= nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n");
        
        $index ++;
        }
    }
    $mail->Body .= nl2br(" \r\nTOTAL: $ <u>$total</u>");
    /// Cart Item(s) End

    $mail->Body .= nl2br("Kind regards \r\n");

    ...
。。。
$mail->Body=nl2br(“Hi{$\u POST['name']}\r\n”);
///列出购物车项目开始
$total=0;
如果(!empty($\u会话['cart'])){
包括'config.php';
$index=0;
如果(!isset($\u会话['cart'])){
$_会话['cart']=数组填充(0,计数($_会话['cart']),1);
}
$sql=“从id位于(“.infrade(',',$”会话['cart'])中的产品中选择*”;
$query=$conn->query($sql);
而($row=$query->fetch\u assoc()){
美元指数;
$service=$row['name'];
$qty=$\会话['cart'][$index];
$qtyPrice=$row['price'];
$qtyTotalprice=数字\格式($\会话['cart'][$index]*$row['price'],2);
$total+=$\会话['cart'][$index]*$row['price'];
$mail->Body.=nl2br(“$service:($qty)x($$qtyPrice)=$$qtyTotalprice\r\n”);
$index++;
}
}
$mail->Body.=nl2br(“\r\n总计:$$total”);
///购物车项目结束
$mail->Body.=nl2br(“亲切问候\r\n”);
...
我找到了一个解决方案:

phpmailer.php

    ...

    $mail->Body  = nl2br("Hi {$_POST['name']} \r\n");

    /// List Cart Item(s) Start
    $total = 0;
    if(!empty($_SESSION['cart'])){
        include 'config.php';
        $index = 0;
        if(!isset($_SESSION['cart'])){
            $_SESSION['cart'] = array_fill(0, count($_SESSION['cart']), 1);
        }
        $sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
        $query = $conn->query($sql);
        while($row = $query->fetch_assoc()){
            $index;
            $service = $row['name'];
            $qty = $_SESSION['cart'][$index];
            $qtyPrice = $row['price'];
            $qtyTotalprice = number_format($_SESSION['cart'][$index]*$row['price'], 2);
            $total += $_SESSION['cart'][$index]*$row['price'];
        
        $mail->Body .= nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n");
        
        $index ++;
        }
    }
    $mail->Body .= nl2br(" \r\nTOTAL: $ <u>$total</u>");
    /// Cart Item(s) End

    $mail->Body .= nl2br("Kind regards \r\n");

    ...
。。。
$mail->Body=nl2br(“Hi{$\u POST['name']}\r\n”);
///列出购物车项目开始
$total=0;
如果(!empty($\u会话['cart'])){
包括'config.php';
$index=0;
如果(!isset($\u会话['cart'])){
$_会话['cart']=数组填充(0,计数($_会话['cart']),1);
}
$sql=“从id位于(“.infrade(',',$”会话['cart'])中的产品中选择*”;
$query=$conn->query($sql);
而($row=$query->fetch\u assoc()){
美元指数;
$service=$row['name'];
$qty=$\会话['cart'][$index];
$qtyPrice=$row['price'];
$qtyTotalprice=数字\格式($\会话['cart'][$index]*$row['price'],2);
$total+=$\会话['cart'][$index]*$row['price'];
$mail->Body.=nl2br(“$service:($qty)x($$qtyPrice)=$$qtyTotalprice\r\n”);
$index++;
}
}
$mail->Body.=nl2br(“\r\n总计:$$total”);
///购物车项目结束
$mail->Body.=nl2br(“亲切问候\r\n”);
...