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 6242531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:58:17+00:00 2026-05-24T11:58:17+00:00

I am working on a simple order system. the peice of code I am

  • 0

I am working on a simple order system.

the peice of code I am stuck on is the following:

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();
}

This is the code is build on a preset array. I am working with a table with a few columns in mysql.

I have decided on the following:

if (isset($_GET['cart']))
{
$cart = array();
$total = 0;
foreach ($_SESSION['cart'] as $id)
{
            while($row = mysql_fetch_assoc($productsSql)) {
    foreach ($row as $product)
    {
        if ($product['id'] == $id)
        {
            $cart[] = $product;
            $total += $product['price'];
            break;
        }
    }
}
    include 'cart.html.php';
exit();
}}

To display this “cart” I have decided on this:

foreach ($cart as $item) {
        $pageContent .= '
                <tr>
                    <td>'.$item['desc'].'</td>
                    <td>
                        R'.number_format($item['price'], 2).'
                    </td>
                </tr>
';

    }

All this seems to do is produce my cart in a fashion where when viewed it displays a list of only the items’ id, e.g. where the description and price are supposed to be, I only get the items id in both fields… I also get a total price of 0.

Can anyone spot where I am going wrong here?

Or atleast try to give me some input so that I get going in the right direction!

Thanks!!

$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());
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="text" size="3" name="quantity" /> 
                </div>
            </form>
        </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>
';
}}
  • 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-24T11:58:18+00:00Added an answer on May 24, 2026 at 11:58 am

    Let’s say that each of the rows in your database looks like this…

    [product_id][product_name][product_description][product_price]
    

    When you assign your query return to a variable passed through mysql_fetch_assoc() using a while loop, each pass will isolate a whole row. Of which you can piece apart manually by array key reference ($array['product_id']) or by using a foreach loop. I think the problem you’re having is that you are mixing this up. Keeping the above example table layout in mind, you could do something like the following:

    while ($tableRow = mysql_fetch_assoc($query)) { // Loops 3 times if there are 3 returned rows... etc
    
        foreach ($tableRow as $key => $value) { // Loops 4 times because there are 4 columns
            echo $value;
            echo $tableRow[$key]; // Same output as previous line
        }
        echo $tableRow['product_id']; // Echos 3 times each row's product_id value
    }
    

    Look at this line in your code: if ($product['id'] == $id) { }

    I think you probably mean if ($row['id'] == $id) { } instead.

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

Sidebar

Related Questions

Im working on simple PHP CMS system and Im stuck with database design. This
I'm working on a pretty simple Java app in order to learn more about
I'm looking for good/working/simple to use PHP code for parsing raw email into parts.
Working on a simple C program I'm stuck with an if test: int line_number
I am working on a simple chat application using a System.Windows.Forms.WebBrowser Control to display
I'm working on a fairly simple ticket managment system. I want to keep a
I'm working on a simple ASP.Net page (handler, actually) where I check the value
I'm working on a simple multiplayer game in which 2-4 players are placed at
i am working on a simple web app which has a user model and
I'm working on a simple 2D game engine in Java, and having no trouble

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.