searls03 Posted February 4, 2012 Share Posted February 4, 2012 so I need some help passing these variable from this page to final.php. how do I pass these arrays? I know if it were singled....not arrayed, I could use hidden fields in a form and echo them out....but these are multiples....not singled. The form way is prefered.....but it doesn't have to be. I just need these passed to the page where I am going to process them. I am not good at working with arrays. Thanks in advance <?php include_once("connect.php"); session_start(); foreach($_POST["product"] AS $key => $val) { $product = $val; $month = $_POST['month'][$key]; $day = $_POST['day'][$key]; $year = $_POST['year'][$key]; $date = $_POST['date'][$key]; $price = $_POST['price'][$key]; $qty = $_POST['qty'][$key]; $id = $_POST['id'][$key]; $total = $_POST['total'][$key]; $academy = $_POST['academy'][$key]; $priceunit = $price * $qty; } ?> Quote Link to comment Share on other sites More sharing options...
noXstyle Posted February 4, 2012 Share Posted February 4, 2012 how about using sessions since you have them: foreach($_POST["product"] AS $key => $val) { $_SESSION['product']=$val; $_SESSION['month'] = $_POST['month'][$key]; ... } to access on next page just call the $_SESSION[key] *Edit: if there is arrays that you are passing on session vars, you might need to serialize them. just use serialize() while assigning and unserialize() on retrieval. Quote Link to comment Share on other sites More sharing options...
digibucc Posted February 4, 2012 Share Posted February 4, 2012 serialize the array to a string, pass it, unserialize it on receipt. http://php.net/manual/en/function.serialize.php http://php.net/manual/en/function.unserialize.php Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 so like: <?php include_once("connect.php"); session_start(); session_destroy(); session_start(); $sql = mysql_query("SELECT * FROM cart"); while($row = mysql_fetch_array($sql)){ $cid = $row["cart_id"]+1; $_SESSION['cart_id'] = $cid; } if($_POST['payment2']){ $sql = "INSERT INTO cart (cart_id) VALUES('".$_SESSION['cart_id']."' )"; $rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error()) ; echo $cid; echo $sql; } foreach($_POST["product"] AS $key => $val) { $_SESSION['product']['key'] = $product; $_SESSION['month']['key'] = $month; $_SESSION['day'] ['key']= $day; $_SESSION['year']['key'] = $year; $_SESSION['date']['key'] = $date; $_SESSION['price']['key'] = $price; $_SESSION['qty']['key'] = $qty; $_SESSION['id']['key'] = $id; $_SESSION['total']['key'] = $total; $_SESSION['academy']['key'] = $academy; $_SESSION['priceunit']['key'] = $priceunit; $sql = "INSERT INTO transactions (price, product, quantity, product_id, priceunit, month, day, year, academy, date) VALUES('$priceunit', '$product', '$qty', '$id', '$price', '$month', '$day', '$year', '$academy', '$date' )"; $rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error()); } then where setting: <?php include_once("connect.php"); session_start(); foreach($_POST["product"] AS $key => $val) { $_SESSION['product'] = $val; $_SESSION['month'] = $_POST['month'][$key]; $_SESSION['day'] = $_POST['day'][$key]; $_SESSION['year'] = $_POST['year'][$key]; $_SESSION['date'] = $_POST['date'][$key]; $_SESSION['price'] = $_POST['price'][$key]; $_SESSION['qty'] = $_POST['qty'][$key]; $_SESSION['id'] = $_POST['id'][$key]; $_SESSION['total'] = $_POST['total'][$key]; $_SESSION['academy'] = $_POST['academy'][$key]; $_SESSION['priceunit'] = $price * $qty; } ?> Like this? Quote Link to comment Share on other sites More sharing options...
noXstyle Posted February 4, 2012 Share Posted February 4, 2012 What just happened? The setting is fine. But what i meant when you handle the data you use it like: $_SESSION['month'], where month is your key. And you don't want to call session_destroy() on the beginning, otherwise all the data you have set will be lost. and you would use it like: $product = $_SESSION['product']; If you want to keep your foreach loop you've got to loop through the session superglobal. For instance: foreach($_SESSION as $k=>$v) { $$k = $v; } After that you should be able to access the variables via normal $priceunit etc.. (just like in your sql statement). Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 5, 2012 Author Share Posted February 5, 2012 I am not quite sure what you mean. could you like write the first 3 pieces for me? that might help it make a bit more sense. as said before, I am not good with arrays or loops. Quote Link to comment Share on other sites More sharing options...
noXstyle Posted February 5, 2012 Share Posted February 5, 2012 Ok, when you are setting the values to session (i.e. the page from which you need to transfer the variables): include_once("connect.php"); session_start(); foreach($_POST["product"] AS $key => $val) { $_SESSION['product'] = $val; $_SESSION['month'] = $_POST['month'][$key]; etc... Just like you have on your code. And then on the page where your need to retrieve values: <?php include_once("connect.php"); session_start(); foreach($_SESSION AS $key => $val) { $$key = $val; } $sql = "INSERT INTO transactions (price, product, quantity, product_id, priceunit, month, day, year, academy, date) VALUES('$priceunit', '$product', '$qty', '$id', '$price', '$month', '$day', '$year', '$academy', '$date' )"; $rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error()); } Should work like a charm. If you dont understand the $$key = $val, check out php's variables variable. Basically it's just creating a variable out of your session key. Quote Link to comment Share on other sites More sharing options...
digibucc Posted February 5, 2012 Share Posted February 5, 2012 your way is interesting, similar to mine but not quite, what do you think of mine (obviously without your checks, as this wasn't meant to be live just an example): <?php foreach ($_POST as $key=>$value){ if ($value != '' && $value != 'Submit'){ $cols .= mysql_real_escape_string($key). ', '; $vals .= '\''. mysql_real_escape_string($value). '\', '; } } $columns = substr($cols,0,-2); // trim trailing "'," , $values = substr($vals,0,-2); $sql="INSERT INTO table ( $columns )VALUES ( $values )"; ?> just curious. i like to compare my solutions with others, as there is always a better way to do the things we do Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 5, 2012 Author Share Posted February 5, 2012 could you just show me one more line after $$key=$val? i am much more of a visual learner and I want to make sure that I do it right......I know that sometimes that first one can be different than the others... Quote Link to comment Share on other sites More sharing options...
digibucc Posted February 5, 2012 Share Posted February 5, 2012 in his reply, $$key => $val is creating a variable (hence the first $) , with whatever name $key has from the loop, which would be the submitted session var's key that's where this list of variables comes from '$priceunit', '$product', '$qty', '$id', '$price', '$month', '$day', '$year', '$academy', '$date' Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 5, 2012 Author Share Posted February 5, 2012 ok....so I don't have to add anything to it.....so like this? <?php include_once("connect.php"); session_start(); foreach($_SESSION AS $key => $val) { $$key = $val; } $sql = "INSERT INTO transactions (price, product, quantity, product_id, priceunit, month, day, year, academy, date) VALUES('$priceunit', '$product', '$qty', '$id', '$price', '$month', '$day', '$year', '$academy', '$date' )"; $rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error()); session_destroy(); $sql = mysql_query("SELECT * FROM cart"); while($row = mysql_fetch_array($sql)){ $cid = $row["cart_id"]+1; $_SESSION['cart_id'] = $cid; } ?> only problem is that it does insert one correctly, but then the rst of them insert blank. Quote Link to comment Share on other sites More sharing options...
digibucc Posted February 5, 2012 Share Posted February 5, 2012 when you say only one inserts, do you mean one key/value pair or one entire record? that insert is only inserting one record, and i only see where information for one record is being processed - more info please? Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 6, 2012 Author Share Posted February 6, 2012 it inserts one record properly......so all the fields insert right for one record.....but after that, they are inserting blank records. I may also not have the code written properly,so if there is an error, please tell:) Quote Link to comment Share on other sites More sharing options...
digibucc Posted February 6, 2012 Share Posted February 6, 2012 and sorry to keep clarifying, but it is definitely inserting empty rows - not just skipping the rest? and actually, what are the rest? you are pulling one session into key value pairs - where would the extra records come from? Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 6, 2012 Author Share Posted February 6, 2012 yes Quote Link to comment Share on other sites More sharing options...
digibucc Posted February 6, 2012 Share Posted February 6, 2012 var dump session before the foreach, like this: <?php include_once("connect.php"); session_start(); var_dump($_SESSION); foreach($_SESSION AS $key => $val) { $$key = $val; } ?> do you see all you would expect there, or just one record? copy and past the var_dump result Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 6, 2012 Author Share Posted February 6, 2012 the picture shows the result: I posted both DVD:TKD and Sparring Package This code only let it insert the one instead of one and a blank. the original code posted the right one about 7 times then inserted the blank one..............it wasn't doin this before. Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 6, 2012 Author Share Posted February 6, 2012 array(12) { ["product"]=> string(7) "DVD:TKD" ["month"]=> string(13) "February 2012" ["day"]=> string(2) "05" ["year"]=> string(4) "2012" ["date"]=> string(10) "2012-02-05" ["price"]=> string(2) "69" ["qty"]=> string(1) "1" ["id"]=> string(1) "4" ["total"]=> string(1) "6" ["academy"]=> string(0) "" ["priceunit"]=> int(0) ["cart_id"]=> string(1) "5" } I guess it is doing something now that it wasn't before, idk why, but it is. I realize that didn't change the results, but it was only doing two records when I tried two before. this is strange. Quote Link to comment Share on other sites More sharing options...
digibucc Posted February 6, 2012 Share Posted February 6, 2012 so should session dump that be showing more than one record? or is it simply that mysql is inserting a blank one when there should only be one? Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 6, 2012 Author Share Posted February 6, 2012 currently it is doing exactly what the picture shows regardless of how many I try to insert. Quote Link to comment Share on other sites More sharing options...
digibucc Posted February 6, 2012 Share Posted February 6, 2012 so from a blank table, you run that and it inserts it 7 times and then a blank row? and what i'm asking is if that session variable should have info on more than one product. regardless of what it is doing, what should it do should it pass the info for one product at a time, exactly what that session var shows when dumped - or should there be more products there? Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 6, 2012 Author Share Posted February 6, 2012 the picture shows exactly what it is doing. I want it to post all the products that the sessions are holding. yes there should be more than one if there is more than one present. it should look similar to product 1 price 1 product id1 ,etc product 2 price 2 product id2 ,etc and so forth. it shouldn't be doing what the picture show. does this make a bit more sense? Quote Link to comment Share on other sites More sharing options...
digibucc Posted February 6, 2012 Share Posted February 6, 2012 so there are two different things here. something i sup with your syntax, and the session isn't passing the additional products. the picture shows exactly what it is doing. it looks like multiple attempts each with different results, i am just trying to confirm every entry in that picture came from one insert. by that i would mean empty the table, try one insert, and post that result, if that's is what that picture is ... that's way different. Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 6, 2012 Author Share Posted February 6, 2012 ok, that is exactly one insert. I had cleared the table and ran the query, that's what the result was. Quote Link to comment Share on other sites More sharing options...
digibucc Posted February 6, 2012 Share Posted February 6, 2012 are there any more loops in may be inside of? as this stands: <?php include_once("connect.php"); session_start(); foreach($_SESSION AS $key => $val) { $$key = $val; } session_destroy();//small change, vars were made so no need to keep it open to to do the insert $sql = "INSERT INTO transactions (price, product, quantity, product_id, priceunit, month, day, year, academy, date) VALUES('$priceunit', '$product', '$qty', '$id', '$price', '$month', '$day', '$year', '$academy', '$date' )"; $rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error()); $sql = mysql_query("SELECT * FROM cart"); while($row = mysql_fetch_array($sql)){ $cid = $row["cart_id"]+1; $_SESSION['cart_id'] = $cid; } ?> it should be performing one insert. i can't tell you why it's inserting so many, and then missing info on some, etc. that is very odd and i'd need additional before and after code to help more.... sorry. Quote Link to comment 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.