SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
Apr 25, 2006, 10:09 #1
- Join Date
- Dec 2004
- Location
- USA
- Posts
- 1,407
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
how to get all fields starting with "field_" in an array
I build a form using Pear's Quickform and using a dynamic list of fields from a database as part of the form.
The builds out as something like:
<input size="10" maxlength="10" name="fieldNum_1" type="text" />
<input size="10" maxlength="10" name="fieldNum_2" type="text" />
where 1, 2,... are ID numbers for the database and I need those.
How can I get only those fields in an array?
Thanks in advance!
-
Apr 25, 2006, 10:32 #2
why not explode?
Assuming all of the Fieldnums are in an array just do:
foreach ($fieldnum as $item) {
list($first, $var) = explode('_', $item);
//$temp contains the ID.
}
I'm not sure if that's what you asked for.Last edited by ngi112; Apr 25, 2006 at 15:48.
-
Apr 25, 2006, 10:39 #3
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can try something like this:
PHP Code:$arr = array();
foreach($_POST as $key=>$value)
{
if(stristr($key, 'fieldNum') != FALSE)
{
$arr[$key] = $value;
}
}
echo "<pre>";
print_r($arr);
echo "</pre>";
-
Apr 25, 2006, 11:06 #4
- Join Date
- Dec 2004
- Location
- USA
- Posts
- 1,407
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
7stud - I'll try that that - thanks!
ngi - thanks but they are not in an array - my post title asked about getting them into an array.
-
Apr 25, 2006, 12:26 #5
- Join Date
- Dec 2004
- Location
- USA
- Posts
- 1,407
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
7stud - that did work - thanks again!
-
Apr 25, 2006, 17:54 #6
- Join Date
- Jan 2006
- Location
- California, USA
- Posts
- 134
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
HTML supports this completely:
Code:<input name="field[0]" /> <input name="field[1]" /> <!-- or short hand to give same effect --> <input name="field[]" /> <input name="field[]" />
-
Apr 25, 2006, 18:04 #7
The problem with the [] in the form element name to specify an array in php is that it does not work and play well with javascript. But now that all browsers are mostly dom compliant you can work around this by accessing elements by id rather than name (with javascript).
-
Apr 25, 2006, 19:05 #8
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Can you post an example of how name values with brackets can cause problems with javascript?
-
Apr 25, 2006, 19:23 #9
- Join Date
- Jan 2006
- Location
- California, USA
- Posts
- 134
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm no JavaScript expert, I think that just a syntax error would be thrown. For example, on NS 4.x, this would probably not work: document.form1.field[0].value. The way to simply get around it is to use IDs as Gator99 suggested.
Bookmarks