Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6254553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:11:55+00:00 2026-05-24T14:11:55+00:00

I am having problems with a simple script that allows a user to browse

  • 0

I am having problems with a simple script that allows a user to browse products drawn from a MySQL table.

The script has a “cart” function where the user can add specific items to an order. The user can then view the order which produces a list of specific items that were ordered, their price and a total amount. (later the user will be able to email the output to the website creator)

Here is the controller:

<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/includes/config.php';
require_once($docRoot . '/includes/layout.php');
require_once($docRoot . '/includes/magicquotes.inc.php');

$productsQuery = 'SELECT `id`, `refCode`, `desc`, `pack`, `measure`, `quantity`, `deptCode`, `taxable`, `price1`, `price2`, `crdCode`, `cost1`, `cost2` FROM `products` ORDER BY `desc` ';
$productsSql = mysql_query($productsQuery) or die(mysql_error());

session_start();
if (!isset($_SESSION['order']))
{
$_SESSION['order'] = array();
}

if (isset($_POST['action']) and $_POST['action'] == 'Order')
{
// Add item to the end of the $_SESSION['order'] array
$_SESSION['order'][] = $_POST['id'];
header('Location: .');
exit();
}

if (isset($_POST['action']) and $_POST['action'] == 'Clear order')
{
// Empty the $_SESSION['order'] array
unset($_SESSION['order']);
header('Location: ?order');
exit();
}

if (mysql_num_rows($productsSql) == 0)
{
die('No results.');
} else
{
$orderContent = '';
while($row = mysql_fetch_assoc($productsSql))
{
$prId = $row['id'];
$prRefCode = $row['refCode'];
$prDesc = $row['desc'];
$prPack = $row['pack'];
$prMeasure = $row['measure'];
$prQuantity = $row['quantity'];
$prDeptCode = $row['deptCode'];
$prTaxable = $row['taxable'];
$prPrice1 = $row['price1'];
$prPrice2 = $row['price2'];
$prCrdCode = $row['crdCode'];
$prCost1 = $row['cost1'];
$prCost2 = $row['cost2'];
$orderContent .= '
    <tr>
        <td>'.$prId.'</td>
        <td>'.$prDesc.'</td>
        <td>'.$prPack.'x'.$prSize.' '.$prMeasure.'</td>
        <td>R'.$prPrice1.'</td>
        <td>
            <form action="" method="post">
                <div>
                    <input type="hidden" name="id" value="'.$prId.'" />
                    <input type="submit" name="action" value="Order" />
                </div>
            </form>
        </td>           
   </tr>
';

if (isset($_GET['order']))
{
$order = array();
$total = 0;
foreach ($_SESSION['order'] as $id)
{
        foreach ($prId as $product)
        {
        if ($product == $id) 
            {
            $order[] = $product;
            $total += $prPrice1;
            break;
            }
        }

include($docRoot . '/orders/orders-finalize.php');
exit();

}
}
}}
include 'orders-layout.php';

?>

This is the include orders-finalize:

<?php
ob_start();

<meta name="keywords" content="'.$keyWords.'" />
';

$belowMenu = '
<p>Orders Page</p>
';

$pageContent = '
<h2>Your order</h2>
';
if (count($order) > 0)
{
    $pageContent .= '
    <table>
        <thead>
            <tr>
                <th>Item Description</th>
                <th>Price</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td>Total:</td>
                <td>R'.number_format($total, 2).'</td>
            </tr>
        </tfoot>
        <tbody>
';  
    foreach ($order as $item) 
{
    if ($item == $prId) 
    {
        $pageContent .= '
                <tr>
                    <td>'.$prDesc.'</td>
                    <td>
                        R'.number_format($prPrice1, 2).'
                    </td>
                </tr>
';      
    }
}
    $pageContent .= '
        </tbody>
        </table>
';

} else
{ 
$pageContent .= '
<p>Your cart is empty!</p>
';
}

$pageContent .= '
<form action="?" method="post">
    <p>
        <a href="?">Continue shopping</a>
        or
        <input type="submit" name="action" value="Clear order" />
    </p>
</form>
';


echo $head1 . $pageDetails . $head2 . $header . $menu . $belowMenu . $content . $pageContent . $footer . $pageScripts;
exit;
?>

This is the orders-layout include:

<?php
ob_start();

$belowMenu = '
<p>Orders Page</p>
';

$pageContent = '
<h2>Place your order</h2>
<p>Your order contains '.count($_SESSION['order']).' items.</p>
<p><a href="?order">View your order</a></p>
<table border="1">
    <thead>
        <tr>
            <th>Stock Code</th>
            <th>Description</th>
            <th>Packsize</th>
            <th>Price</th>
            <th>Quantity</th>
        </tr>
    </thead>
    <tbody>
