SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php problem in for loop

    hi everyone please i want to print the value of the variables
    $myradiobutton1
    $myradiobutton2
    $myradiobutton3
    $myradiobutton4
    ....... $myradiobutton10

    i made a for loop to print the values of each $myradiobutton($i)
    but i can not print it ...
    can anyone help me

    the code is
    Code:
    for($i = 1; $i < 11; $i++)
                 {			
    	
    	  echo $myradiobutton.$i.'<Br><BR>';
    		
    	}
    please try to fix this problem
    thnx in advance

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    PHP Code:

    $str 
    '';
    for(
    $i=1;$i<11;$i++) {

        
    $str.= '.$myradiobutton'.$i.".'<Br><BR>'";

    }

    eval(
    'echo '.substr($str,1).';'); 
    It would probably be easier to just place that stuff in an array rather then variables. Then you won't need to eval.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    PHP Code:

    $str 
    '';
    for(
    $i=1;$i<11;$i++) {

        
    $str.= '.$myradiobutton'.$i.".'<Br><BR>'";

    }

    eval(
    'echo '.substr($str,1).';'); 
    It would probably be easier to just place that stuff in an array rather then variables. Then you won't need to eval.

    thnx for ur reply but i need to print it like this:

    myradiobutton1=..
    myradiobutto2=...
    myradiobutto3=...
    .....etc

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    huh? maybe I'm not understanding because the below code as well as the previous works.

    PHP Code:
    $but1 '1';
    $but2 '2';
    $but3 '3';
    $but4 '4';
    $but5 '5';

    $str '';
    for(
    $i=1;$i<6;$i++) {

        
    $str.= '."<p>but'.$i.' = ".$but'.$i.".'</p>'";

    }

    eval(
    'echo '.substr($str,1).';'); 

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here's a non-eval solution...
    PHP Code:
    <?php
    for($iCounter 1$iCounter <= 10$iCounter++)
    {
        
    $fAnon create_function(''"global \$myRadioButton$iCounter; return isset(\$myRadioButton$iCounter) ? \$myRadioButton$iCounter : false ;");
        if(
    $fAnon())
        {
            
    printf(
                
    '$myRadioButton%s = %s<br />',
                
    $iCounter,
                
    $fAnon()
            );
        }
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net/manual/en/languag...s.variable.php
    PHP Code:
    echo ${'myradiobutton' $i}; 
    I'll second the suggestion you should be using an array here.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Bah!

    You know, I read that page too, so close and yet so far.

    Great work crmalibu.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Evangelist achintya's Avatar
    Join Date
    Apr 2005
    Location
    Chandannagar(India)
    Posts
    459
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can do it like this
    PHP Code:
    for($i 1$i 11$i++)
                 {            
        
          
    $echoval '$myradiobutton'.$i;
              eval(
    "\$echoval = \"$echoval\";");
              echo 
    $echoval.'<BR>';
            
        } 


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
  •