Edit Global Variable?
Moderator: General Moderators
Edit Global Variable?
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.
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.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
um... maybe I'm misunderstanding what you're after... but read http://www.w3schools.com/php/php_sessions.asp
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?
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?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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?
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?
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
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>- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
try the code I posted and then use
to view the results - you'll get the idea.
Code: Select all
print_r($_POST)- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK