Page 1 of 2
Edit Global Variable?
Posted: Mon Feb 19, 2007 12:57 pm
by gfirman
Hi,
What I am doing is a maths calculation. The user enters the quantity into a form, and then clicks buy. Another page is then opened confirming what has been bought and the cost.
The problem is how to calculate the cost, as it is randomly generated using the following code:
srand((double)microtime()*1000000);
echo rand(0,100);
The prices are generated when the user opens up the first page. I use these prices on the next page to determine the total cost.
I was think of using global variables, but can you edit them every time the page is opened (assign them a generated number).
Any help would be greatly appreciated.
Posted: Mon Feb 19, 2007 1:18 pm
by Kieran Huggins
If the cost is random, what does it matter?
Posted: Mon Feb 19, 2007 1:30 pm
by gfirman
It does matter - the user must pay the amount that is displayed to them.
This is a odd question I know, but its what needs to be done
Posted: Mon Feb 19, 2007 1:34 pm
by Kieran Huggins
If you use session_start() at the top of each page, you can store stuff in the $_SESSION array.
Is that what you're looking for?
Posted: Mon Feb 19, 2007 1:49 pm
by gfirman
I don't think so......wouldn't that start a new session?
Posted: Mon Feb 19, 2007 1:56 pm
by Kieran Huggins
it does nothing if you already have a session started, otherwise it will make the $_SESSION array available to you.
Posted: Mon Feb 19, 2007 2:17 pm
by gfirman
Ya, thats no good then, any other suggestions?
Posted: Mon Feb 19, 2007 2:33 pm
by Kieran Huggins
um... maybe I'm misunderstanding what you're after... but read
http://www.w3schools.com/php/php_sessions.asp
Posted: Mon Feb 19, 2007 2:53 pm
by gfirman
No I don't think you understand me. I'll show you:
PAGE 1 - which includes a generated number which is stored in a variable:
-----------------------------
Item Price Quantity
Gun $100 2 (user enters amount)
etc.....
.....
...
..
.
.
Buy -(Button)
----------------------------
The person then enters the amount and clicks buy.
PAGE 2 will look like this:
--------------------------------------
You have bought 2 guns at a price of $200
--------------------------------------
What I need to do is bring the variable that stores the price to this page to calculate the total price.
.....any ideas?
Posted: Mon Feb 19, 2007 3:00 pm
by John Cartwright
nope, sessions are what you are after.
Posted: Mon Feb 19, 2007 3:08 pm
by gfirman
I have the session up and running. users can log in and view there account details. But this is different. When i open this page a different set of numbers must be displayed every time. Hence I'm using the random number generator. This is placed in the php file itself not the session.
What is the best thing to do?
Move the variables to the session - this means having to change the variables each time the page is opened.
OR
Keep the variables in the page (PAGE 1 as referred to above) - this means having to access the session variables and change them every time the page is open and new prices are set.
Got an idea of what I'm saying?
Posted: Mon Feb 19, 2007 3:46 pm
by Kieran Huggins
you could submit the price as a hidden form field (not good practice) like this:
Code: Select all
<form method="post" action="?">
<input type="hidden" name="item['1']['price']" value="100"/>
How many guns for $100? <input type="text" name="item['1']['qty']"/>
<input type="submit"/>
</form>
Posted: Tue Feb 20, 2007 12:14 pm
by gfirman
Thanks for that, I'v been messing around with it and can't get it to work,
Could you explain the code more?
Posted: Tue Feb 20, 2007 12:18 pm
by Kieran Huggins
try the code I posted and then use
to view the results - you'll get the idea.
Posted: Thu Feb 22, 2007 6:58 am
by Ollie Saunders
Single quotes the name attribute are, at best, redundant and, at worst, aren't going to behave how you expect.