aebstract
-
Posts
1,105 -
Joined
-
Last visited
Posts posted by aebstract
-
-
Noone has a clue as to how I can control these elements?
-
I think I am getting closer with this:
foreach ($_SESSION['cart'] as $id => $material){ foreach ($material as $row){ print "ID: $id, Meterial: $material, Qty: $row[0], Size: $row[1]"; } }
but still nope, that's showing:
ID: 8, Meterial: Array, Qty: , Size:
ID: 8, Meterial: Array, Qty: , Size:
-
Using:
$_SESSION['cart'][$id][mildsteel] = array(qty => 1, size => 4.5);
I get this array:
Array ( [8] => Array ( [mildsteel] => Array ( [qty] => 1 [size] => 4.5 ) [stainless] => Array ( [qty] => 1 [size] => 4.5 ) ) )
Now I am trying to control it so that I can display the cart correctly, which for this let's say there's a table displaying each item. For each material of each item I need to show a new row. The foreach I have below will not work correctly.
foreach ($_SESSION['cart'] as $id){ foreach ($id as $material){ foreach ($material as $row){ print "ID: $id, Meterial: $material, Qty: $row[0], Size: $row[1]"; } } }
Basically need to somehow use that array to do something like this:
ID: 8, Material: mildsteel, QTY: 1, Size, 4.5
ID: 8, Material: stainless, QTY: 1, Size, 4.5
and for it to be able to do this for every item ID and each different Material under each id. Hope this makes sense, and I'm still playing with it to see if I can control it.
-
Is this method:
foreach ($_SESSION['cart'] as $id => $array2){ print "item: $id<br />"; foreach ($array2 as $row => $item){ if ($row == 0){ print "Qty: $item<br />"; } if ($row == 1){ print "Material: $item<br />"; } } }
The best for getting and using the items? I don't think an if statement should be necessary, but I'm not entirely sure. This resulted with:
item: 7
Qty: 1
Material: mild steel
-
Cool, tried out this:
$_SESSION['cart'] = array($specialid => array('1', 'mild steel'));
which easily gave me:
Array
(
[7] => Array
(
[0] => 1
[1] => mild steel
)
)
Which, I'm gonna hope all I have to do is run two loops to grab all the information. Thanks
-
I've tried a few different things, I'm getting more comfortable with arrays, but..
$_SESSION['cart'] = array($specialid => '1', 'mild steel');
All this will do is make my array like this:
Array
(
[7] => 1
[8] => mild steel
)
Somehow I want more values for [7] without creating 8, since I had to reserve all of the keys for ids. Trying to make it possible something like:
Array
(
[7] => '1', 'mild steel', 'etc'
[8] => '2', 'stainless steel'
)
Also I'm sure there might be a special way to go about getting the extra values, if something like this is possible? Possibly inside the foreach loop.
-
I think this is solved for now, being able to:
foreach ($_SESSION['cart'] as $id=>$qty){ print "$id qty of $qty"; }
-
$_SESSION['cart'] = array($specialid => '1');
That should work, correct? I just don't know how to echo out the $_SESSION['cart']
-
Right. So instead use this structure:
$_SESSION['cart'][ItemID] = Quantity;
$_SESSION['cart'][4] = 4; $_SESSION['cart'][3] = 1;
So something like,
$_SESSION['cart'][$specialid] = 1;
right? With that, how would I work with the id and qty for each item? Like how can I set em as a variable or something?
-
Okay, I'm gonna work with that in the morning. Currently all I am doing to make the cart is add the id of the item on to the end of a string. So I have $_SESSION['cart'] = "4,4,4,4"; and just if I wanted to add item 3 it would just get added to the end: 4,4,4,4,3. This leaves me pretty much option-less to do anything else ;(
-
My current array when printed looks like this:
array(7) {
[0]=>
string(1) "8"
[1]=>
string(1) "8"
[2]=>
string(6) "11;4.2"
[3]=>
string(1) "7"
[4]=>
string(6) "11;4.3"
[5]=>
string(6) "11;4.2"
[6]=>
string(6) "11;4.2"
}
Needs to be formatted something like
array (
'11;4.2' => 3,
'11;4.2' => 3,
'11;4.2' => 3,
'8' => 2,
'7' => 1
)
Though I'm not sure how to set it up to work like that.
-
Well if I have 3 of the same item and want to update it to 10, or 1... I'm having trouble figuring out how to easily update my quantity because of the way it is listed.
edit: I see what you mean but I am pretty stupid with arrays, gonna fiddle around and see if I can figure it out.
-
Just want some input on your thoughts for an array structure. Right now, for a shopping cart, each item is added as it's own id to a session and then split up in to an array. So 4,4,4,5,3. Then after splitting that up a foreach ($contents as $id=>$qty) {
will tell us that we have qty 3 of item 4, 1 of 5 and 1 of 3. Would it be better to somehow make this a deeper array, something like:
array('item1' => array('1', '1', '1'),
'item2' => array('1', '1'));
Then I can display item 1 and know I have 3 of it and item2 I have 2 of it? If so how do I go about storing something like this in a variable and how do I go about grabbing those results?
-
You told me to change it from what it is to what it is? O.o
I think the problem originates from this line;
$specialid = $id.";".$sizenum;
Try changing it to this for now...
$specialid = $id.";".$sizenum;
This solved it, I wonder why it mattered that I was using the html version of ;. I mean, it recognized it to explode it and all, weird stuff.. Thanks everyone for their time and input
-
Right now it is getting set with:
<?php $product_cat = $_GET['product_cat']; $checknum = 1; $result = mysql_query("SELECT * FROM `p_products` WHERE category='$product_cat'") or DIE(mysql_error()); while($r = mysql_fetch_array($result)) { $id = $r['id']; $partnumber = $r['partnumber']; $partname = $r['partname']; $description = $r['description']; $price = $r['price']; $option1 = $r['option1']; $specialid = $id; $postname = image.$id; $postsize = size.$id; if (isset($_POST[$postname])) { $error = 0; if (isset($_POST['hideme'])) { if (empty($_POST[$postsize])){ $error = 1; $errormsg = "You must fill in a Pipe Size for $partname."; } else { $sizenum = $_POST[$postsize]; $specialid = $id.";".$sizenum; } } if ($error == 1){ $errordisplay .= "<div id=\"error_box\"> $errormsg</div>"; } else { $cart = $_SESSION['cart']; if ($cart) { $cart .= ','.$specialid; } else { $cart = $specialid; } $_SESSION['cart'] = $cart; header("Location: index.php?page=cart"); exit; } } if (!empty($option1)){ $clicker = " Pipe Size: <input type=\"text\" size=\"4\" value=\"\" name=\"size$id\" style=\"background-color: #FFFFFF; border: solid 1px #000000; height: 20px; font-size: 10px;\" /><br /> <div class=\"product_image\"> <input type=\"hidden\" name=\"hideme\" value=\"needsize\" /> <input type=\"image\" value=\"submit$id\" src=\"addtocart.jpg\" width=\"144\" height=\"34\" border=\"0\" alt=\"Add to Cart\" name=\"image$id\"> </div>"; } else { $clicker = "<br /><div class=\"product_image\"> <input type=\"image\" value=\"submit$id\" src=\"addtocart.jpg\" width=\"144\" height=\"34\" border=\"0\" alt=\"Add to Cart\" name=\"image$id\"> </div>"; } if( $odd = $checknum%2 ) { $content .= " <form action=\"index.php?page=product_page&product_cat=$product_cat\" method=\"post\" name=\"add_product$id\"> <div class=\"product_left\"> <div class=\"product_image\"><img src=\"products/$partnumber-cart.jpg\" class=\"jkimagelarge\" title=\"products/$partnumber.jpg\" height=\"100\"/></div> <div class=\"product_info\"> $partname <br /> $partnumber <br /> $$price <br /> $clicker </div> </div></form>"; } else { $content .= "<div class=\"product_right\"> <form action=\"index.php?page=product_page&product_cat=$product_cat\" method=\"post\" name=\"add_product$id\"> <div class=\"product_image\"><img src=\"products/$partnumber-cart.jpg\" class=\"jkimagelarge\" title=\"products/$partnumber.jpg\" height=\"100\"/></div> <div class=\"product_info\"> $partname <br /> $partnumber <br /> $$price <br /> $clicker </div> </div></form>"; } $checknum ++; } ?>
But.. it is set correctly, let me add a few items to the cart without the value and then throw one in the middle with it and show you the echo results:
1,9,8,11;4.5,7,8 array(6) { [0]=> string(1) "1" [1]=> string(1) "9" [2]=> string(1) "8" [3]=> string(10) "11;4.5" [4]=> string(1) "7" [5]=> string(1) "8" } ID is 1 and there is no size! ID is 9 and there is no size! ID is 8 and there is no size! ID is 11; and Size is 4.5 ID is 7 and there is no size! ID is 8 and there is no size!
You can see it is set correctly, and it's splitting them all correctly but leaving that ;. What section of my code do you think you will need to look at? Not sure if what I posted is all, but it's where it gets set at.
-
I logged out on my website and logged back in and added the same item with a different second value of 4.2, just so you know:
11;4.2 array(1) { [0]=> string(10) "11;4.2" } ID is 11; and Size is 4.2
-
that is strange, just to give it a try, pass the variable to the function rather than using the $_SESSION inside the function;
function showCart2($cart) { #$cart = "11;5 5/8"; echo "$cart"; $var = explode(',',$cart); echo '<pre>'; var_dump($var); echo '</pre>'; $cnt = count($var); for ($i=0; $i<$cnt; $i++) { list($id, $size) = explode(";", $var[$i]); if (isset($size)) { echo "ID is $id and Size is $size <br />"; } else { echo "ID is $id and there is no size! <br />"; } } } showCart2("11;5 5/8");
This worked fine.
-
First off, I've been copy and pasting.. no need to bold it and make me feel as if you're talking down of me..
11;5 5/8
array(1) {
[0]=>
string(12) "11;5 5/8"
}
ID is 11; and Size is 5 5/8
-
well in that code your form doesn't have an action or method or anything. This is why it is good to have actual code presented, and if this is.. then there is problem 1. Also, if you're gonna have a submit button for each bin item, each bin item is also going to have to have its own form, so the actual form will have to be included in to the loop. If this is true, each form will need it's own name, I would suggest using your id to uniquely name each form.
-
when $cart is echoed it displays: 11;5 5/8
then the rest of the script runs and the second set of echos sends: ID is 11; and Size is 5 5/8
which should be: ID is 11 and Size is 5 5/8
-
I just posted it:
cart is this one = 11;5 5/8
and the other echo is = ID is 11; and Size is 5 5/8
-
Like I said, id is being set to 5; and the size is setting as 1 1/2 I need the ; gone. Hrm..
With the posted code, I'm getting this echoed:
11;5 5/8ID is 11; and Size is 5 5/8
-
You're making your submit button's name Bin32, so in order to get the value you need to use Bin32. Might be needing some more code to help you and what exactly you're trying to do?
-
It displays the number.. 5;1 1/2 why?
[SOLVED] Multiple array values for a single key?
in PHP Coding Help
Posted
Well I got this:
Which turns out to be:
8 mildsteel qty 1
8 mildsteel size 4.5
8 stainless qty 1
8 stainless size 4.5
Which is obviously not right, need it to do something like
8 mildsteel qty 1 size 4.5
8 stainless qty 1 size 4.5
Still trying to get it under control though, anyone?