[php] arrays and loops

Kevork Krozian kevork at edulists.com.au
Sun Oct 31 01:08:22 EST 2010


Hi Maggie,

 

 What you need is the ability to create a dynamic form - it can grow or
shrink on the run before submitting the form.

This requires javascript or similar.

As the form grows, the variable ( array )  capturing each line of input
grows. 

When this is sent in to the php page to process ,a loop steps through and
picks up each element and prints it until there are no more elements.

 

Attached 2 files ( dynamic.php and process.php ) demonstrate this. The
javascript is a modification of one I found during a search.

Try it. Note , no sentinel required.

 

Enjoy

 

Kevork Krozian

Edulists Creator Administrator

www.edulists.com.au

tel: 0419 356 034

 

From: php-bounces at edulists.com.au [mailto:php-bounces at edulists.com.au] On
Behalf Of Margaret King Iaquinto
Sent: Saturday, 30 October 2010 4:41 PM
To: 'PHP programming Teachers' Mailing List'
Subject: Re: [php] arrays and loops

 

My code didn't show up -- for the HTML, that is. Apologies

So here it is , I hope.

"<p><input type = "text" name = "languages[]"  >Add a language</p>"
"<p><input type = "text" name = "languages[]"  > Add a language</p>"
"<p><input type = "text" name = "languages[]"  > Add a language</p>"
"<p><input type = "text" name = "languages[]"  > Add a language</p>"

I used the name of the input box as languages[ ] -- to indicate an array.

When sent to the server, the php used this line of code to store the
contents of the array:

$languages = $_POST['languages'];

In the case above, I have shown 4 data items to be stored in the area. This
is a fixed number. Sure, the user can enter fewer than the 4 displayed. No
problems.

What I want to learn is: how to allow the user to choose his own number of
data items to enter. He may have 5 or 10, whatever. But that would mean my
HTML form is unusually (and foolishly) long. Even then, I may not be able to
accommodate the huge number of data items for this array.

So, my user hits the form and it can instruct the user this way : "Enter the
names of the programming languages you know. Type in a -1 when finished." Or
something to that effect.

The user will then enter 5 or 10 languages. The -1 is a signal that the data
entry is finished. It doesn't need to be a -1 but something that triggers
the end of the data entry. Like "Press hash to end the message."

Thank you for the prompt reply!

Maggie
VK3CFI 

On Sat Oct 30 13:03 , Kevork Krozian sent:

Hi Maggie,


I need more information.
I am a little thrown by the fact you have PHP code within the html part of
the form - languages[].
You could call them lang1, lang2, lang3, lang4 and when received in the page
at the server to process, you can go to the end of your array and add each
of the lang1, lang2, lang3, lang4 to it as follows:

$languages[] = $POST['lang1']; // add one more item to array $languages
$languages[] = $POST['lang2']; // add one more item to array $languages
$languages[] = $POST['lang3']; // add one more item to array $languages
$languages[] = $POST['lang3']; // add one more item to array $languages

However, if you go back to the form and submit again, the $languages[] array
is no longer "alive" unless saved to a file to read from. Do I make sense ?
You can keep it alive by keeping it as a session variable but it has to be
passed back and forth to be alive in that session. You need to save it
before the session ends otherwise it is gone. What is a session ? It is the
time a browser window is open forwards, backwards etc until closed where the
session ends. 

If I can get a grasp of what you wish to do it would be easier to see the
best way to go.
What I observe here is you wish to collect individual elements of an array
via fields in a form restricted to 4 fields. This is kind of ok I guess but
what is the design aim , to collect input to fill an array 1? 2 ? 3 ? 4? how
many at a time ?
Remember with PHP you have a client filling a form potentially in Lebanon
and a server receiving the form contents to process it in Armenia. Therefore
the application environment of VB is not a reasonable analogy because with
VB you are using the form and processing at the same time in the one loop.
Does that make sense ?
It makes sense to collect data one "row" , "record" at a time and add to an
array. You keep doing this for as long as you want, but need to save to a
file to keep the data. Having a sentinel doesn't make sense as the cessation
of form filling is the end of your input process.
I don't wish to sound judgmental but the client /server concept is the point
I am trying to explain. 
Fill form -> send to server -> process data. If you fill form again repeat
the previous loop having saved the data before leaving the processing . No
need for a sentinel. 

I hope I have made some sense here
If not please tell me.

Take Care
Kevork

-----Original Message-----
From: php-bounces at edulists.com.au
<javascript:top.opencompose('php-bounces at edulists.com.au','','','')>
[php-bounces at edulists.com.au <javascript:top.opencompose('%3ca%20href=>
','','','')">php-bounces at edulists.com.au
<javascript:top.opencompose('php-bounces at edulists.com.au','','','')> ] On
Behalf Of Margaret Iaquinto
Sent: Saturday, 30 October 2010 3:41 PM
To: php at edulists.com.au
<javascript:top.opencompose('php at edulists.com.au','','','')> 
Subject: [php] arrays and loops

I can gather data into an array by using a form. I can spill the 
contents of that array by using a loop.

In the code snippet below, the user enters the name of a programming 
language.

snippet from the form:

p>[          ]Add a language

 

[          ] Add a language

 

[          ] Add a language

 

[          ] Add a language



The php code snippet looks like this:

echo "Display the data - using a REPEAT UNTIL loop";
echo "
";
$position = 0;
do {
echo "$languages[$position]";
$position += 1;
echo "
";
}
while ($position < count($languages));

And the code works.

The form limits the number of languages to four, in the code above.

Is there a way to use a loop or some kind of syntax to ask the user to 
keep entering data until a sentinel is entered?
In VB, I can use an InputBox within a loop. When the user types in, say 
-1, this ends the loop and the array stops being filled.

But how do I do that in PHP?

Thanks

Maggie
_______________________________________________
http://www.edulists.com.au - FAQ, resources, subscribe, unsubscribe
PHP Mailing List kindly supported by
http://www.vcaa.vic.edu.au - Victorian Curriculum and Assessment Authority
and
http://www.vitta.org.au - VITTA Victorian Information Technology Teachers
Association Inc

_______________________________________________
http://www.edulists.com.au - FAQ, resources, subscribe, unsubscribe
PHP Mailing List kindly supported by
http://www.vcaa.vic.edu.au - Victorian Curriculum and Assessment Authority
and
http://www.vitta.org.au - VITTA Victorian Information Technology Teachers
Association Inc
)

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/php/attachments/20101031/50278e83/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dynamic.php
Type: application/octet-stream
Size: 1365 bytes
Desc: not available
Url : http://www.edulists.com.au/pipermail/php/attachments/20101031/50278e83/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: process.php
Type: application/octet-stream
Size: 249 bytes
Desc: not available
Url : http://www.edulists.com.au/pipermail/php/attachments/20101031/50278e83/attachment-0001.obj 


More information about the php mailing list