Jump to content

[SOLVED] marging array in to a session


aebstract

Recommended Posts

I've got a script I am working on to function as a shopping cart. I can add items to the cart and it sets in my session, though I am trying to get a way so that if I was to add another item or even update the items that I already have, I can update my session with the new numbers. Be it adding, subtracting or keeping the values the same. I am trying to use array_merge, though atm when I go back and try to add more items, it simply starts numbering at the bottom and adding more. Right now I have 18 items, and here is what you get after going back and adding more items a few times:

1 - 10

2 - 0

3 - 0

4 - 0

5 - 0

6 - 0

7 - 0

8 - 0

9 - 0

10 - 0

11 - 0

12 - 0

13 - 0

14 - 0

15 - 0

16 - 0

17 - 0

18 - 0

19 - 15

20 - 6

21 - 3

22 - 2

23 - 9

24 - 0

25 - 0

26 - 0

27 - 0

28 - 0

29 - 0

30 - 0

31 - 0

32 - 0

33 - 0

34 - 0

35 - 0

36 - 0

 

As you can see, it just keeps going. This is a problem because I am matching up the stored id of the items with the database, which if it continues down like this, it won't match up. Here is what I have:

 



if (isset ($_POST['submit']))
{

if (isset ($_SESSION['cart'])){
$_SESSION['cart'] = array_merge ($_SESSION['cart'], $_POST['items_quantity']);
} else {
foreach ($_POST['items_quantity'] as $product_id => $quantity) {
$_SESSION['cart'] = $_POST['items_quantity'];
}
}
}

Link to comment
Share on other sites

The reason I am not just replacing is:

 

If they added 5 of item one to start, then they went and added 10 more of item one.. they should have 15 of item one. If I replaced it, they would just end up with 10 of item one. Another example for not just replacing it with the new values is: If they add 10 of item one, and then go back and add 10 of item two, it would replace item one with 0 and they would be left with only item two. I hope that makes sense. I need some way for it to check each line in the array and add the qty's of the previous values with the qty's of the new values and keep it all in the session.

Link to comment
Share on other sites

I understand what you mean there, but i think I did not explain myself fully.

 

I am assuming (which we all know what that does) you have an order screen that has all of your products listed with input boxes by them.  If you made it so that when a user goes into the input screen the page calls the session array and places the values of the array as the default values. example below

 

The values of the array

$product = $_session['product'];
$product[1] = 0;
$product[2] = 5;
$product[3] = 0;
$product[4] = 1;

 

The order page code (would have to be a forum

<?php
echo 'Product1: <input type="text" name="product1" value="'.$product[1].'"><br>';
echo 'Product2: <input type="text" name="product2" value="'.$product[2].'"><br>';
echo 'Product3: <input type="text" name="product3" value="'.$product[3].'"><br>';
echo 'Product4: <input type="text" name="product4" value="'.$product[4].'"><br>';
?>

 

Then when they submit it, it all goes back to the session array. (as below)

 

The submit page

$product[1] = $_POST[product1];
$product[2] = $_POST[product2];
$product[3] = $_POST[product3];
$product[4] = $_POST[product4];

$_session['product'] = $product

 

Of course you would want to sterilize it so that you dont get negative numbers or text, but in basic it should work.

 

~D

 

p.s. Since PHP is a dirty language that doesnt require you to specify array sizes, you could add more products by adding another text input box and adding it to the submit page.

Link to comment
Share on other sites

 

 

 

Then when they submit it, it all goes back to the session array. (as below)

 

The submit page

$product[1] = $_POST[product1];
$product[2] = $_POST[product2];
$product[3] = $_POST[product3];
$product[4] = $_POST[product4];

$_session['product'] = $product

 

 

This is still going to just override the information I have already saved rather than merging it together as one/new value, isn't it?

Link to comment
Share on other sites

More or less yes, but the information gets saved because it is reinserted into the order form.  So if there is an 8 in $product[1] then the Product 1 text box will have an 8 in it when they load the page and will still have an 8 in it when they hit submit, unless they edit.

 

So yes, it will be over writinng the data that is already there, but if they dont change it, it will over write with whatever it started with.

 

Give me a few minutes and i will write a working example script of what i am talking about.

 

~D

Link to comment
Share on other sites

Yeah I am lost. Here was my attempt, which is failing to work correctly:

 

if (isset ($_SESSION['cart'])){
foreach ($_SESSION['cart'] as $product_id => $quantity) {
$parts .= "<tr cellspacing=6><td align=center bgcolor=\"$row_color\">$loc</td><td bgcolor=\"$row_color\" width=\"100\">$pn</td><td bgcolor=\"$row_color\" width=\"330\">$desc</td><td></td><td><input type=\"text\" size=\"5\" name='items_quantity[{$pid}]' value=\"$quantity\" /></td></tr>";
}
} else {
$parts .= "<tr cellspacing=6><td align=center bgcolor=\"$row_color\">$loc</td><td bgcolor=\"$row_color\" width=\"100\">$pn</td><td bgcolor=\"$row_color\" width=\"330\">$desc</td><td></td><td><input type=\"text\" size=\"5\" name='items_quantity[{$pid}]' value=\"0\" /></td></tr>";
}

 

Also, something that is important is that the items have a unique id in the database, which is what we should be matching these results up with. (pid) This way, if they were to go to another order page (there will be multiple order forms, different ones for each machine) it has the values set in the boxes the correct way it should.

Link to comment
Share on other sites

Here is the code I promised that works.  Copy and paste it into a .php file and give it a run.

 

<?php
// Since this is an example, the page calls itself using PHP_SELF.
// In production using PHP_SELF can cause a lot of problems.
//
// creates a session
session_start();
setcookie(session_name(), session_id(), 0, "/", "");
// destroy the session, which resets the values
IF($_GET['log'] == "out"){
session_destroy();
ECHO'<meta  http-equiv="refresh" content="1; url='.$_SERVER['PHP_SELF'].'" />';
};
IF($_POST['order'] == 'order'){
// the oder has been placed
$temp[1] = $_POST['product1'];
$temp[2] = $_POST['product2'];
$temp[3] = $_POST['product3'];
$temp[4] = $_POST['product4'];

$_SESSION['product'] = $temp;
echo 'Stuff has been changed.  Redirecting';
ECHO'<meta  http-equiv="refresh" content="1; url='.$_SERVER['PHP_SELF'].'" />';
}else{
$product = $_SESSION['product'];
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
echo 'Product1: <input type="text" name="product1" value="'.$product[1].'"><br>';
echo 'Product2: <input type="text" name="product2" value="'.$product[2].'"><br>';
echo 'Product3: <input type="text" name="product3" value="'.$product[3].'"><br>';
echo 'Product4: <input type="text" name="product4" value="'.$product[4].'"><br>';
echo '<input type="submit" name="order" value="order">';
}
?>

 

As for a PID, i have to think about that for a few minutes, but I do believe it is addressable in this context.

 

~D

Link to comment
Share on other sites

As I stand now, I have my form display correctly, when I put in a value and go back, it updates the 0's to the new numbers. Problem is that I am doing this within a while so that I can use my unique id's, though when I put a foreach to split my session array up and then show the form, it displays each row 18 times (or however many items I have).

 

$result2 = mysql_query("SELECT * FROM parts WHERE MCHN='PH' ORDER BY LOC ASC") or DIE(mysql_error());
while($r2=mysql_fetch_array($result2))
{
$pid=$r2["id"];
$loc=$r2["LOC"];
$pn=$r2["PN"];
$desc=$r2["DESC"];

$row_color = ($row_count % 2) ? $color1 : $color2;



if (isset ($_SESSION['cart'])){
foreach ($_SESSION['cart'] as $product_id => $quantity) {
$parts .= "<tr cellspacing=6><td align=center bgcolor=\"$row_color\">$loc</td><td bgcolor=\"$row_color\" width=\"100\">$pn</td><td bgcolor=\"$row_color\" width=\"330\">$desc</td><td></td><td><input type=\"text\" size=\"5\" name='items_quantity[{$pid}]' value=\"$quantity\" /></td></tr>";
}
} else {
$parts .= "<tr cellspacing=6><td align=center bgcolor=\"$row_color\">$loc</td><td bgcolor=\"$row_color\" width=\"100\">$pn</td><td bgcolor=\"$row_color\" width=\"330\">$desc</td><td></td><td><input type=\"text\" size=\"5\" name='items_quantity[{$pid}]' value=\"0\" /></td></tr>";
}



$row_count++;
}

Link to comment
Share on other sites

Ok, i have been looking at your code of about 30 minutes now, and I think i understand your problem, and i think I have a cure.

 

In the database call you get the product id out (which i am assuming is the $pid )

 

You are referencing everything in the array by the Product Id:

So if the product id is 1234 the spot in the array would be $_SESSION['cart'][1234]

If the product id is 4321 the array spot is $_SESSION['cart'][4321]

 

So rather then using a 'foreach'  why dont you just call it using the variable $pid ?

the call would look like this

 

<?php
$result2 = mysql_query("SELECT * FROM parts WHERE MCHN='PH' ORDER BY LOC ASC") or DIE(mysql_error());
while($r2=mysql_fetch_array($result2))
{
$pid=$r2["id"];
$loc=$r2["LOC"];
$pn=$r2["PN"];
$desc=$r2["DESC"];

$row_color = ($row_count % 2) ? $color1 : $color2;



if (isset ($_SESSION['cart'])){

$quantity = $_SESSION['cart'][$pid];
$parts .= "<tr cellspacing=6><td align=center bgcolor=\"$row_color\">$loc</td><td bgcolor=\"$row_color\" width=\"100\">$pn</td><td bgcolor=\"$row_color\" width=\"330\">$desc</td><td></td><td><input type=\"text\" size=\"5\" name='items_quantity[{$pid}]' value=\"$quantity\" /></td></tr>";

} else {
$parts .= "<tr cellspacing=6><td align=center bgcolor=\"$row_color\">$loc</td><td bgcolor=\"$row_color\" width=\"100\">$pn</td><td bgcolor=\"$row_color\" width=\"330\">$desc</td><td></td><td><input type=\"text\" size=\"5\" name='items_quantity[{$pid}]' value=\"0\" /></td></tr>";
}



$row_count++;
}
?>

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.