SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Rusted & Weathered HumanClay's Avatar
    Join Date
    Sep 2000
    Location
    Milwaukee, WI
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Loop and Form Values?!?

    Given the following code snippet, and assuming $num = 5, what would I set the value equal to if someone would submit the form (the form will return you to the form page, but have the values entered inside the input box.)

    PHP Code:
    for ($i 1$i <= $num$i++0
    {
       echo 
    "<input type=\"text\" name=\"a_$i\" value=\"???\" />";

    Ok, so the first text box would have a name of a_1. In PHP you would access that variable by $a_1 correct? So I'd have to set the value of the input box to $a_1, but how do I do that when the number is added into the input name dynamically as the loop goes through?
    Steve Caponetto - [profile] [e-mail]
    CreedFeed.com - feed your need for Creed!

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use variable variables like this

    PHP Code:
    for ($i 1$i <= $num$i++) {
        
    $tmp "a_".$i;
        print 
    '<input type="text" name="a_'.$i.'" value="'.$$tmp.'">'."\n";
        } 

    You see you can assign the name of the variable you want use to a temporary variable. In this example, I assign $tmp the value of a_ and the current value of $i. Now I can use $$tmp and it will print the value of $a_$i
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2000
    Location
    Allentown PA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It'd probably be easier by naming your inputs on your form as an array:

    <input type="text" name="a[0]" value="???" />
    <input type="text" name="a[1]" value="???" />
    <input type="text" name="a[2]" value="???" />
    etc...

    PHP will create an array $a that you can traverse through as you would any other array.

  4. #4
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or just a[]


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
  •