Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>At first this is not good way to use the array like this. Try this: </p> <pre><code>&lt;?php $products = array( array('SKU'=&gt;test1, 'name'=&gt;ProductTest1, 'Price'=&gt;10.00), array('SKU'=&gt;test2, 'name'=&gt;ProductTest2, 'Price'=&gt;11.00), array('SKU'=&gt;test3, 'name'=&gt;ProductTest3, 'Price'=&gt;12.00), ); ?&gt; &lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" type="text/css" href="Style.css"&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="container"&gt; &lt;div id="main"&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;SKU&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Price&lt;/th&gt; &lt;th&gt;Action&lt;/th&gt; &lt;/tr&gt; &lt;?php foreach ($products as $key =&gt; $product): ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $product['SKU']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $product['name']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo '£'. number_format($product['Price'],2); ?&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="#"&gt;Add To Cart&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php endif; ?&gt; &lt;/table&gt; &lt;input id="Proceed" type="Submit" value="Proceed"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Now how to solve the cart. You have to use something persistent. The session will be easiest:</p> <pre><code>&lt;?php session_start(); $products = array( array('SKU'=&gt;test1, 'name'=&gt;ProductTest1, 'Price'=&gt;10.00), array('SKU'=&gt;test2, 'name'=&gt;ProductTest2, 'Price'=&gt;11.00), array('SKU'=&gt;test3, 'name'=&gt;ProductTest3, 'Price'=&gt;12.00), ); if (isset($_GET['action'] &amp;&amp; $_GET['action'] === 'addToCart') { if (!isset($_SESSION['cart']) $_SESSION['cart'] = array(); $_SESSION['cart'][] = $_GET['product']; } ?&gt; &lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" type="text/css" href="Style.css"&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="container"&gt; &lt;div id="main"&gt; &lt;?php if (isset($_SESSION['cart']) &amp;&amp; !empty($_SESSION['cart'])): ?&gt; &lt;ul id="cart"&gt; &lt;?php foreach($_SESSION['cart'] as $product): ?&gt; &lt;li&gt;&lt;?= $products[$product]['name'] ?&gt;&lt;/li&gt; &lt;?php endforeach; ?&gt; &lt;/ul&gt; &lt;?php endif; ?&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;SKU&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Price&lt;/th&gt; &lt;th&gt;Action&lt;/th&gt; &lt;/tr&gt; &lt;?php foreach ($products as $key =&gt; $product): ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $product['SKU']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $product['name']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo '£'. number_format($product['Price'],2); ?&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="?action=addToCart&amp;product=&lt;?php echo $key ?&gt;"&gt;Add To Cart&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php endif; ?&gt; &lt;/table&gt; &lt;input id="Proceed" type="Submit" value="Proceed"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>It should work. Just adapting your code.</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