SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: PHP While Loops

  1. #1
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know ya'll must be tired of me asking for help, but hey, this is the only site I've gotten good positive feedback on programming with PHP.

    Anyway, let's say I have a FORM that has 4 fields (In reality it's more like 12):
    nomineename_1
    nomineename_2
    nomineename_3
    nomineename_4

    On the submission page, rather than typing a long-winded code like:
    ...
    if ($nomineename_1) ( echo $nomineename_1; }
    if ($nomineename_2) ( echo $nomineename_2; }
    if ($nomineename_3) ( echo $nomineename_3; }
    if ($nomineename_4) ( echo $nomineename_4; }
    ...
    How could I do something like increment the number at the end of the variable?

    while ($i < 4) {
    if ($nomineename_[number]) {
    echo $nomineename;
    $i++;
    }

    Just something that's got me stuck on stupid for the moment.

    Steven



    <///////~

    www.sohh.com

  2. #2
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try something like this:
    Code:
    <?
     $foo_1 = 'hello';
     $foo_2 = 'how';
     $foo_3 = 'are';
     $foo_4 = 'you';
     $i = 1;
     while($i<=4)
     {
    
            echo ${"foo_$i"}." ";
            $i++;
     }
    ?>
    output: 'hello how are you'

    hope that helps


  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or just make an array out of them

    nomineename[]
    nomineename[]
    nomineename[]
    nomineename[]


    for($i = 0;$i<count($nomineename);$i++) {
    print $nomineename[$i];
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here the eval() function work great!
    http://www.php.net/eval
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  5. #5
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey robp that worked like a charm. But Freddie, I did do your "for" statement. That worked even better than while, as I was getting a mis-count.

    I have no idea how or why I would use the eval() function Son Nguyen. Maybe you could explain why you would do it with the eval() function. I'd like to know how to use it.

    Steven
    <///////~

    www.sohh.com

  6. #6
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    eval($string) will execute the command you put in there, so say for your situation:

    Code:
    $foo_1 = "Value for foo1";
    $foo_2 = "Value for foo2";
    $foo_3 = "Value for foo3";
    
    for ($i=1;$i<3;$i++)
    { $val = "";
      eval("\$val = \$foo_".$i.";");
      echo($val);
    }
    will print values of 3 entries. Well, this situation the other solution should be the best, but eval could do much more than just converting the variable names!



    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


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
  •