Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can i display an array outside the for each loop?
    text
    copied!<p>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.</p> <p>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 </p> <pre><code>print_r ($contents); </code></pre> <p>But only inside the for each loop, not outside. Below is the code, sorry if it's messy. Any help would be appreciated.</p> <p>Thank you!</p> <pre><code>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 '&lt;form action="http://www.changecompanies.net/dev/theme/cart_checkout.php?action=update" method="post" id="cart"&gt;'; //$output[] = '&lt;table&gt;'; ?&gt; &lt;table id="cart"&gt; &lt;tr&gt; &lt;th&gt;Quantity&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;Item&lt;/th&gt; &lt;th&gt;Product Type&lt;/th&gt; &lt;th&gt;Unit Price&lt;/th&gt; &lt;th&gt;Total&lt;/th&gt; &lt;th&gt;Remove&lt;/th&gt; &lt;/tr&gt; &lt;?php foreach ($contents as $id=&gt;$qty) { $sql = 'SELECT * FROM tccproducts WHERE id = '.$id; $result = $db-&gt;query($sql); $row = $result-&gt;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']; </code></pre> <p>Rest just displays the cart and ends the for each loop.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload