Jump to content

MrL8Knight

New Members
  • Posts

    6
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

MrL8Knight's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. logu Thank you for your post, much respect, I tried the script and it gave me a sum of 0 instead of 36. I have posted your code for you to see how I had implemented it, maybey I was missing something. <? $total =0; $temp = $_COOKIE[mycookie] ; for($i=0;$i<count($temp);$i++){ $total += $temp[$i][9]; } echo "Total Cost = $total"; ?> [b]BUT WAIT!!!!![/b]I just got my hands on the script to make it work properly from the Guru Banfa Here it is <? $total = 0; foreach ($_COOKIE[mycookie] as $valueq) { $value3 = unserialize(stripslashes($valueq)); $total += $value3[9]; } echo "Total Cost = " . $total . "\n"; ?> Thanks again, back in the running!!! MrL8Knight
  2. I am building a simple shopping cart and I am having problems trying to add the costs of the items to generate a total cost. I am new at this so forgive me if my technical verbiage isn’t the greatest! I am trying to sum specific values in my arrays (or the price portions of the arrays). I have tried every imaginable way with the array_sum command and nothing has worked for me! Here is what my cookie information looks like after I put 2 test items into the cart that cost 18.00 each. Array ( [0] => 21 [1] => test1 [2] => 12 [3] => 12 [4] => 1 [5] => Please select [6] => Please select [7] => Please select [8] => [9] => 18.00 ) Array ( [0] => 22 [1] => test2 [2] => 12 [3] => 12 [4] => 1 [5] => Please select [6] => Please select [7] => Please select [8] => [9] => 18.00 ) Arrays 21 and 22 both contain items with a price of 18.00 I am trying to add both of the [9] fields (18.00) to come up with a total cost Here is the code I am using to try to add the price of both items: <? foreach ($_COOKIE[mycookie] as $valueq) { $value3 = unserialize(stripslashes($valueq)); } echo "Total Cost = " . array_sum($value3) . "\n"; ?> When I load this page, it comes up with a total of 65 instead of 36 (18+18) I’m thinking the problem is that I am summing all the values when I just need to sum the values of the [9] field which contains the prices. Any help would be greatly appreciated!!!!!! MrL8Knight
  3. Ok I finally figured this one out, first thank you to all that have helped me with this problem; I can’t express how much it is appreciated. Here are the 3 working pages, hope someone else finds this simple, PHP form based shopping cart usefull! [b]Page 1 (index2.php)(the Form, which starts a cookie to keep an incremental count (Key) for each opening)[/b] <?php if (isset($_COOKIE['count'])) { $count = $_COOKIE['count'] + 1; } else { $count = 1; } setcookie('count', $count, time()+13600); echo "$count"; echo "<form action='add.php?id=$count' method='post'> <input name='name' type='hidden' value='$count'> Opening name<br> <input name='name' type='text' size='30'> </b></p> <p><b> Width<br> <input name='w' type='text' size='30'> </b></p> <p><b> Height<br> <input name='h' type='text' size='30'> </b></p> <p> <input type='image' src='../images/Start-Order.gif' name='submit' alt='Start Order'> <br> </p> </form>"; ?> [b]Page 2 (add.php)(the script to get and serialize data typed in the form and add it to the cookie)[/b] <?php $count= $_GET['id']; $name= $_POST['name']; $w= $_POST['w']; $h= $_POST['h']; $serialized_data = serialize (array ($count,$name,$w,$h)); setcookie("mycookie[$count]", $serialized_data, time()+13600); echo "Opening was Added<br><br>"; echo "$count"; echo "<br><br>"; echo "$name"; echo "$w"; echo "$h"; echo "<br><br>"; echo "<a href='index2.php'>Add another opening!</a><br>"; echo "<a href='show.php'>Show Cart!</a><br>"; ?> [b]Page 3 (show.php)(the show cart page that checks to see if items are in the cart (or cookie) and if so, shows the unserialized data)[/b] <?php if($_COOKIE["mycookie"]) { foreach( $_COOKIE[mycookie] as $key => $value) { $value2 = unserialize(stripslashes($value)); echo "<br>"; print_r($value2[0]); print_r($value2[1]); print_r($value2[2]); print_r($value2[3]); } echo "<br><br>"; echo "<a href='index2.php'>Add Opening</a><br><br>"; } else { print "No Items in your cart<br><br>"; echo "<a href='index2.php'>Add Opening</a><br><br>"; } ?> [b]Thanks again everyone… Cheers![/b] -MrL8Knight
  4. I added a print_r($_COOKIE); at the beginning of the page Hooker provided me to display all my cookie values for diagnosis. Here is what my output looks like, I'm still missing the unserialized values, I'm wondering am I storing the values wrong on page 2?, or am I retreiving them wrong on page 3?.... Array ( [Cart3] => [mycookie] => Array ( [1] => a:4:{i:0;s:1:\"1\";i:1;s:6:\"tester\";i:2;s:2:\"12\";i:3;s:2:\"12\";} [2] => a:4:{i:0;s:1:\"2\";i:1;s:7:\"tester2\";i:2;s:2:\"48\";i:3;s:2:\"48\";} [4] => a:4:{i:0;s:1:\"4\";i:1;s:5:\"test4\";i:2;s:2:\"23\";i:3;s:2:\"23\";} ) [count] => 4 [phpbb2mysql_data] => a:2:{s:11:\"autologinid\";s:0:\"\";s:6:\"userid\";s:1:\"2\";} [PHPSESSID] => 5c7ba3baf248de93aaf9ff3c7cc32702 ) Number 1 : Description Number 2 : Description Number 4 : Description Add Opening
  5. Hey Hooker, Thanks for your response! I tried your code for the page 3 and still no data displaying. It will display the key data but still doesn't display the "value" data, if I remove the unserialize command it displays the data fine, but of course serialized. One other thing to note your code did work fine without any errors, just still can't see the data. Any other ideas? Thank you for your help! MrL8Knight
  6. Hello, I am trying to build a simple php form based shopping cart using a cookie with arrays. I need to use 1 cookie because each order will have over 20 items. With that said, I realize I need to serialize the data to put the array into the cookie. That part of my code is working just fine and displaying fine. The problem I’m having is when I try to unserialize and display; the data does not appear. If I remove my unserialize command line (see page 3) the data displays just fine with the serialization. So basically I need help unserializing and displaying the data so I can display it in my shopping cart nice and clean. [b]Here is page 1(index2.php) which is a form that asks for opening name, width and height.[/b] <?php if (isset($_COOKIE['count'])) { $count = $_COOKIE['count'] + 1; } else { $count = 1; } setcookie('count', $count, time()+3600); setcookie("Cart[$count]", $item, time()+3600); echo "$count"; echo "<form action='add.php?id=$count' method='post'> <input name='name' type='hidden' value='$count'> Opening name<br> <input name='name' type='text' size='30'> </b></p> <p><b> Width<br> <input name='w' type='text' size='30'> </b></p> <p><b> Height<br> <input name='h' type='text' size='30'> </b></p> <p> <input type='image' src='../images/Start-Order.gif' name='submit' alt='Start Order'> <br> </p> </form>"; ?> [b]The second page(add.php) receives the data, creates a serialized array and adds it to the cookie just fine.[/b] <?php $count= $_GET['id']; $name= $_POST['name']; $w= $_POST['w']; $h= $_POST['h']; $serialized_data = serialize (array ($count, $name, $w, $h)); setcookie("mycookie[$count]", $serialized_data, time()+13600); echo "Opening was Added<br><br>"; echo "$count"; echo "<br><br>"; echo "$name"; echo "$w"; echo "$h"; echo "<br><br>"; echo "<a href='index2.php'>Add another opening!</a><br>"; echo "<a href='show.php'>Show!</a><br>"; ?> [b]Where the problem is when you click show (show.php) you see the serialized data. I am trying to restore the data with an unserialize (see below line 8), but when I add that line of code, my data disappears instead of being stripped of the serialization. When I remove line 8, the data displays serialized. So something is wrong, I guess with my unserialize method.[/b] <?php if($_COOKIE["mycookie"]) { foreach( $_COOKIE[mycookie] as $key => $value) { $value2 = unserialize ($value); echo "Number $key : Description $value2"; } echo "<a href='index2.php'>Add Opening</a><br><br>"; } else { print "No Items in your cart<br><br>"; echo "<a href='index2.php'>Add Opening</a><br><br>"; } ?> I have tried all weekend long and could use some help! Thanks - Late Nighter
×
×
  • 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.