You are here: Re: Variable in a variable name « PHP Programming Language « IT news, forums, messages
Re: Variable in a variable name

Posted by Jerry Stuckle on 04/13/06 23:32

Jerry Stuckle wrote:
> Garry Jones wrote:
>
>> "Jerry Stuckle" <jstucklex@attglobal.net> skrev i meddelandet
>> news:1cydncizcunm8aPZnZ2dnUVZ_uudnZ2d@comcast.com...
>>
>>
>>> Sjoerd is correct - you really want to use arrays. They're much
>>> easier than your route, and the code much cleaner.
>>
>>
>>
>> I have looked at arrays. My problem is that some people only enter
>> their christain name and leave the surname blank. Other strange user
>> combinations are possible, people can jump to a lower place in the
>> form and use the forth group of input fields for the third person for
>> instance.
>>
>> As I am reading in up to 8 values for each person I saw that I was
>> going to have to synchronise the input if I use arrays. Data entered
>> into each of the groups of fields has to be kept together. As I am new
>> to arrays and have already got the code sorted out I thought a
>> variable name would be the simpliest way of doing this.
>>
>> However, sticking ones head in the mud and refusing to adapt to the
>> correct tool for the job never pays in the long term. So a few tips
>> about keeping input data in groups so that an array reading does not
>> group together the information from different groups of fields would
>> come in very handy for me.
>>
>> Garry Jones
>> Sweden
>>
>
> Garry,
>
> OK, in that case name you fields with the array index, i.e.
>
> <type=input, name="personage[1]" ...
> <type=input, name="personage[2]" ...
>
> This works just as well.
>
>

A minor correction to the above, and more clarification. It will work, but if
you don't specify an index in your html, PHP starts arrays at index zero. So to
be consistent, I should have said:

<type=input name="personage[0]" ...
<type=input name="address[0]" ...
<type=input name="personage[1]" ...
<type=input name="address[1]" ...

for consistency.

Then you can access the data with

for ($i=0; i <= 10; i++) {
$name = $_POST["personage[$i]";
$addr = $_POST["address[$i]"];

etc.

Of course, you will need to see if the variable is set - if they don't enter
something in the field you won't get it in the $_POST variable.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация