homepage Welcome to WebmasterWorld Guest from 54.226.147.84
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Complete beginner's question (have mercy)
Can PHP do this?
Algebrator

10+ Year Member



 
Msg#: 4487 posted 1:15 am on Jul 22, 2004 (gmt 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 ...

 

ruserious

10+ Year Member



 
Msg#: 4487 posted 1:22 am on Jul 22, 2004 (gmt 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.

coopster

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



 
Msg#: 4487 posted 3:14 pm on Jul 22, 2004 (gmt 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.

ergophobe

WebmasterWorld Administrator ergophobe us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4487 posted 3:42 pm on Jul 22, 2004 (gmt 0)

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'.... :-)

coopster

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



 
Msg#: 4487 posted 4:00 pm on Jul 22, 2004 (gmt 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.

:-)

satanclaus



 
Msg#: 4487 posted 4:03 pm on Jul 22, 2004 (gmt 0)

Go pick up the O'Reilly PHP book. Its useful and should get you moving easily.

timster

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4487 posted 5:57 pm on Jul 22, 2004 (gmt 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."

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved