[php] arrays and loops

Kevork Krozian K.Krozian at fhc.vic.edu.au
Sat Oct 30 16:03:57 EST 2010


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 [mailto: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
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><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>

The php code snippet looks like this:

echo "Display the data  -  using a REPEAT UNTIL loop";
echo "<br>";
$position = 0;
do {
     echo "$languages[$position]";
     $position += 1;
     echo "<br>";
}
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



More information about the php mailing list