Variable in a variable name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Garry Jones

    #1

    Variable in a variable name

    I have an entry page which can be used to key in data for ten people. My
    assigned variables for each field are in line with

    $personage1
    $personage2
    ....etc
    $personage9
    $personage10

    and so on. (I have 8 fields for every person, 80 values in all).

    To simplify my code and make changes easier I would like to create and use
    these variables with a variable "x" in the name

    This by recycling the code with $personX in and changing the value of x and
    running the code ten times. Probably in some kind of do while loop (fairly
    new to php, but I have been using Excel VB for years and am familiar with
    the syntax and effectiveness of Do While Loops).

    1) how do I put a variable in a variable name?
    2) syntax for a do while loop with this variable name.

    The beauty is if I can get this working I can make x=1 the first time adding
    one each time and using value of X in my while statement.


    Garry Jones
    Sweden


  • Sjoerd

    #2
    Re: Variable in a variable name

    Garry Jones wrote:[color=blue]
    > This by recycling the code with $personX in and changing the value of x and
    > running the code ten times. Probably in some kind of do while loop (fairly
    > new to php, but I have been using Excel VB for years and am familiar with
    > the syntax and effectiveness of Do While Loops).[/color]

    You probably want to use arrays, which are variables which can contain
    multiple values.
    $personage[1] = 'John';
    $personage[2] = 'Gerry';

    You can use $personage[1] as you would use any other variable. You can
    loop through this array with:
    foreach ($personage as $person) {
    echo $person;
    }
    This would print "JohnGerry" .

    See

    for more information about arrays.

    It is possible in PHP to have variable variables (e.g. specify the name
    of the variable in another variable), but it is almost never what you
    want.

    Comment

    • Garry Jones

      #3
      Re: Variable in a variable name

      "Sjoerd" <sjoerder@gmail .com> skrev i meddelandet
      news:1144926545 .087071.20180@e 56g2000cwe.goog legroups.com...
      [color=blue]
      > It is possible in PHP to have variable variables (e.g. specify the name
      > of the variable in another variable), but it is almost never what you
      > want.[/color]

      This seems to be the easiest way of doing it for me. To ease processing of
      the form I want to use the same code. On the form there is a input named
      "realname1" . The following code sets the data entered into realname1 as
      var1.

      $x = 1;
      $y = "var".$x;
      $$y = $_POST['realname1'];

      So far so good, that works.

      But to be able to recycle this code I need to change the number 1 in
      realname. How do I use the same "$x" in the followingbracke ts.

      $x = 1;
      $y = "var".$x;
      $$y = $_POST['realname' ($x)];

      I then want to create a do while lopp. Something like..

      DO WHILE $x <11
      $y = "var".$x;
      $$y = $_POST['realname' ($x)];

      $x = $x + 1;
      :Loop

      I am greatful if you someone can explain the syntax for this.

      Garry Jones
      Sweden



      Comment

      • Jerry Stuckle

        #4
        Re: Variable in a variable name

        Garry Jones wrote:[color=blue]
        > "Sjoerd" <sjoerder@gmail .com> skrev i meddelandet
        > news:1144926545 .087071.20180@e 56g2000cwe.goog legroups.com...
        >
        >[color=green]
        >>It is possible in PHP to have variable variables (e.g. specify the name
        >>of the variable in another variable), but it is almost never what you
        >>want.[/color]
        >
        >
        > This seems to be the easiest way of doing it for me. To ease processing of
        > the form I want to use the same code. On the form there is a input named
        > "realname1" . The following code sets the data entered into realname1 as
        > var1.
        >
        > $x = 1;
        > $y = "var".$x;
        > $$y = $_POST['realname1'];
        >
        > So far so good, that works.
        >
        > But to be able to recycle this code I need to change the number 1 in
        > realname. How do I use the same "$x" in the followingbracke ts.
        >
        > $x = 1;
        > $y = "var".$x;
        > $$y = $_POST['realname' ($x)];
        >
        > I then want to create a do while lopp. Something like..
        >
        > DO WHILE $x <11
        > $y = "var".$x;
        > $$y = $_POST['realname' ($x)];
        >
        > $x = $x + 1;
        > :Loop
        >
        > I am greatful if you someone can explain the syntax for this.
        >
        > Garry Jones
        > Sweden
        >
        >
        >[/color]

        Garry,

        Sjoerd is correct - you really want to use arrays. They're much easier than
        your route, and the code much cleaner.

        And you might as well get your feet wet with arrays now - they are very heavily
        used in PHP. This is a great way of doing it.

        Simply name your fields in the html something like:

        <input name="personage[]" ...>

        Then in your code (no error checking included):

        $personage = $_POST['personage'];
        // $personage now contains the entire list of names

        foreach ($personage as $person) {
        // $person contains the first name in the first loop iteration,
        // the second name in the second iteration, etc.

        doSomethingToPe rson($person)
        }


        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Garry Jones

          #5
          Re: Variable in a variable name

          "Jerry Stuckle" <jstucklex@attg lobal.net> skrev i meddelandet
          news:1cydncizcu nm8aPZnZ2dnUVZ_ uudnZ2d@comcast .com...
          [color=blue]
          > Sjoerd is correct - you really want to use arrays. They're much easier
          > than your route, and the code much cleaner.[/color]

          I have looked at arrays. My problem is that some people only enter their
          christain name and leave the surname blank. Other strange user combinations
          are possible, people can jump to a lower place in the form and use the forth
          group of input fields for the third person for instance.

          As I am reading in up to 8 values for each person I saw that I was going to
          have to synchronise the input if I use arrays. Data entered into each of the
          groups of fields has to be kept together. As I am new to arrays and have
          already got the code sorted out I thought a variable name would be the
          simpliest way of doing this.

          However, sticking ones head in the mud and refusing to adapt to the correct
          tool for the job never pays in the long term. So a few tips about keeping
          input data in groups so that an array reading does not group together the
          information from different groups of fields would come in very handy for me.

          Garry Jones
          Sweden


          Comment

          • Jerry Stuckle

            #6
            Re: Variable in a variable name

            Garry Jones wrote:[color=blue]
            > "Jerry Stuckle" <jstucklex@attg lobal.net> skrev i meddelandet
            > news:1cydncizcu nm8aPZnZ2dnUVZ_ uudnZ2d@comcast .com...
            >
            >[color=green]
            >>Sjoerd is correct - you really want to use arrays. They're much easier
            >>than your route, and the code much cleaner.[/color]
            >
            >
            > I have looked at arrays. My problem is that some people only enter their
            > christain name and leave the surname blank. Other strange user combinations
            > are possible, people can jump to a lower place in the form and use the forth
            > group of input fields for the third person for instance.
            >
            > As I am reading in up to 8 values for each person I saw that I was going to
            > have to synchronise the input if I use arrays. Data entered into each of the
            > groups of fields has to be kept together. As I am new to arrays and have
            > already got the code sorted out I thought a variable name would be the
            > simpliest way of doing this.
            >
            > However, sticking ones head in the mud and refusing to adapt to the correct
            > tool for the job never pays in the long term. So a few tips about keeping
            > input data in groups so that an array reading does not group together the
            > information from different groups of fields would come in very handy for me.
            >
            > Garry Jones
            > Sweden
            >
            >[/color]

            Garry,

            OK, in that case name you fields with the array index, i.e.

            <type=input, name="personage[1]" ...
            <type=input, name="personage[2]" ...

            This works just as well.


            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Jerry Stuckle

              #7
              Re: Variable in a variable name

              Jerry Stuckle wrote:[color=blue]
              > Garry Jones wrote:
              >[color=green]
              >> "Jerry Stuckle" <jstucklex@attg lobal.net> skrev i meddelandet
              >> news:1cydncizcu nm8aPZnZ2dnUVZ_ uudnZ2d@comcast .com...
              >>
              >>[color=darkred]
              >>> Sjoerd is correct - you really want to use arrays. They're much
              >>> easier than your route, and the code much cleaner.[/color]
              >>
              >>
              >>
              >> I have looked at arrays. My problem is that some people only enter
              >> their christain name and leave the surname blank. Other strange user
              >> combinations are possible, people can jump to a lower place in the
              >> form and use the forth group of input fields for the third person for
              >> instance.
              >>
              >> As I am reading in up to 8 values for each person I saw that I was
              >> going to have to synchronise the input if I use arrays. Data entered
              >> into each of the groups of fields has to be kept together. As I am new
              >> to arrays and have already got the code sorted out I thought a
              >> variable name would be the simpliest way of doing this.
              >>
              >> However, sticking ones head in the mud and refusing to adapt to the
              >> correct tool for the job never pays in the long term. So a few tips
              >> about keeping input data in groups so that an array reading does not
              >> group together the information from different groups of fields would
              >> come in very handy for me.
              >>
              >> Garry Jones
              >> Sweden
              >>[/color]
              >
              > Garry,
              >
              > OK, in that case name you fields with the array index, i.e.
              >
              > <type=input, name="personage[1]" ...
              > <type=input, name="personage[2]" ...
              >
              > This works just as well.
              >
              >[/color]

              A minor correction to the above, and more clarification. It will work, but if
              you don't specify an index in your html, PHP starts arrays at index zero. So to
              be consistent, I should have said:

              <type=input name="personage[0]" ...
              <type=input name="address[0]" ...
              <type=input name="personage[1]" ...
              <type=input name="address[1]" ...

              for consistency.

              Then you can access the data with

              for ($i=0; i <= 10; i++) {
              $name = $_POST["personage[$i]";
              $addr = $_POST["address[$i]"];

              etc.

              Of course, you will need to see if the variable is set - if they don't enter
              something in the field you won't get it in the $_POST variable.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Scott

                #8
                Re: Variable in a variable name

                On Thu, 2006-04-13 at 20:41 +0200, Garry Jones wrote:[color=blue]
                > "Jerry Stuckle" <jstucklex@attg lobal.net> skrev i meddelandet
                > news:1cydncizcu nm8aPZnZ2dnUVZ_ uudnZ2d@comcast .com...
                >[color=green]
                > > Sjoerd is correct - you really want to use arrays. They're much easier
                > > than your route, and the code much cleaner.[/color]
                >
                > I have looked at arrays. My problem is that some people only enter their
                > christain name and leave the surname blank. Other strange user combinations
                > are possible, people can jump to a lower place in the form and use the forth
                > group of input fields for the third person for instance.
                >
                > As I am reading in up to 8 values for each person I saw that I was going to
                > have to synchronise the input if I use arrays. Data entered into each of the
                > groups of fields has to be kept together. As I am new to arrays and have
                > already got the code sorted out I thought a variable name would be the
                > simpliest way of doing this.
                >
                > However, sticking ones head in the mud and refusing to adapt to the correct
                > tool for the job never pays in the long term. So a few tips about keeping
                > input data in groups so that an array reading does not group together the
                > information from different groups of fields would come in very handy for me.
                >
                > Garry Jones
                > Sweden
                >
                >[/color]

                Garry,

                If your input fields are all text fields, and you assign them variable
                names (<input type="text" name="personage[]">), then any empty fields
                will simply be an empty string. However, they will still hold a place in
                the array.

                For example, if the 2nd person enters their age, but the 1st person
                doesn't:

                $_POST['personage'][0] will equal ''
                $_POST['personage'][1] will equal '25' (or whatever age they put in).

                The point is, if you have your text fields named personage[] and
                personfname[], and have the same fields for each entry, then
                $_POST['personage'][2] will correspond with $_POST['personfname'][2].

                Does that help?

                Scott

                Comment

                Working...