variable concatenation problem

Discussion in 'PHP' started by vassili, Jun 9, 2010.

  1. #1
    hi, i have a quick question regarding this.

    $rsvp1 through $rsvp5 is being passed on from the previous page by POST. value is either "1" or "".

    $g1 through $g5 is being passed on from the previous page by POST. value is either a string or "".

    $guest1 through $guest5 is created on current page. they are a lines of html code.

    now, i want to check $rsvp1 through $rsvp5 in a while loop to see what their values are...and then echo the appropriate things. $n1 is the counter.

    i want $rsvp.$n to be $rsvp1 if $n is 1

    $guest.$n
    and $g.$n is the same idea.

    your help is much appreciated

    
    
    $n=1;
    
    while ( $n < 6 ) 
    {
    	
    	if ($rsvp.$n == "1")
    	{	
    	echo "<p>$guest.$n</p>";
    	}
    	else
    	{
    	echo "<p>$g.$n is not attending</p>";
    	}
    	
    $n++;
    }	
    
    
    PHP:
     
    vassili, Jun 9, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    its better if you use array.. its really confusing..
     
    bartolay13, Jun 10, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    and your description is much confusing :p
     
    danx10, Jun 10, 2010 IP
  4. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #4
    I think you really need to redesign what you are doing. Your structure is awkward and would be far better suited to an array.

    You can do it through something like this - but you are better of redesigning what you are doing.

    $n = 1;
    
    while ($n < 6)
      {
      $var = "rsvp".$n;
    
      if($$var == 1)
        {
        $guest = "guest".$n;
        print "<p>".$$guest."</p>";
        }
        else
        {
        $guest = "g".$n;
        print "<p>".$$guest." is not attending</p>";
        }
      $n++;
      }
    PHP:
     
    lukeg32, Jun 10, 2010 IP
  5. vassili

    vassili Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yea, i'll look into arrays, but this seems to be working as i want. thanks!
     
    vassili, Jun 13, 2010 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Another approach:
    
    echo '<p>', ${"guest{$n}"}, '</p>';
    
    PHP:
    ... but yes, arrays are definitely the way to go.
     
    nico_swd, Jun 14, 2010 IP