<HTML>
My code didn't show up -- for the HTML, that is. Apologies<BR>
<BR>
So here it is , I hope.<BR>
<BR>
"<p><input type = "text" name = "languages[]" >Add a language</p>"<BR>
"<p><input type = "text" name = "languages[]" > Add a language</p>"<BR>
"<p><input type = "text" name = "languages[]" > Add a language</p>"<BR>
"<p><input type = "text" name = "languages[]" > Add a language</p>"<BR>
<BR>
I used the name of the input box as languages[ ] -- to indicate an array.<BR>
<BR>
When sent to the server, the php used this line of code to store the contents of the array:<BR>
<BR>
$languages = $_POST['languages'];<BR>
<BR>
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.<BR>
<BR>
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.<BR>
<BR>
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.<BR>
<BR>
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."<BR>
<BR>
Thank you for the prompt reply!<BR>
<BR>
Maggie<BR>
VK3CFI <BR>
<BR>
<span style="font-weight: bold;">On Sat Oct 30 13:03 , Kevork Krozian <k.krozian@fhc.vic.edu.au> sent:<BR>
<BR>
</k.krozian@fhc.vic.edu.au></span><blockquote style="border-left: 2px solid rgb(245, 245, 245); margin-left: 5px; margin-right: 0px; padding-left: 5px; padding-right: 0px;">Hi Maggie,<BR>
<BR>
<BR>
I need more information.<BR>
I am a little thrown by the fact you have PHP code within the html part of the form - languages[].<BR>
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:<BR>
<BR>
$languages[] = $POST['lang1']; // add one more item to array $languages<BR>
$languages[] = $POST['lang2']; // add one more item to array $languages<BR>
$languages[] = $POST['lang3']; // add one more item to array $languages<BR>
$languages[] = $POST['lang3']; // add one more item to array $languages<BR>
<BR>
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. <BR>
<BR>
If I can get a grasp of what you wish to do it would be easier to see the best way to go.<BR>
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 ?<BR>
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 ?<BR>
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.<BR>
I don't wish to sound judgmental but the client /server concept is the point I am trying to explain. <BR>
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. <BR>
<BR>
I hope I have made some sense here<BR>
If not please tell me.<BR>
<BR>
Take Care<BR>
Kevork<BR>
<BR>
-----Original Message-----<BR>
From: <a href="javascript:top.opencompose('php-bounces@edulists.com.au','','','')">php-bounces@edulists.com.au</a> [<a href="javascript:top.opencompose('<a href=" javascript:top.opencompose(="" php-bounces@edulists.com.au="" ,="" )="">php-bounces@edulists.com.au</a>','','','')"><a href="javascript:top.opencompose('php-bounces@edulists.com.au','','','')">php-bounces@edulists.com.au</a>] On Behalf Of Margaret Iaquinto<BR>
Sent: Saturday, 30 October 2010 3:41 PM<BR>
To: <a href="javascript:top.opencompose('php@edulists.com.au','','','')">php@edulists.com.au</a><BR>
Subject: [php] arrays and loops<BR>
<BR>
I can gather data into an array by using a form. I can spill the <BR>
contents of that array by using a loop.<BR>
<BR>
In the code snippet below, the user enters the name of a programming <BR>
language.<BR>
<BR>
snippet from the form:<BR>
<BR>
p><input name="languages[]" type="text">Add a language<p></p><BR>
<p><input name="languages[]" type="text"> Add a language</p><BR>
<p><input name="languages[]" type="text"> Add a language</p><BR>
<p><input name="languages[]" type="text"> Add a language</p><BR>
<BR>
The php code snippet looks like this:<BR>
<BR>
echo "Display the data - using a REPEAT UNTIL loop";<BR>
echo "<BR>
";<BR>
$position = 0;<BR>
do {<BR>
echo "$languages[$position]";<BR>
$position += 1;<BR>
echo "<BR>
";<BR>
}<BR>
while ($position < count($languages));<BR>
<BR>
And the code works.<BR>
<BR>
The form limits the number of languages to four, in the code above.<BR>
<BR>
Is there a way to use a loop or some kind of syntax to ask the user to <BR>
keep entering data until a sentinel is entered?<BR>
In VB, I can use an InputBox within a loop. When the user types in, say <BR>
-1, this ends the loop and the array stops being filled.<BR>
<BR>
But how do I do that in PHP?<BR>
<BR>
Thanks<BR>
<BR>
Maggie<BR>
_______________________________________________<BR>
<a href="parse.pl?redirect=http%3A%2F%2Fwww.edulists.com.au" target="_blank"><span style="color: red;">http://www.edulists.com.au</span></a> - FAQ, resources, subscribe, unsubscribe<BR>
PHP Mailing List kindly supported by<BR>
<a href="parse.pl?redirect=http%3A%2F%2Fwww.vcaa.vic.edu.au" target="_blank"><span style="color: red;">http://www.vcaa.vic.edu.au</span></a> - Victorian Curriculum and Assessment Authority and<BR>
<a href="parse.pl?redirect=http%3A%2F%2Fwww.vitta.org.au" target="_blank"><span style="color: red;">http://www.vitta.org.au</span></a> - VITTA Victorian Information Technology Teachers Association Inc<BR>
<BR>
_______________________________________________<BR>
<a href="parse.pl?redirect=http%3A%2F%2Fwww.edulists.com.au" target="_blank"><span style="color: red;">http://www.edulists.com.au</span></a> - FAQ, resources, subscribe, unsubscribe<BR>
PHP Mailing List kindly supported by<BR>
<a href="parse.pl?redirect=http%3A%2F%2Fwww.vcaa.vic.edu.au" target="_blank"><span style="color: red;">http://www.vcaa.vic.edu.au</span></a> - Victorian Curriculum and Assessment Authority and<BR>
<a href="parse.pl?redirect=http%3A%2F%2Fwww.vitta.org.au" target="_blank"><span style="color: red;">http://www.vitta.org.au</span></a> - VITTA Victorian Information Technology Teachers Association Inc<BR>
)<BR>
</blockquote></HTML>
<BR>