';

$pageContentEnd = '
    </tbody>
</table>
<br />
<p>All prices are inclusive of VAT</p>
';


echo $head1 . $pageDetails . $head2 . $header . $menu . $belowMenu . $content . $pageContent . $orderContent . $pageContentEnd . $footer . $pageScripts;
exit;
?>

This produces a page where each result is listed.

If the user clicks “view order”, It should lead to a page where the user can see all the items that have been ordered.

What it actually does, is empty the session and refresh the page.

If anyone can spot where I am going wrong here, It would be appreciated if you could pass on some input!

Here is the code that the controller was based on:

<?php
include_once $_SERVER['DOCUMENT_ROOT'] .
    '/includes/magicquotes.inc.php';

$items = array(
    array('id' => '1', 'desc' => 'Canadian-Australian Dictionary',
            'price' => 24.95),
    array('id' => '2', 'desc' => 'As-new parachute (never opened)',
            'price' => 1000),
    array('id' => '3', 'desc' => 'Songs of the Goldfish (2CD set)',
            'price' => 19.99),
    array('id' => '4', 'desc' => 'Simply JavaScript (SitePoint)',
            'price' => 39.95));

session_start();
if (!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}

if (isset($_POST['action']) and $_POST['action'] == 'Buy')
{
// Add item to the end of the $_SESSION['cart'] array
$_SESSION['cart'][] = $_POST['id'];
header('Location: .');
exit();
}

if (isset($_POST['action']) and $_POST['action'] == 'Empty cart')
{
// Empty the $_SESSION['cart'] array
unset($_SESSION['cart']);
header('Location: ?cart');
exit();
}

if (isset($_GET['cart']))
{
$cart = array();
$total = 0;
foreach ($_SESSION['cart'] as $id)
{
    foreach ($items as $product)
    {
        if ($product['id'] == $id)
        {
            $cart[] = $product;
            $total += $product['price'];
            break;
        }
    }
}

include 'cart.html.php';
exit();
}

include 'catalog.html.php';

?>

Here is the code for cart.html.php:

<?php include_once $_SERVER['DOCUMENT_ROOT'] .
    '/includes/helpers.inc.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Shopping cart</title>
    <meta http-equiv="content-type"
            content="text/html; charset=utf-8" />
    <style type="text/css">
    table {
        border-collapse: collapse;
    }
    td, th {
        border: 1px solid black;
    }
    </style>
</head>
<body>
    <h1>Your Shopping Cart</h1>
    <?php if (count($cart) > 0): ?>
    <table>
        <thead>
            <tr>
                <th>Item Description</th>
                <th>Price</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td>Total:</td>
                <td>$<?php echo number_format($total, 2); ?></td>
            </tr>
        </tfoot>
        <tbody>
            <?php foreach ($cart as $item): ?>
                <tr>
                    <td><?php htmlout($item['desc']); ?></td>
                    <td>
                        $<?php echo number_format($item['price'], 2); ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php else: ?>
    <p>Your cart is empty!</p>
    <?php endif; ?>
    <form action="?" method="post">
        <p>
            <a href="?">Continue shopping</a> or
            <input type="submit" name="action" value="Empty cart"/>
        </p>
    </form>
</body>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:11:56+00:00Added an answer on May 24, 2026 at 2:11 pm

    Since you say this is not a typo introduced when pasting to stackoverflow, please see this:

    foreach ($_SESSION['order'] as $id)
    {
            foreach ($prId as $product)
            {
                if ($product == $id) 
                {
                   $order[] = $product;
                   $total += $prPrice1;
                   break;
                 }
            }
    
    include($docRoot . '/orders/orders-finalize.php'); **//why this is included here? you are not yet looped to include all the orders from session inside order array**
    exit();
    
    }
    

    see the line where I say why this is included here? you are not yet looped to include all the orders from session inside order array. you are putting that line inside the foreach that loops through session that actually generate order array, yet in the included order finalize, you try to loop through the uncompleted orders

    not sure if this gonna fix the problem you are having, but try to check this and let us know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've coded a little simple script that allows me to use custom checkboxes. It
I'm having problems with this getopt() code in a script that I'm writing which
Having some problems with a solution that apparently works: <script type=text/javascript > //following code
I have a simple script that dynamically takes images from a Cloudfront bucket, resizes
I'm having problems deploying a simple WebServices app (like Hello World simple) to OC4J.
I'm trying to make a simple blackjack program. Sadly, I'm having problems right off
Im having problems displaying records to my view when passing viewdata to a user
I'm having problems using textures that are larger than the OpenGL window or the
I am having problems getting text within a table to appear centered in IE.
I am having problems manually looping through xml data that is received via an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.