Ankh2054
Ankh2054

Reputation: 1153

Foreach loop - receiving duplicates

Wonder if someone could assist me with the below. I have created a foreach loop to show all the children pages of current page. This works.

I am then trying to show different text for each child based on whether the current logged in user is the owner or not. This works, apart from at each loop iteration the previous results are added prior to the current.

<?php 

    $results = $page->children;

    foreach($results as $reply) { 

        //Bottom post buttin display options - Reply
        if ($reply->createdUser->name == $user->name){

            $bottom_options_reply .= "<p> This is the current owner</p>";

        }

        else  {

            $bottom_options_reply .= "<p> This is not the owner </p>";
        }

        //Theme each topic                   
        echo " 

    <div class='panel panel-default'>

                <div class='panel-body'>
                     <div class='col-md-3'>
                        {$avatar_profile_logo}
                        {$reply->createdUser->name}
                     </div>
                     <div class='col-md-6'>
                        {$reply->body}
                     </div>
                </div>


            <div class='panel-footer'>

                {$bottom_options_reply}

            </div>


    </div>
        ";
?>

Below is a screenshot of the output. enter image description here

Upvotes: 0

Views: 64

Answers (3)

DenisJ
DenisJ

Reputation: 1

This because you make a concatenation. You re-initialize to empty your variable $bottom_options_reply at the begin of your loop.

Regards

Upvotes: 0

andrew
andrew

Reputation: 2098

 $bottom_options_reply .= "<

Here lies your error. With each loop, you add to $bottom_options_reply variable.
Change it to:

$bottom_options_reply = "< // note the missing . 

Upvotes: 3

Onimusha
Onimusha

Reputation: 3385

$bottom_options_reply is appending each time in foreach

Try:

   //Bottom post buttin display options - Reply
   $bottom_options_reply  = ''; //this to reset
   if ($reply->createdUser->name == $user->name){

or remove the . if you're not going to use that variable for anything else before it

Upvotes: 1

Related Questions