SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: variable loop

  1. #1
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    variable loop

    Lets say I have 4 variables
    $var1
    $var2
    $var3
    $var4

    and I want to do the exact same thing to all of them (put them in a database) but I need to do it in a loop. In hte loop I need to call $varx where x is 1,2,3,4. the x goes up each itteration. so the first time through $varx would be $var1 , and the second time var2
    Its kind of hard to explain the effect Im going for.. I hope you can figure it out from that

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The best thing to do is to store those variables in an array to start with. Instead of having $var1, $var2, etc., you should have the values stored in $var[1], $var[2], ..etc. Arrays are very useful because you can use loops to display them or change them.
    Last edited by 7stud; Jun 2, 2001 at 23:09.

  3. #3
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how could i get an array from a form

    the variables are comming from those checkboxes things. They are called $var1 $var2 and so on

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Simple! Name them $var[0], $var[1] and so on.

  5. #5
    SitePoint Zealot oodie's Avatar
    Join Date
    Jul 2000
    Location
    Misty Mountain
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Easy ...
    Code:
    <input type=checkbox name='var[]' value=val1>One
    <input type=checkbox name='var[]' value=val2>Two
    <input type=checkbox name='var[]' value=val3>Three
    If, for example, the first and the third checkboxes are selected, you'll get an array with $var[0] = 'val1', $var[1]='val3'


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
  •