Jump to content

Reading from session array


rafal
Go to solution Solved by Barand,

Recommended Posts

Hello Everybody,

i have shopping-cart with session_product.

i add arrays per button to the session.

 

i want get the shipping1 and shipping2 fields from the array where the field qty great is or equal to 2 >= 2.

i did this manualy in my example to get shi1 and shi2 lines 15 and 16.

how can i get shipping1 and shipping2 automaticly?

 

thank you very much for your help.

 

here is my code

01.<?php
02.$maxshipping1=0;
03.foreach ($_SESSION['products'] as $pro1) {
04.$maxshipping1 = max($maxshipping1, $pro1['shipping1']); }
05.?>
06.<?php
07.$maxshipping2=0;
08.foreach ($_SESSION['products'] as $pro2) {
09.$maxshipping2 = max($maxshipping2, $pro2['shipping2']); }
10.?>
11.<?php
12.$maxqty=0;
13.foreach ($_SESSION['products'] as $quant) {
14.$maxqty = max($maxqty, $quant['qty']); }
15.$shi1 = $_SESSION["products"][0]["shipping1"];
16.$shi2 = $_SESSION["products"][0]["shipping2"];
17.?>

the Arrays looks like the following:

Array
(
    [0] => Array
        (
            [product] => Orangensaft 0,3l
            [code] => 5
            [qty] => 3
            [price] => 2.99
            [shipping1] => 5
            [shipping2] => 7
        )

    [1] => Array
        (
            [product] => Kuchen
            [code] => 3
            [qty] => 1
            [price] => 2.95
            [shipping1] => 4
            [shipping2] => 6
        )

    [2] => Array
        (
            [product] => Burger 200g
            [code] => 4
            [qty] => 1
            [price] => 3.95
            [shipping1] => 1
            [shipping2] => 3
        )

    [3] => Array
        (
            [product] => Pizza Pollo
            [code] => 2
            [qty] => 1
            [price] => 5.95
            [shipping1] => 2
            [shipping2] => 4
        )

    [4] => Array
        (
            [product] => Sake Maki Lachs, 6 St�cke Pommes
            [code] => 4236134485469
            [qty] => 1
            [price] => 2
            [shipping1] => 1
            [shipping2] => 2
        )

)
Link to comment
Share on other sites

  • Solution

To get the values automatically you close your eyes and make a wish. However, as this seldom works, you have to write the code.

 

Why are you looping 3 times through the same array?

$maxshipping1=$maxshipping2=$maxqty=0;
foreach ($_SESSION['products'] as $prod) {
    $maxshipping1 = max($maxshipping1, $prod['shipping1']);
    $maxshipping2 = max($maxshipping2, $prod['shipping2']);
    $maxqty = max($maxqty, $prod['qty']);
    if ($prod['qty'] >= 2) {
        echo $prod['product'] . '<br>';
        echo "Ship1: " . $prod["shipping1"] . '<br>';
        echo "Ship2: " . $prod["shipping2"] . '<br><br>';
    }
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.