0

Would be great if anyone can help? I currently have the data below being output by the code at the bottom but ranther than it repeating for each order_product_id I want to build an multi level array from the data.

Current output:

[0] => Array
        (
            [order_product_id] => 43
            [order_id] => 1
            [product_id] => 161
            [name] => Hoodie
            [model] => Hoodie
            [quantity] => 1
            [price] => 23.9500
            [total] => 23.9500
            [tax] => 0.0000
            [reward] => 0
            [option_id] => 141
            [option_name] => Hoodie Style
            [option_value] => Pull over
        )

    [1] => Array
        (
            [order_product_id] => 43
            [order_id] => 1
            [product_id] => 161
            [name] => Hoodie
            [model] => Hoodie
            [quantity] => 1
            [price] => 23.9500
            [total] => 23.9500
            [tax] => 0.0000
            [reward] => 0
            [option_id] => 142
            [option_name] => Hoodie Colour
            [option_value] => Light Pink
        )

    [2] => Array
        (
            [order_product_id] => 43
            [order_id] => 1
            [product_id] => 161
            [name] => Hoodie
            [model] => Hoodie
            [quantity] => 1
            [price] => 23.9500
            [total] => 23.9500
            [tax] => 0.0000
            [reward] => 0
            [option_id] => 143
            [option_name] => Adult Sizes
            [option_value] => Ladies Meduim 10-12
        )

Desired array where each product has all its options in multi level rather than how the current set up shows it:

[0] => Array
        (
            [order_product_id] => 43
            [order_id] => 1
            [product_id] => 161
            [name] => Hoodie
            [model] => Hoodie
            [quantity] => 1
            [price] => 23.9500
            [total] => 23.9500
            [tax] => 0.0000
            [reward] => 0
            Array
                (
                [0] => Array
                    (
                    [option_id] => 141
                    [option_name] => Hoodie Style
                    [option_value] => Pull over
                    )
                [1] => Array
                    (
                    [option_id] => 142
                    [option_name] => Hoodie Colour
                    [option_value] => Light Pink
                    )
                [2] => Array
                    (
                    [option_id] => 141
                    [option_name] => Adult Sizes
                    [option_value] => Ladies Meduim 10-12
                    )
                )
        )

The current output is being built by this:

$sql = "SELECT site_order_product.*, 
site_order_option.order_option_id AS option_id, site_order_option.name AS option_name, site_order_option.value AS option_value
FROM site_order_product
INNER JOIN site_order_option ON site_order_product.order_product_id = site_order_option.order_product_id; ";

$result=mysqli_query($dbh2,$sql);
  if($result) {
            $row = mysqli_fetch_all($result,MYSQLI_ASSOC);
            return $row;
        } else {
            return false;
        }   

Thanks for any help!

Tommo-101
  • 13
  • 3

1 Answers1

0

NOTE : The feature that your looking is lazy loading. ORM suits best for your problem. To name some of ORM's RedBean, Doctrine, Propel.

This is an example just for the sake of demonstration.

Without any proper database table schema its difficult to give you the exact answer output what you were looking for. But will help you with some example which will do the same.

Think that your having the products table and orders table.

Products

id | product_name | product_price | product_notes

Orders

id | product_id | order_number

Problem your facing : Now if you query for products then you will get the list of products details. But the problem you were facing was to gets its respective orders too.

Solution : When you loop into each product, get the respective product id and query all the orders for that product and assign into one main array.

/* Get the list of products */
$productsQuery = mysqli_query($link, "SELECT * FROM products"); 
/* Array to hold the details or products and its respective order per product */
$productDetails = array(); 

if(mysqli_num_rows($productsQuery) > 0){
    $productCount = 0;
    while($product = mysqli_fetch_assoc($productsQuery)){
        $productId = $product['id'];

        /* Assign the product details to productDetails array */
        $productDetails[$productCount] = $product;

        /* Get the details of orders which respective productid */
        $ordersQuery = mysqli_query($link, "SELECT * FROM orders WHERE product_id = ".$productId);
        if(mysqli_num_rows($ordersQuery) > 0){
            while($order = mysqli_fetch_assoc($ordersQuery)){
                /* Here we have assigned the product orders to respective product */
                $productDetails[$productCount]['orders'] = $order;
            }
        }else{
            /* If no order assign an empty array */
            $productDetails[$productCount]['orders'] = [];
        }
    }
}
Channaveer Hakari
  • 2,338
  • 1
  • 26
  • 34