concatenate object variable name

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

    concatenate object variable name

    I have form values stored in an object.

    I have a set of numbered form fields:

    specialty1
    specialty2
    specialty3

    I need to loop through these, check for content and process accordingly. I
    can't figure out how to correctly concatenate the number to the end of the
    field name. For example:

    for ($i = 1; $i <= 3; $i++) {
    if (eval("$node->specialty" . $i)) {
    //do something
    }
    }
    I know eval is slow, but what other options are there?

    Thanks.


  • Willem

    #2
    Re: concatenate object variable name



    Bosconian wrote:[color=blue]
    > I have form values stored in an object.[/color]

    I suppose you stored them in separate variables. It may be handier to
    use an array for that.

    When calling a method, you can pass the array index as a parameter.
    [color=blue]
    >
    > I have a set of numbered form fields:
    >
    > specialty1
    > specialty2
    > specialty3
    >
    > I need to loop through these, check for content and process accordingly. I
    > can't figure out how to correctly concatenate the number to the end of the
    > field name. For example:
    >
    > for ($i = 1; $i <= 3; $i++) {
    > if (eval("$node->specialty" . $i)) {
    > //do something
    > }
    > }
    > I know eval is slow, but what other options are there?
    >
    > Thanks.
    >
    >[/color]

    Comment

    • Jerry Stuckle

      #3
      Re: concatenate object variable name

      Bosconian wrote:[color=blue]
      > I have form values stored in an object.
      >
      > I have a set of numbered form fields:
      >
      > specialty1
      > specialty2
      > specialty3
      >
      > I need to loop through these, check for content and process accordingly. I
      > can't figure out how to correctly concatenate the number to the end of the
      > field name. For example:
      >
      > for ($i = 1; $i <= 3; $i++) {
      > if (eval("$node->specialty" . $i)) {
      > //do something
      > }
      > }
      > I know eval is slow, but what other options are there?
      >
      > Thanks.
      >
      >[/color]

      As Willem said - use an array. For instance:
      <form action="..." method="post">
      <b>Specialty 1</b><input type="text" name="specialty[]"><br>
      <b>Specialty 2</b><input type="text" name="specialty[]"><br>
      <b>Specialty 3</b><input type="text" name="specialty[]"><br>
      (other stuff)
      </form>

      In your target's code,

      $specialty=$_PO ST['specialty'];

      The three (if they are filled in) will be in $specialty[0],
      $specialty[1] and $specialty[2].

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

      Comment

      • Bosconian

        #4
        Re: concatenate object variable name

        "Bosconian" <bosconian@plan etx.com> wrote in message
        news:P7mdnYTMUJ p7Jt3eRVn-tA@comcast.com. ..[color=blue]
        > I have form values stored in an object.
        >
        > I have a set of numbered form fields:
        >
        > specialty1
        > specialty2
        > specialty3
        >
        > I need to loop through these, check for content and process accordingly. I
        > can't figure out how to correctly concatenate the number to the end of the
        > field name. For example:
        >
        > for ($i = 1; $i <= 3; $i++) {
        > if (eval("$node->specialty" . $i)) {
        > //do something
        > }
        > }
        > I know eval is slow, but what other options are there?
        >
        > Thanks.
        >
        >[/color]

        When concatenating objects in the above example simply

        $node->specialty . $i

        eval is not needed.


        Comment

        Working...