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;
?>
Navita Saini
  • 362
  • 3
  • 12
Paul
  • 3,348
  • 5
  • 32
  • 76
  • You could save all results into an array and print them in your desired output. – Manikiran Nov 19 '16 at 05:41
  • @Manikiran Could you show me a basic concept or point me towards an online example? – Paul Nov 19 '16 at 05:43
  • You can do something like this. $pending_friends = array(); foreach($desired_array as $key => $value){ $pending_friends[] = $value . '
    '; } print_r($pending_friends);
    – Amir Hossain Nov 19 '16 at 05:44
  • You can look at this http://stackoverflow.com/questions/676677/how-to-add-elements-to-an-empty-array-in-php – Amir Hossain Nov 19 '16 at 05: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. – TimBrownlaw Nov 19 '16 at 05:55
  • @TimBrownlaw Thanks! Didn't catch that. – Paul Nov 19 '16 at 05:57

3 Answers3

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>";}
Manikiran
  • 2,618
  • 1
  • 23
  • 39
  • I get undefined $friend and it throws and error for the foreach you added. – Paul Nov 19 '16 at 05:52
  • @Manikiran , you might want to also fix the incorrect comparison in the else if ($pending_friend_1 = $user_id) { :) Seems everyone has missed that. – TimBrownlaw Nov 19 '16 at 05:57
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;
?>
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
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>
fuxia
  • 62,923
  • 6
  • 54
  • 62
Poiz
  • 7,611
  • 2
  • 15
  • 17