Jump to content

pass variable


searls03

Recommended Posts

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;


}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

post-112513-13482403228657_thumb.png

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.