Skip to main content
gamedev.net
🔒 Locked

PHP question on arrays

Started by PhilVaz Aug 16, 2003 at 3:22 AM 1 replies 450+ views
Original Post
PhilVaz
PhilVaz
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]
Nik02
Nik02
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 )

Niko Suni
PhilVaz
PhilVaz
<< 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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.