0

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.

2
  • Why wouldn't you be able to dump the array outside the foreach loop? Commented Sep 27, 2011 at 18:14
  • Thats my question Interstellar.... I can display the array but I do not know how to get the correct values from it... for example I can it to display the 'Pids' A9,A9,A9,B12,B12,B12 as such if there were 3 products each. But how do I dig in those numbers and display the Title, Price, and basically everything else that is listed on the cart to an email msg? Commented Sep 29, 2011 at 21:01

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I understand what your saying.. except the results of what the user "adds" to his cart is stored in the session variable cart. So why can't I just reference that variable and display it again? If i were to use your solution would i implement the while outside of the foreach or inside it still?
I was able to use a variation on your method Marc. Basically, made a new function and called that to repeat the cart variables... essentially a separate loop with different display methods. Works great! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.