[php] arrays and loops

Michael Greenhill mi at whsc.vic.edu.au
Sat Oct 30 20:10:50 EST 2010


I always prefer to use $_REQUEST[value] instead of $_POST[]. Also, don't
forget to wrap your $_REQUEST or $_POST in htmlspecialchars - eg:

htmlspecialchars($_POST['languages']);



Regards,
Michael Greenhill

Please think of the environment before printing off this email or any
attachments.


On Sat, Oct 30, 2010 at 4:41 PM, Margaret King Iaquinto <
iaquinto at ozemail.com.au> wrote:

> 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 [php-bounces at edulists.com.au
> ','','','')">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>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<http://parse.pl?redirect=http%3A%2F%2Fwww.edulists.com.au>- FAQ, resources, subscribe, unsubscribe
> PHP Mailing List kindly supported by
> http://www.vcaa.vic.edu.au<http://parse.pl?redirect=http%3A%2F%2Fwww.vcaa.vic.edu.au>- Victorian Curriculum and Assessment Authority and
> http://www.vitta.org.au<http://parse.pl?redirect=http%3A%2F%2Fwww.vitta.org.au>- VITTA Victorian Information Technology Teachers Association Inc
>
> _______________________________________________
> http://www.edulists.com.au<http://parse.pl?redirect=http%3A%2F%2Fwww.edulists.com.au>- FAQ, resources, subscribe, unsubscribe
> PHP Mailing List kindly supported by
> http://www.vcaa.vic.edu.au<http://parse.pl?redirect=http%3A%2F%2Fwww.vcaa.vic.edu.au>- Victorian Curriculum and Assessment Authority and
> http://www.vitta.org.au<http://parse.pl?redirect=http%3A%2F%2Fwww.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/20101030/7e7e340e/attachment-0001.html 


More information about the php mailing list