SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    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!

  2. #2
    SitePoint Zealot ngi112's Avatar
    Join Date
    May 2005
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 14:48.
    ProxyTor - Submit your Proxy Site for free, fast.
    Parxy.com - Find a New Hobby!

  3. #3
    SitePoint Wizard
    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>"

  4. #4
    SitePoint Wizard
    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.

  5. #5
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    USA
    Posts
    1,407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7stud - that did work - thanks again!

  6. #6
    SitePoint Zealot Mau's Avatar
    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[]" />
    Then $_REQUEST['field'] = array()

  7. #7
    Non-Member Gator99's Avatar
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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).

  8. #8
    SitePoint Wizard
    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?

  9. #9
    SitePoint Zealot Mau's Avatar
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •