0

I am stuck on a possibly very simple thing...just can't figure it out. I have this: echo "Pending Friends Are - " . $pending_friend_1 . "<br>"; in my loop. I need the $pending_friend_1 to be within the loop while outputting it, otherwise I do not get my full loop of results. How can I take out the text Pending Friends Are - so that is still pieced together with the list. I am trying to do something like this:

Pending Friends Are -

Bob

George

Etc

    <div id="main">
<?php
//Display pending friends
    $friends_pending_sql = "
        SELECT *
        FROM friends
        WHERE friend_two = ?
        AND status = ?
    ";
    $pending_friend_count_stmt = $con->prepare($friends_pending_sql);
    $pending_friend_count_stmt->execute(array($user_id, $status_one));
    $pending_friend_rows = $pending_friend_count_stmt->fetchAll(PDO::FETCH_ASSOC);
    echo '<div id="pending-request_count">Total Pending Friends -' . $total_pending_count . '</div>';
    foreach ($pending_friend_rows as $pending_friend_row) {
        $pending_friend_1           = $pending_friend_row['friend_one'];
        $pending_friend_2           = $pending_friend_row['friend_two'];
        $pending_friend_status      = $pending_friend_row['status'];
        $pending_friend_status_date = $pending_friend_row['date'];
        $total_pending_friends      = $pending_friend_1 . "<br>" . $pending_friend_2;

        if ($pending_friend_2 == $user_id) {

            echo "Pending Friends Are - " . $pending_friend_1 . "<br>";
        }
        else if ($pending_friend_1 = $user_id) {
            echo "Friend Requests waiting for approval - " . $total_requests_sent_count . "<br>";
        }
    }
    echo $friend_status_button;
    echo $profile_viewer_message;
?>
6
  • You could save all results into an array and print them in your desired output. Commented Nov 19, 2016 at 5:41
  • @Manikiran Could you show me a basic concept or point me towards an online example? Commented Nov 19, 2016 at 5:43
  • You can do something like this. $pending_friends = array(); foreach($desired_array as $key => $value){ $pending_friends[] = $value . '<br>'; } print_r($pending_friends); Commented Nov 19, 2016 at 5:44
  • You can look at this stackoverflow.com/questions/676677/… Commented Nov 19, 2016 at 5:47
  • 2
    Please fix this error- else if ($pending_friend_1 = $user_id) { is an assignment and not a comparision... It needs == or === if you are matching value and type. Commented Nov 19, 2016 at 5:55

3 Answers 3

1

insert to an array

$friends_pending_arr[]=$pending_friend_1;  

Then show outside the loop

   echo "Pending Friends Are - <br>";
   echo implode("<br>",$friends_pending_arr);

Full Code

<div id="main">
<?php
//Display pending friends
    $friends_pending_sql = "
        SELECT *
        FROM friends
        WHERE friend_two = ?
        AND status = ?
    ";
    $pending_friend_count_stmt = $con->prepare($friends_pending_sql);
    $pending_friend_count_stmt->execute(array($user_id, $status_one));
    $pending_friend_rows = $pending_friend_count_stmt->fetchAll(PDO::FETCH_ASSOC);
    echo '<div id="pending-request_count">Total Pending Friends -' . $total_pending_count . '</div>';
    foreach ($pending_friend_rows as $pending_friend_row) {
        $pending_friend_1           = $pending_friend_row['friend_one'];
        $pending_friend_2           = $pending_friend_row['friend_two'];
        $pending_friend_status      = $pending_friend_row['status'];
        $pending_friend_status_date = $pending_friend_row['date'];
        $total_pending_friends      = $pending_friend_1 . "<br>" . $pending_friend_2;

        if ($pending_friend_2 == $user_id) {

            $friends_pending_arr[]=$pending_friend_1;
        }
        else if ($pending_friend_1 == $user_id) {
            echo "Friend Requests waiting for approval - " . $total_requests_sent_count . "<br>";
        }
    }

    echo "Pending Friends Are - <br>";
    echo implode("<br>",$friends_pending_arr);

    echo $friend_status_button;
    echo $profile_viewer_message;
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Is implode a better method to use rather than another foreach?
1

You could try this:

$friends_pending_arr=array();
$friends_pending_sql = "
    SELECT *
    FROM friends
    WHERE friend_two = ?
    AND status = ?
";
$pending_friend_count_stmt = $con->prepare($friends_pending_sql);
$pending_friend_count_stmt->execute(array($user_id, $status_one));
$pending_friend_rows = $pending_friend_count_stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<div id="pending-request_count">Total Pending Friends -' . $total_pending_count . '</div>';
foreach ($pending_friend_rows as $pending_friend_row) {
    $pending_friend_1           = $pending_friend_row['friend_one'];
    $pending_friend_2           = $pending_friend_row['friend_two'];
    $pending_friend_status      = $pending_friend_row['status'];
    $pending_friend_status_date = $pending_friend_row['date'];
    $total_pending_friends      = $pending_friend_1 . "<br>" . $pending_friend_2;

    if ($pending_friend_2 == $user_id) {
        $friends_pending_arr[]=$pending_friend_1;
    }
    else if ($pending_friend_1 == $user_id) {
        echo "Friend Requests waiting for approval - " . $total_requests_sent_count . "<br>";
    }
}
echo "Pending Friends Are - ";
foreach($friends_pending_arr as $friend){echo $friend . "<br>";}

2 Comments

I get undefined $friend and it throws and error for the foreach you added.
@Manikiran , you might want to also fix the incorrect comparison in the else if ($pending_friend_1 = $user_id) { :) Seems everyone has missed that.
1

It seems there is either an intentional code or a potential bug on the else branch of your Code, within the foreach Loop. else if ($pending_friend_1 = $user_id) { is more an assignment than a check for equality. Perhaps the code below - which is essentially your code: with a few minor tweaks - could help.

<?php
    // OBSERVE THAT THE GLOBAL PART OF YOUR PHP WAS MOVED OUT
    // FROM THE DIV... YOU DON'T NEED IT WITHIN A HTML DIV

    // CREATE A STRING TO HOLD PENDING FRIENDS.
    $strPendingFriends          = "";
    $friends_pending_sql        = "
        SELECT *
        FROM friends
        WHERE friend_two = ?
        AND status = ?
    ";
    $pending_friend_count_stmt  = $con->prepare($friends_pending_sql);
    $pending_friend_count_stmt->execute(array($user_id, $status_one));
    $pending_friend_rows        = $pending_friend_count_stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div id="main">
    <?php if($total_pending_count){ ?>
    <!-- Display pending friends -->
    <div id="pending-request_count">Total Pending Friends - <?php echo $total_pending_count; ?></div>
    <?php } ?>
    <?php
        foreach ($pending_friend_rows as $pending_friend_row) {
            $pending_friend_1           = $pending_friend_row['friend_one'];
            $pending_friend_2           = $pending_friend_row['friend_two'];
            $pending_friend_status      = $pending_friend_row['status'];
            $pending_friend_status_date = $pending_friend_row['date'];
            $total_pending_friends      = $pending_friend_1 . "<br>" . $pending_friend_2;

            if ($pending_friend_2 == $user_id) {
                $strPendingFriends     .= "Pending Friends Are - " . $pending_friend_1 . "<br>";
            }else if ($pending_friend_1  == $user_id) {  //<== USED '==' THAN '='               
                $strPendingFriends     .= "Friend Requests waiting for approval - " . $total_requests_sent_count . "<br>";
            }
            echo $strPendingFriends;

        }
        echo $friend_status_button;
        echo $profile_viewer_message;
    ?>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.