Edit Global Variable?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Edit Global Variable?

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

If the cost is random, what does it matter?
gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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?
gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Post by gfirman »

I don't think so......wouldn't that start a new session?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

it does nothing if you already have a session started, otherwise it will make the $_SESSION array available to you.
gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Post by gfirman »

Ya, thats no good then, any other suggestions?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

um... maybe I'm misunderstanding what you're after... but read http://www.w3schools.com/php/php_sessions.asp
gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

nope, sessions are what you are after.
gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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>
gfirman
Forum Commoner
Posts: 30
Joined: Tue Nov 21, 2006 10:41 am

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

try the code I posted and then use

Code: Select all

print_r($_POST)
to view the results - you'll get the idea.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Single quotes the name attribute are, at best, redundant and, at worst, aren't going to behave how you expect.
Post Reply