Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

PhilVaz

PHP question on arrays

This topic is 5598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am doing an apartment site for a guy using PHP, with a flat text file (I haven't learned MySQL yet). I want to allow up to 40 apts, up to 12 rooms each, each room has a price, a status (vacant or occupied), a description, a pet status, etc. A couple of fields like that, nothing huge. My problem is trying to figure out how to use a PHP array passed through an HTML form. In other words, I want to pass an array with type = hidden. For example, instead of print" < input type='hidden' name='A' value='$A' >"; print" < input type='hidden' name='B' value='$B' >"; print" < input type='hidden' name='C' value='$C' >"; print" < input type='hidden' name='D' value='$D' >"; I want to do this using a PHP array -- e.g. $T = array(3000) print" < input type='hidden' name='T[1]' value='$T[1]' >"; print" < input type='hidden' name='T[2]' value='$T[2]' >"; print" < input type='hidden' name='T[3]' value='$T[3]' >"; print" < input type='hidden' name='T[4]' value='$T[4]' >"; Since I have 40 apts x 12 rooms each x 6 vars associated with each room = approx 3000 variables, if I can use an array I can specify a for loop and pass all the variables in one line in the loop -- e.g. $T[$i]. But I'm pretty sure the above T[1] T[2] ... $T[$i] etc won't work since arrays can't be specified in the HTML input name = field. So how can I pass this information as an array without naming each variable separately. Hope that's clear. I'm not that experienced with PHP and maybe there is an easier way to do this. Phil P [edited by - PhilVaz on August 16, 2003 4:23:47 AM]

Share this post


Link to post
Share on other sites
Advertisement
You can create arrays of html inputs when using php.

Just name all the elements of an array as ''element[]'' where ''element'' is a name you specify.

Then, when parsing the request with php, you can access them as normal array.

Advice:
-Do not use an index number.
If you do need a fixed index to an element, use a pairing hidden input for each element.

It is a misconception that element''s name cannot contain [].
It''s just a matter of whether the server software recognizes the syntax or not (which php does )

Share this post


Link to post
Share on other sites
<< Just name all the elements of an array as ''element[]'' where ''element'' is a name you specify. Then, when parsing the request with php, you can access them as normal array. >>

Thanks, I''ve asked on PHPBuilder.com and someone suggested "implode" to put the whole array into one giant string, pass it, then use "explode" to put the array back in normal array order. Cool, I''m gonna try that...

Phil P

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!