Jump to content

heighst value in session data


Go to solution Solved by rafal,

Recommended Posts

merry christmas everybody :)

 

i have shopping basket, i put many items in it per session.

i want get the heighst shipping1 value from session but i dont know how.

i tryed last 3 day without success.

now i get the following value for shipping1

2

3.90

5

6.90

 

but i see only 6.90 as it is the heighst shipping1 in basket.

 

thank you very much for your help

 

here is my code

foreach ($_SESSION["products"] as $cart_itm)
{
$inventor = $cart_itm["code"];
$shippall = $cart_itm["shipping1"];
$query1 = "SELECT MAX(shipping1) FROM $table1 WHERE inventar='$inventor'";
$result1 = $link1->query($query1) or die ("Database Error!");
$row = mysqli_fetch_row($result1);
echo $shippall."<br>";
}
Link to comment
https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/
Share on other sites

Change this:

$query1 = "SELECT MAX(shipping1) FROM $table1 WHERE inventar='$inventor'";
$result1 = $link1->query($query1) or die ("Database Error!");
$row = mysqli_fetch_row($result1);
echo $shippall."<br>";
}

to:

$query1 = "SELECT MAX(shipping1) as maxshipping1 FROM $table1 WHERE inventar='$inventor'";
$result1 = $link1->query($query1) or die ("Database Error!");
$row = mysqli_fetch_row($result1);
echo $row['maxshipping1'] . "<br>";
}

 

Of course, you really should rewrite this since you shouldn't be running a query inside a loop

Edited by ginerjm

 

i have shopping basket, i put many items in it per session.

i want get the heighst shipping1 value from session but i dont know how.

 

You state you want the highest shipping1 value from session, but then you attempt to get the highest value from a query. Not exactly clear as to what you want.

If the query is what you are looking for then gingerjm gave you the solution and also noted to take the query out of the session loop.

$query1 = "SELECT MAX(shipping1) as maxshipping1 FROM $table1 WHERE inventar='$inventor'";
$result1 = $link1->query($query1) or die ("Database Error!");
$row = mysqli_fetch_row($result1);
echo $row['maxshipping1'] . "<br>";

However, if you want the highest value from the sessions array named: $_SESSION["products"]["shipping1"]

you need something like this:

$shippall = max($_SESSION["products"]["shipping1"]);
Edited by hansford

thank you very much dear Ginerjm and dear hansford,

i want get the heighst value from the session key shipping1.

now i changed the code to the following.

$shippall = $cart_itm["shipping1"];
echo $shippall."<br>";

i see only one shipping1 value but not the heighst.

when i change to

echo MAX($shippall);

i dont see anything more.

i am very confused why i dont see heightst value!!!

Edited by rafal

Forget the loop to get the max value of all array values within the:

$_SESSION["products"]["shipping1"]

if this session variable is indeed an array of values, then all you need to do to get the highest value from all values is:

echo max($_SESSION["products"]["shipping1"]);

No loop required for this one.

Thank you dear hansford,

i have removed the loop.

when i write

$shippall = $_SESSION["products"]["shipping1"];

i dont see any text

same when i write

$shippall = max($_SESSION["products"]["shipping1"]);

 

i see number but not heighst value when i write

$shippall = $cart_itm["shipping1"];

but if i add max() as the following, i dont see any number

$shippall = max($cart_itm["shipping1"]);

Edited by rafal

this is the basket.php page. maybe it can help

<?php
session_start();
include("info.php");
if(isset($_GET["emptybasket"]) && $_GET["emptybasket"]==1)
{
$return_url = base64_decode($_GET["return_url"]);
session_destroy();
header('Location:'.$return_url);
}
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
$inventar 	= filter_var($_POST["inventar"], FILTER_SANITIZE_STRING);
$product_qty 	= filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT);
$return_url 	= base64_decode($_POST["return_url"]);
if($product_qty > 1000){
die('Bestellung von mehr als 1000 Stück ist nicht möglich!');
}
$query2="SELECT * FROM $table1 WHERE inventar='$inventar' LIMIT 1";
$result2 = $link1->query($query2) or die ("Database Error!");
$row = $result2->fetch_array(MYSQLI_ASSOC);
if ($result2) {
$new_product = array(array('product'=>$row[product], 'code'=>$inventar, 'qty'=>$product_qty, 'price'=>$row[price], 'shipping1'=>$row[shipping1], 'shipping2'=>$row[shipping2]));
if(isset($_SESSION["products"]))
{
$found = false;
foreach ($_SESSION["products"] as $cart_itm)
{
if($cart_itm["code"] == $inventar){
$product[] = array('product'=>$cart_itm["product"], 'code'=>$cart_itm["code"], 'qty'=>$product_qty, 'price'=>$cart_itm["price"], 'shipping1'=>$cart_itm["shipping1"], 'shipping2'=>$cart_itm["shipping2"]);
$found = true;
}
else{
$product[] = array('product'=>$cart_itm["product"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"], 'shipping1'=>$cart_itm["shipping1"], 'shipping2'=>$cart_itm["shipping2"]);
}
}
if($found == false)
{
$_SESSION["products"] = array_merge($product, $new_product);
}
else{
$_SESSION["products"] = $product;
}
}
else{
$_SESSION["products"] = $new_product;
}
}
header('Location:'.$return_url);
}
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"]))
{
$inventar 	= $_GET["removep"];
$return_url 	= base64_decode($_GET["return_url"]);
foreach ($_SESSION["products"] as $cart_itm)
{
if($cart_itm["code"]!=$inventar){
$product[] = array('product'=>$cart_itm["product"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"], 'shipping1'=>$cart_itm["shipping1"], 'shipping2'=>$cart_itm["shipping2"]);
}
$_SESSION["products"] = $product;
}
header('Location:'.$return_url);
}
?>

Two things -

 

1 - it is 'highest', not 'heighst'

 

2 - Why do you use proper syntax when you type $_GET['return_url'] but when you use $row[price] or $row[shipping1] you don't?  By not using quotes on your indices you cause PHP to have to figure out what you mean.  Use quotes on your index values to make it clear to php what you mean.  Not saying this is the cause of your problem, but we can eliminate it by fixing it.

dear hansford, thank you very much for your help :)

this is what i get from the session

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

    [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] => 5.9
            [shipping2] => 7.9
        )

    [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
        )

)

Now it's perfectly clear and Barand posted exactly what you need.

 

I'll just elaborate since the code is already posted and so you understand how to do it next time.

// set a default value of zero to compare against other possible numbers
$maxshipping=0;

foreach ($_SESSION['products'] as $prod) {

    // compare each number in the array and assign the highest value to $maxshipping 
    $maxshipping = max($maxshipping, $prod['shipping1']);
}

echo $maxshipping;
Edited by hansford
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.