I have been battling with this code for quite a while and just don’t seem to be able to get it to work.
Have tried quite a few variants of the configuration but to no avail. The most confusing part is that it works absolutely fine for displaying a product catalogue (with different variables) but just not this!
errors are:
( ! ) Notice: Undefined variable: quotes in C:\wamp\www\longlifedecking\quote.php on line 40 Call Stack
# Time Memory Function Location 1 0.0089 689992 {main}( ) ..\quote.php:0
( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\www\longlifedecking\quote.php on line 40 Call Stack
# Time Memory Function Location 1 0.0089 689992 {main}( ) ..\quote.php:0
and the code which I have combined is:
<?php include 'inc/header.php'; ?>
<?php include 'inc/dbconnect.php'; ?>
<?php
// Quotes link from Nav
$sessionProdId = $_SESSION["cart"];
$result = mysqli_query($link, "SELECT prod_id, prod_name, prod_text FROM products WHERE prod_id ='$sessionProdId'");
if (!$result)
{
$error = 'Error fetching products: ' . mysqli_error($link);
include 'error.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$quotes[] = array('id' => $row['prod_id'], 'name' => $row['prod_name'], 'text' => $row['prod_text']);
}
?>
<section id="ctrCol" class="fl">
<section class="partFill lrgMod ctr">
<header>
<h1>Quote me</h1>
</header>
<p>Your shopping cart contains <?php echo count($_SESSION['cart']); ?> items.</p>
<?php Print_r ($_SESSION); ?>
<div class="partFill">
<ul>
<?php foreach ($quotes as $quote): ?>
<li>
<h3><?php echo htmlspecialchars($quote['name'], ENT_QUOTES,'UTF-8'); ?></h3>
<p><?php echo htmlspecialchars($quote['text'], ENT_QUOTES,'UTF-8'); ?></p>
</li>
<?php endforeach; ?>
</ul>
</div>
</section>
</section> <!-- end of ctrCol -->
<?php include 'inc/rightnav.php'; ?>
<?php include 'inc/footer.php'; ?>
My apologies if this post isn’t formatted correctly but this is my first post and just getting used to it!
Many thanks all.
Finally got the problem sorted. The issue was that no value was being passed to the variable for the MySQL query.
I was not using the session’s array properly. To use the ID’s held within the session I had to
implode the array.
This then allows the output to function correctly with the previous code.