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

  • Home
  • SEARCH
  • 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 6355799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:50:19+00:00 2026-05-24T22:50:19+00:00

I am trying to build an array of cart items that has a mix

  • 0

I am trying to build an array of cart items that has a mix of a la carte items and package deals. I want to pull the products from the packages and enter the products into my order table. Below is what I have. It pulls the data, but will only add the last record of the inner array (package products) to the outer array. The a la carte items are fine.

$sql = "SELECT id, item_id, package
        FROM cart WHERE user_id = $userId";
$result = dbQuery($sql);

$arrCartContent = array();

while ($row = dbFetchAssoc($result)) {
    if ($row['package'] == 1) {

        // Add Product Credits to Company and Order
        $arrProductData = fetchProductDataByPackage($row['item_id']);

        $numItem = count($arrProductData);    
        if ($numItem > 0) {                        
            for ($i = 0; $i < $numItem; $i++) {
                extract($arrProductData[$i]);

                $row['product_id'] = $product_id;
                $row['description'] =  $pack_product_name;
            }
        }

    } else {
        $itemDetails = fetchProductDetails($row['item_id']);

        $row['product_id'] = $row['item_id'];
        $row['description'] = $itemDetails['name'];
    }
    $arrCartContent[] = $row;
}

if(!empty($arrCartContent)) {
    foreach ($arrCartContent as $cartData) {

        $product_id = $cartData['product_id'];
        $description = $cartData['description'];

        $sqlInsert      = "INSERT INTO shop_order_x_product 
                            (order_id, product_id, description)
                         VALUES 
                            ($orderId, $product_id, '$description')";

        $resultInsert = dbQuery($sqlInsert) or die('Cannot add order products: ' . mysql_error());
    }
}
  • 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-24T22:50:20+00:00Added an answer on May 24, 2026 at 10:50 pm

    You mixed it up a bit, your general approach is fine, maybe streamlining it a bit will make this a breeze and prevent you from overwriting the $row variable inside the loop:

    $arrCartContent = array();
    
    while ($row = dbFetchAssoc($result))
    {
        if ($row['package'] == 1)
        {    
            // Add Product Credits to Company and Order
            $arrProductData = fetchProductDataByPackage($row['item_id']);
            foreach($arrProductData as $productData)
            {
               $new = array();
               $new['product_id'] = $productData['product_id'];
               $new['description'] = $productData['description'];
               $arrCartContent[] = $new;
            }    
        }
        else
        {
            $itemDetails = fetchProductDetails($row['item_id']);
            $new = array();
            $new['product_id'] = $row['item_id'];
            $new['description'] = $itemDetails['name'];
            $arrCartContent[] = $new;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to build an array from an Object in PHP. I only want
I am trying to build a Hash that has an array as one value;
I'm trying to build a hash from an array. Basically I want to take
I'm trying to build an array/object based on user-input to a form that has
I am trying to build a hierarchical array in PHP, from relational database contents
I am trying to build a program that accepts an array of integers as
I'm trying to build a PHP function that allows me to have an array
I'm trying to build an array in javascript that contains a list of ids.
I'm trying to build a multidimensional array dynamically. What I want is basically this
I am trying to build a query from the values of an array so

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.