Support » Fixing WordPress » if/else inside foreach loop problem

  • Resolved echstudios


    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?

Viewing 12 replies - 1 through 12 (of 12 total)
  • esmi


    Forum Moderator

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

    Note the double equals. 🙂

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



    Forum Moderator

    Sorry! Right approach. Wrong edits.

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

    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]

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

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

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

    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

    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!

    foreach ($results as $o):

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

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

    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.

    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?


    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!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘if/else inside foreach loop problem’ is closed to new replies.