Welcome to WebmasterWorld Guest from 54.196.231.129

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Complete beginner's question (have mercy)

Can PHP do this?

     
1:15 am on Jul 22, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 14, 2002
posts:200
votes: 0


Coming from a 'traditional' programming environment I have hard time adjusting to PHP; I have started by recoding some simple C programs in PHP. What gets me is statelessness of http. Here is a silly (pseudocode) example:

write "how many grades?
read n
total = 0
for i = 1 to n
begin
write "enter grade"
read grade
total = total + grade
end
write "your average is", total/n

How do you do this in PHP? (without hidden fields, session IDs, arrays ....)
I did "solve" it in a way that I am pretty sure is not right:
On the first page ask for n
On the second page generate form with n entries for values. Pass these via hidden fields (as an array) to the third php page which uses the loop to calculate the average. (OK, you are now laughing)
Please, point me in the right direction ...

1:22 am on July 22, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 26, 2002
posts:173
votes: 0


How do you do this in PHP? (without hidden fields, session IDs, arrays ....)

You don't. :D ;)

If you want to have a stateful-application, you have to deal with sessions (or other means of transporting state, i.e. through GET/POST Parameters, just like you have done), there is no way around that. Other than reformulating your problem/use-case as a stateless problem (if possible):
- Make a textbox and require the user to input one grade per line.

Your approach is actually pretty common and not as bad as you seem to think.

3:14 pm on July 22, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


Excellent response.

As noted, your struggle won't necessarily be with PHP, but the stateless connection which is both the greatness as well as the boon of HTTP. Coming from a "terminal-interface, always connected" type environment into the stateless world of this protocol is a big jump and takes some thinking before it all clicks, but rest assured, it will click.

I just wanted to add that PHP uses a lot of functions similar to C, and is itself written in C. I think once you nail down the whole "stateless" concept, you are going to love PHP.

3:42 pm on July 22, 2004 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8137
votes: 102


Your program, however, could be done, using Javascript and DOM, essentially putting the ap on the client computer and running it from there.


both the greatness as well as the boon of HTTP.

I think he meant 'bane'.... :-)

4:00 pm on July 22, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


hehe, yeah, I did. My spell-checker didn't catch that...
...no wait, I mean, that's how we say it in Wisconsin...
...no wait, I've got more excuses...

I wuz go-ing to menshun the clyent-syde (JavaScript) possumbilitees two, butt fourgot. Goode poynt.

:-)

4:03 pm on July 22, 2004 (gmt 0)

Preferred Member

joined:Apr 13, 2001
posts:372
votes: 0


Go pick up the O'Reilly PHP book. Its useful and should get you moving easily.
5:57 pm on July 22, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 1, 2003
posts:815
votes: 0


I gather you're thinking "3 pages to add a few numbers, sheesh."

For something this simple, of course you'll want to do this with one page. You can do this by having PHP check its input & figure out where it is:

"I got a list of numbers? I'll add them?"
"I got how many numbers to add? I'll present a list of that length."
"Did I get nothing? I'll start at the beginning."