如何在 foreach 循环之外显示数组?

发布于 2024-12-06 23:17:15 字数 1880 浏览 1 评论 0原文

因此,我有一个工作正常的购物车,但我想在 foreach 循环之外的联系表单内显示购物车的结果。

我遇到的问题是,每当我尝试显示 $Ptitle 时,它​​只会显示购物车中的最后一个添加项。不是数组内标题的完整列表。我尝试了各种方法来显示数组结果,但似乎都不起作用。看来我可以通过

print_r ($contents);

但只能在 foreach 循环内部打印数组,而不是在外部。 下面是代码,如有混乱请见谅。任何帮助将不胜感激。

谢谢你!

function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
    $items = explode(',',$cart);
    $contents = array();
    foreach ($items as $item) {
        $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1: 1;

    }
    echo '<form action="http://www.changecompanies.net/dev/theme/cart_checkout.php?action=update" method="post" id="cart">';
    //$output[] = '<table>';
    ?>
    <table id="cart">
                <tr>
                    <th>Quantity</th>
                    <th></th>
                    <th>Item</th>
                    <th>Product Type</th>
                    <th>Unit Price</th>
                    <th>Total</th>
                    <th>Remove</th>
                </tr>
    <?php
    foreach ($contents as $id=>$qty) {



        $sql = 'SELECT * FROM tccproducts WHERE id = '.$id;
        $result = $db->query($sql);
        $row = $result->fetch();
        extract($row);

        $result = mysql_query("SELECT * FROM tccproducts WHERE Pid ='$id'")or die (mysql_error());
        //if (!$result);
        //echo "no result";
        $myrow = mysql_fetch_array($result) or mysql_error('error!');
        $name = $myrow['Ptitle'];
        //$name = substr($name,0,40);
        $Pprice = $myrow['Pprice'];
        $Ptitle = $myrow['Ptitle'];
        $Pimage = $myrow['Pimage'];
        $Pcat = $myrow['Pcategory'];
        $mini = $myrow['Pminimum'];

Rest 只是显示购物车并结束 for 每个循环。

So I have a shopping cart that works fine, but I want to display the results of the shopping cart inside a contact form outside of the for each loop.

The problem I am having is whenever I try to display $Ptitle it just displays the last addition to the shopping cart. Not the full list of titles inside the array. I have tried various methods to display the array results, but none seem to work. And it seems i can print the array via

print_r ($contents);

But only inside the for each loop, not outside.
Below is the code, sorry if it's messy. Any help would be appreciated.

Thank you!

function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
    $items = explode(',',$cart);
    $contents = array();
    foreach ($items as $item) {
        $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1: 1;

    }
    echo '<form action="http://www.changecompanies.net/dev/theme/cart_checkout.php?action=update" method="post" id="cart">';
    //$output[] = '<table>';
    ?>
    <table id="cart">
                <tr>
                    <th>Quantity</th>
                    <th></th>
                    <th>Item</th>
                    <th>Product Type</th>
                    <th>Unit Price</th>
                    <th>Total</th>
                    <th>Remove</th>
                </tr>
    <?php
    foreach ($contents as $id=>$qty) {



        $sql = 'SELECT * FROM tccproducts WHERE id = '.$id;
        $result = $db->query($sql);
        $row = $result->fetch();
        extract($row);

        $result = mysql_query("SELECT * FROM tccproducts WHERE Pid ='$id'")or die (mysql_error());
        //if (!$result);
        //echo "no result";
        $myrow = mysql_fetch_array($result) or mysql_error('error!');
        $name = $myrow['Ptitle'];
        //$name = substr($name,0,40);
        $Pprice = $myrow['Pprice'];
        $Ptitle = $myrow['Ptitle'];
        $Pimage = $myrow['Pimage'];
        $Pcat = $myrow['Pcategory'];
        $mini = $myrow['Pminimum'];

Rest just displays the cart and ends the for each loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九命猫 2024-12-13 23:17:15

您正在对购物车中的所有商品运行循环,获取有关该商品的详细信息,然后将详细信息存储在各个变量中。在循环的每次迭代中,您先前检索的数据都会被获取的下一行数据覆盖。因此,您只存储最后获取的项目中的数据。

如果要存储所有项目数据,则必须将每一行存储在箭头中并随后输出,或者仅在获取时输出每一行。

while($row = mysql_fetch(...)) {
     echo <<<EOL
<tr>
    <td>{$quantity}</td>
    <td>{$row['price']}</td>
    etc...
</tr>
EOL;
}

You're running a loop on all the items in the cart, fetching details about that item, then storing the details in individual variables. On every iteration of the loop, the data you retrieve previously gets overwritten by the next row of data fetched. As such, you only ever store the data from the LAST item fetched.

If you want to store all of the item data, you'll have to store each row in an arrow and output afterwards, or just output each row as it's fetched.

while($row = mysql_fetch(...)) {
     echo <<<EOL
<tr>
    <td>{$quantity}</td>
    <td>{$row['price']}</td>
    etc...
</tr>
EOL;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文