PHP VARIABLES [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

PHP VARIABLES [SOLVED]

Post by YoussefSiblini »

Hi guys,
I have two pages:
first.php and second.php
I want to pass a variable from first page to second page and use this variable content as a variable.

first.php code:

Code: Select all

$myVariable .= '$myArr = array("Vijay","Joshi","Vikaspuri", "Delhi",27); $excel->writeLine($myArr); ';
second.php
I want to get the

Code: Select all

$myArr = array("Vijay","Joshi","Vikaspuri", "Delhi",27); $excel->writeLine($myArr); 
only, so I want $myArr to activate which is inside the $myVariable.

I hope you understood what I mean.

Joe
Last edited by YoussefSiblini on Mon Apr 23, 2012 12:22 pm, edited 1 time in total.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: PHP VARIABLES

Post by x_mutatis_mutandis_x »

If you don't mind me asking, why are you storing a syntax as a string in another variable. I'm not sure what you are trying to achieve here.
But anyways, you can use eval to execute the syntax content in $myVariable. Something like this (in second.php)

Code: Select all

eval($myVariable);
// now you can access $myArr
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: PHP VARIABLES

Post by YoussefSiblini »

Thank you for your reply,
I have a shopping cart in the first page and the second page is a result page that activate after the user finish paying.
What I want to achieve is create am excel file in the second page that contains the order details.
So lets say the user bought an apple and an orange, what I want to do is add the apple details to the first line of excel file and the orange details to the second line.
I am not sure how to pass the two (orange and apple) details separately to the second page, so what I am doing is:

Code: Select all

foreach ($_SESSION["cart_array"] as $each_item)
{
    $myArr = array("Vijay","Joshi","Vikaspuri", "Delhi",27); $excel->writeLine($myArr);
}
Since their are 2 products (apple and orange in my array), the $myarr will create an apple line and an orange line in my excel file, but I want to create the excel file in my second page after the user pay.
So I am passing this line $myArr = array("Vijay","Joshi","Vikaspuri", "Delhi",27); $excel->writeLine($myArr); inside a variable called $myVariable to the second page and use the content inside it.
I thought about getting the session and get the info from them, but I got into a problem, which is if the user add products to the basket and then visit another page and click buy a different product, this way I will have different details of a different item inside the session.
heheh I know this is stupid way to use, I am new to php and this is a practice website where I am learning php from, I will be soooo soooo happy if their are a better way.

I know I am trying to learn and it is hard and complicated but in the same time I am finding php so fun to code and use.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: PHP VARIABLES

Post by x_mutatis_mutandis_x »

You can serialize the array in your first page, and send it to your second page through hidden post value on user submit. For example..

In your first page:

$myArr = array("Vijay",....);
....
<html>
....
<form action="secondPage">...<input type="hidden" name="excelOutput" value="<? serialize($myArr) ?>" /> ... </form>
....
</html>

And in your second page:
$myArr = unserialize($_POST['excelOutput']);
$excel->writeLine($myArr);

You can always send data accross the pages using a post, and capturing on user submit.
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: PHP VARIABLES

Post by YoussefSiblini »

Thank you,
I was reading more about this over the weekend and I did it exactly how you mentioned :)
Post Reply