1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

New Variable name in A loop

Discussion in 'PHP' started by qdsouza, Sep 15, 2005.

  1. #1
    hi,

    This is probably I really simple problem. I have a loop and want to create a new set of variables each time the loop completes.

    for ( $v=1; $_POST['rown'] >= $v; $v++ ) {
    	if ($template == 5) {
    		print" <tr>
              <input name=\"$dataa\" type=\"text\" size=\"40\">
              <td bgcolor=\"#FFFFFF\"><input name=\"$dataa\" type=\"text\" size=\"40\"></td>
              <td bgcolor=\"#FFFFFF\"><input name=\"$datab\" type=\"text\" size=\"40\"></td>
              <td bgcolor=\"#FFFFFF\"><input name=\"$datac\" type=\"text\" size=\"40\"></td>
              <td bgcolor=\"#FFFFFF\"><input name=\"$datad\" type=\"text\" size=\"40\"></td>
              <td bgcolor=\"#FFFFFF\"><input name=\"$datae\" type=\"text\" size=\"40\"></td>
    </tr>";
    	}
    PHP:
    So that dataa in the next loop becomes dataa1, then dataa2 etc. Can I use a concatenate to do this?
     
    qdsouza, Sep 15, 2005 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #2
    this should do it
    for ( $v=1; $_POST['rown'] >= $v; $v++ ) {
      if ($template == 5) {
        echo " <tr>
         <input name='$dataa' type='text' size='40'>
         <td bgcolor='#FFFFFF'><input name='dataa{$v}' type='text' size='40'></td>
         <td bgcolor='#FFFFFF'><input name='datab{$v}' type='text' size='40'></td>
         <td bgcolor='#FFFFFF'><input name='datac{$v}' type='text' size='40'></td>
         <td bgcolor='#FFFFFF'><input name='datad{$v}' type='text' size='40'></td>
         <td bgcolor='#FFFFFF'><input name='datae{$v}' type='text' size='40'></td>
       </tr>";
      } 
    }
    PHP:
     
    sarahk, Sep 15, 2005 IP
    qdsouza likes this.
  3. qdsouza

    qdsouza Guest

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks you are awesome!

    Just curious - Why does $data{$v} work and not a concatentation $add.$v ?
     
    qdsouza, Sep 16, 2005 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #4
    because you're already inside a string. The . is seen as plain text.

    {} tells PHP to find a variable to match the variable within (only 1) and not get confused with the other stuff that's plain string. You can leave the {} but it's faster and safer to use it.
     
    sarahk, Sep 16, 2005 IP
  5. qdsouza

    qdsouza Guest

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Using the {} method, is it possible to get php to append a variable (with another variable) before it actually evaluates the variable? So if if have $data{$v} is in a loop. I would want the $v to evaluate first and that would lead to a string that was posted from a form.

    So that $data1 would be evaluated from the post ... $data2 $data3 etc.
     
    qdsouza, Sep 17, 2005 IP