Home > Net >  PHP array only showing last product within json
PHP array only showing last product within json

Time:01-13

I have a shopping cart and once the action placeorder.php is hit, i have set-up a webhook to post to discord.

The cart uses session arrays to store the cart information.

the session array is set as below on the product.php page when the "add to card" button is pressed

$_SESSION['cart'] = array($product_id => $quantity);

The product IDs are pulled from a mysql DB.

With 2x items added to the cart, the array is as follows:

 ["cart"]=>
  array(2) {
    [1]=> int(5)
    [4]=> int(3)
  }

If I echo the array it comes out as

foreach ($products as $product){
    echo $product[name];
    echo $products_in_cart[$product['id']];
}


Output: camera5laptop3

$products_in_cart[$product['id'] is simply grabbing the quantity ordered.

The JSON Structure

I am trying to make it so that every product name in the cart appears on a new line (\n) under the "Ordered Products" field. and the same for the quanity.

This would be through a foreach but since the json structure is within json_encode() I am struggling.

The below set-up currently only shows the last $product['name'] and $products_in_cart[$product['id'] in the array

$msg = json_encode([
    // Message
    "content" => "",
 
    // Username
    //"username" => "",
 
    // Avatar URL.
    // Uncomment to use custom avatar instead of bot's pic
    //"avatar_url" => "",
 
    // text-to-speech
    "tts" => false,
 
    // file_upload
    // "file" => "",
    // Embeds Array
    
    "embeds" => [
        [
            // Title
            "title" => "New Order",
 
            // Embed Type, do not change.
            "type" => "rich",
 
            // Description
            "description" => "New order, go sort it",
 
            // Link in title
            //"url" => "",
 
            // Timestamp, only ISO8601
            "timestamp" => $timestamp,
 
            // Left border color, in HEX
            "color" => hexdec( "3366ff" ),
 
            // Footer text
            "footer" => [
                "text" => "Sent from thestash.store",
                "icon_url" => ""
            ],
 
            // Embed image
            "image" => [
                "url" => ""
            ],
 
            // thumbnail
            //"thumbnail" => [
            //    "url" => ""
            //],
 
            // Author name & url
            "author" => [
                "name" => "TEST3",
                "url" => ""
            ],
 
            // Custom fields

            "fields" => [
                // Field 1
                
                [
                    "name" => "Ordered Products",
                    "value" => $product['name'],
                    "inline" => true
                ],
                // Field 2
                 // Field 1
                 [
                    "name" => "Quantity",
                    "value" => $products_in_cart[$product['id']],
                    "inline" => true
                ],
                [
                    "name" => "Order Total $",
                    "value" => $subtotal,
                ],
                [
                    "name" => "Customer Alias",
                    "value" => $_SESSION['alias'],
                    "inline" => true
                ],
                [
                    "name" => "Customer Number",
                    "value" => $_SESSION['number'],
                    "inline" => true
                ],
                [
                    "name" => "Affiliation",
                    "value" => $_SESSION['affiliation'],
                    "inline" => true
                ],

                // etc
            ]
        ]
    ]

], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );

Current Output on discord:

https://i.imgur.com/dEPPCOk.png

Attempting to output as (manuall set in JSON)

https://i.imgur.com/QBNJuLH.png

print_r($msg);

{
    "content": "",
    "tts": false,
    "embeds": [
        {
            "title": "New Order",
            "type": "rich",
            "description": "New order, go sort it",
            "timestamp": "2023-01-13T11:57:22 00:00",
            "color": 3368703,
            "footer": {
                "text": "Sent from thestash.store",
                "icon_url": ""
            },
            "image": {
                "url": ""
            },
            "author": {
                "name": "TEST3",
                "url": "https://thestash.store"
            },
            "fields": [
                {
                    "name": "Ordered Products",
                    "value": "laptop",
                    "inline": true
                },
                {
                    "name": "Quantity",
                    "value": 3,
                    "inline": true
                },
                {
                    "name": "Order Total $",
                    "value": 96000
                },
                {
                    "name": "Customer Alias",
                    "value": "quigg",
                    "inline": true
                },
                {
                    "name": "Customer Number",
                    "value": "1239871237378127",
                    "inline": true
                },
                {
                    "name": "Affiliation",
                    "value": "yes",
                    "inline": true
                }
            ]
        }
    ]
}

Attempting to have the JSON format as follows. Where each $product[name] and $products_in_cart[$product['id']] is on a new line. Currently the last $product[name] and $products_in_cart[$product['id']] show

{
    "content": "",
    "tts": false,
    "embeds": [
        {
            "title": "New Order",
            "type": "rich",
            "description": "New order, go sort it",
            "timestamp": "2023-01-13T11:57:22 00:00",
            "color": 3368703,
            "footer": {
                "text": "Sent from thestash.store",
                "icon_url": ""
            },
            "image": {
                "url": ""
            },
            "author": {
                "name": "TEST3",
                "url": "https://thestash.store"
            },
            "fields": [
                {
                    "name": "Ordered Products",
                    "value": "camera\nlaptop",
                    "inline": true
                },
                {
                    "name": "Quantity",
                    "value": "5\n3",
                    "inline": true
                },
                {
                    "name": "Order Total $",
                    "value": 96000
                },
                {
                    "name": "Customer Alias",
                    "value": "quigg",
                    "inline": true
                },
                {
                    "name": "Customer Number",
                    "value": "1239871237378127",
                    "inline": true
                },
                {
                    "name": "Affiliation",
                    "value": "yes",
                    "inline": true
                }
            ]
        }
    ]
}

CodePudding user response:

So it's just a case of adding extra things into two strings, separated by newlines. So just loop over the products array to create two strings, one for the name and one for the quantity, and put those into variables. Then, you can just use those variables in the right places in your Fields array.

Heres the general idea:

$names = "";
$quantities = "";

foreach ($products as $product){
    $names .= ($names != "" ? "\n" : "").$product["name"];
    $quantities .= ($quantities != "" ? "\n" : "").$products_in_cart[$product['id']];
}

Then in the bit where you build the array:

...
"fields" => [
                [
                    "name" => "Ordered Products",
                    "value" => $names,
                    "inline" => true
                ],
                [
                    "name" => "Quantity",
                    "value" => $quantities,
                    "inline" => true
                ],
...
  • Related