WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] if/else inside foreach loop problem (13 posts)

  1. echstudios
    Member
    Posted 5 years ago #

    I've set up a foreach loop which works fine, except for some reason a standard if/else statement is not working properly. It seems to be returning true for the same variable on each loop.

    if ($results) {
    	        echo "<ul>";
    	        foreach ($results as $o):
    	        	$type = $o->action_type;
    	        	$post = get_post($o->post_id);
    	        	$time = $o->action_time;
    	        	$user = $o->user_id;
    
    	        if ($type = 'lame') {
    	        	$phrase = 'this was lame';
    	        } else {
    	        	$phrase = 'something else';
    	        } ?>	        	
    
    	<div class="floatLeft"><?php user_avatar($user, 50); ?></div>
    	<p><?php echo TimeAgoInWords($time); ?> ago</p>
    	<p><?php user_nickname($user); ?> <?php echo $phrase; ?></p>
    
    	       <?php   endforeach; }

    Its seems that as long as any value is "lame" it will return true, yet if i just echo out the value without the if/else, it will put the actual value.

    Any ideas?

  2. esmi
    Forum Moderator
    Posted 5 years ago #

    if ($type = 'lame') {
    	        	$phrase == 'this was lame';
    	        } else {
    	        	$phrase == 'something else';
    	        } ?>

    Note the double equals. :-)

  3. echstudios
    Member
    Posted 5 years ago #

    Thanks for the quick reply but that did not work. In fact that just made nothing show up.

  4. esmi
    Forum Moderator
    Posted 5 years ago #

    Sorry! Right approach. Wrong edits.

    if ($type == 'lame') {
    	        	$phrase = 'this was lame';
    	        } else {
    	        	$phrase = 'something else';
    	        } ?>
  5. echstudios
    Member
    Posted 5 years ago #

    Ya that was my first though but that too is not working. Its seems that the if/else is searching the entire array and returns true if the value is anywhere in there instead of just on its current loop. But again, like I said, if I simply echo out the value, it echo the individual value of that loop.

    [moderated--bump removed. Please refrain from bumping as per Forum Rules]

  6. MichaelH
    Member
    Posted 5 years ago #

    Please describe, in words, what you want your code to do.

  7. jamida
    Member
    Posted 5 years ago #

    I'm not familiar with this syntax. Is it valid PHP?

    foreach ($results as $o):
        $type = $o->action_type;
    endforeach;

    Specifically the ":" at the end of the foreach line and the closing "endforeach;".

    If indeed this is the problem then it could be that the closing ":" on the foreach line just makes the loop run through all the $results with the effect that $o is assigned the last result and then your code runs.

    Good luck

  8. echstudios
    Member
    Posted 5 years ago #

    My apologies. Here's what I'm looking to do. I have a custom table with a specific field "action_type" that has 5 or so different possible values, example: 'vote', 'favorite', 'comment', etc. I'm listing out the actions taken by users chronologically by time.

    For example, if the action_type is 'vote' then write "Username has voted on Post 17",
    or if action_type is 'favorite', then write "Username has added Post 17 to his favorites" etc.

    The problem that I'm having is setting up a conditional statement to handle the output phrases within the foreach loop. When I test the current iteration for the action_type == 'vote', it seems to return true if ANY value in my array is 'vote'. Because of this, each and every loop iteration returns the same phrase as the first value that in the array returned trued.

    However, like I said, if I simply echo out the action_type, each loop iteration will write the appropriate type: ie, 'vote' 'vote' 'favorite' 'vote' 'comment' ect.

    Please let me know if if that makes sense or if I can clarify any part. Thanks again!

  9. MichaelH
    Member
    Posted 5 years ago #

    After:
    foreach ($results as $o):

    put this to make sure $o has the necessary elements:

    echo "<pre>"; print_r($o); echo "</pre>";

  10. echstudios
    Member
    Posted 5 years ago #

    Now I take it this is just for testing purposes? This is what it prints on a single loop iteration:

    stdClass Object
    (
        [user_id] => 2
        [post_id] => 131
        [action_type] =>  vote
        [action_time] => 2009-07-15 13:32:06
    )

    Each loop is returning the proper values.

  11. alchymyth
    The Sweeper & Moderator
    Posted 5 years ago #

    i am new to this, just a maybe useless question:
    is ' vote' the same as 'vote' i.e. is a preceeding 'space' important for the comparison?

  12. TomPhil
    Member
    Posted 5 years ago #

    Thanks

  13. echstudios
    Member
    Posted 5 years ago #

    alchymyth, new or not, you are absolutely right! I did indeed accidentally leave a space in my variable. Problem solved. Thanks so much. And thanks MichaelH for allowing him to find it. Really appreciate it guys!

Topic Closed

This topic has been closed to new replies.

About this Topic