rafal Posted December 25, 2014 Share Posted December 25, 2014 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/ Share on other sites More sharing options...
ginerjm Posted December 25, 2014 Share Posted December 25, 2014 (edited) 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 December 25, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500609 Share on other sites More sharing options...
hansford Posted December 25, 2014 Share Posted December 25, 2014 (edited) 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 December 25, 2014 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500610 Share on other sites More sharing options...
rafal Posted December 25, 2014 Author Share Posted December 25, 2014 (edited) 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 December 25, 2014 by rafal Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500626 Share on other sites More sharing options...
hansford Posted December 25, 2014 Share Posted December 25, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500634 Share on other sites More sharing options...
rafal Posted December 26, 2014 Author Share Posted December 26, 2014 (edited) 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 December 26, 2014 by rafal Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500675 Share on other sites More sharing options...
rafal Posted December 26, 2014 Author Share Posted December 26, 2014 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500677 Share on other sites More sharing options...
ginerjm Posted December 26, 2014 Share Posted December 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500694 Share on other sites More sharing options...
hansford Posted December 26, 2014 Share Posted December 26, 2014 It seems we are stuck on the $_SESSION['products'] array. Before we loop through and this and that, let's just see what the array actually contains. echo '<pre>'; print_r($_SESSION['products']); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500703 Share on other sites More sharing options...
rafal Posted December 26, 2014 Author Share Posted December 26, 2014 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500740 Share on other sites More sharing options...
Barand Posted December 26, 2014 Share Posted December 26, 2014 try $maxshipping=0; foreach ($_SESSION['products'] as $prod) { $maxshipping = max($maxshipping, $prod['shipping1']); } echo $maxshipping; Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500742 Share on other sites More sharing options...
hansford Posted December 26, 2014 Share Posted December 26, 2014 (edited) 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 December 26, 2014 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500744 Share on other sites More sharing options...
Solution rafal Posted December 27, 2014 Author Solution Share Posted December 27, 2014 thank you very much dear Barand, hansford and ginerjm now it works $maxshipping=0; foreach ($_SESSION['products'] as $prod) { $maxshipping = max($maxshipping, $prod['shipping1']); } echo $maxshipping; Quote Link to comment https://forums.phpfreaks.com/topic/293320-heighst-value-in-session-data/#findComment-1500783 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.