#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Create, Set and Get Variable Values in a loop


    Below is my code. Basically, where it's breaking on me is the echo statement... it's just spitting out 'One[0]'. Can anyone tell me what I'm doing wrong... I have a feeling it's something so simple that I'm missing:

    Function to convert from number to word
    Code:
    $NumAsString = array( '0' => 'Zero', '1' => 'One', '2' => 'Two', '3' => 'Three', '4' => 'Four', '5' => 'Five', '6' => 'Six', '7' => 'Seven', '8' => 'Eight', '9' => 'Nine', '10' => 'Ten', );
    Create the variables in a loop, creates 'InsideStudsZero' , 'InsideStudsOne' , etc.
    Code:
    for ($i=0; $i<=6; $i++) { ${InsideStuds . strtr($i, $NumAsString)} = get_post_custom_values('InsideStuds' . strtr($i, $NumAsString), $post->ID); }
    Echo the value of the variables - previous statement returned an array for each variable... so I have to get the first value in the array for each. -- THIS IS THE PART THAT I'm MESSING UP SOMETHING -- instead of showing me the value of the first item in the array for that variable, it gives me 'ONE[0]', 'TWO[0]'
    Code:
    <?php for($i=1; $i<=6; $i++) { ?> <div class="one column center"><?php echo $InsideStuds . strtr($i, $NumAsString) . '[0]'; ?></div> <?php }?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,332
    Rep Power
    594
    Can you explain better what you are trying to do. I don't understand what you are expecting strtr to return and print.

    P.S. Please wrap your code in PHP tags.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0
    strtr translates the number to a word... so in the loop as $i increments... $i = 1, $ = 2, etc... strtr will translate 1 to One, 2 to Two, etc.

    That part is working like it should. Basically, the problem is when I try to re-build up the variable name in another loop to echo it.

    Originally Posted by gw1500se
    Can you explain better what you are trying to do. I don't understand what you are expecting strtr to return and print.

    P.S. Please wrap your code in PHP tags.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,332
    Rep Power
    594
    I think I see. You are expecting a generated string to be treated as a variable. It doesn't work that way so you are getting exactly what you are telling PHP to do. While a poor practice, you need to use 'eval'. The gurus here will tell you there is virtually never any reason to use 'eval'. Perhaps if you explained what you are trying to do, we can come up with a better solution.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0
    I knew I was doing something silly. I knew it wouldn't parse the string, but I didn't know about eval. Thanks for the tip.

    Normally, I wouldn't have this problem, but I'm working with another coder and he's new, so it's either redo his hundreds of variables or work with what I have. I know there are several better ways to have handled it.

    Thanks for the tip though!

    Originally Posted by gw1500se
    I think I see. You are expecting a generated string to be treated as a variable. It doesn't work that way so you are getting exactly what you are telling PHP to do. While a poor practice, you need to use 'eval'. The gurus here will tell you there is virtually never any reason to use 'eval'. Perhaps if you explained what you are trying to do, we can come up with a better solution.

IMN logo majestic logo threadwatch logo seochat tools logo