Paul Paul - 12 days ago 8
PHP Question

Displaying data out of foreach statements

I am trying to figure out the best way to output data outside of a foreach while still maintaining all the results.

The variables I am trying to get are

$pending_friend_1
and
$pending_friend_img
.

Currently, I am accomplishing this with the one variable
$pending_friend_1
by setting it to an array
$friends_pending_arr[] = $pending_friend_1;
and then imploding it outside of the
foreach
loop. However, I cannot figure out how to do it with two different variables while outputting it into the
$pending_friend_list
.

Is there a better way to do this than what I am or can I do it with the
implode
method and if so, how can I do it with both variables? I am wanting both variables within
<div class="pending_friend_user">
.

$pending_friend_list = null;
$pending_friend_images = null;
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'];
$pending_friend_img = $pending_friend_row['img'];
$total_pending_friends = $pending_friend_1 . "<br>" . $pending_friend_2;

if ($pending_friend_2 == $user_id) {
$pending_friend_list_approve = '<img src="../icons/collection/checkmark.png" class="pending_approve_friend" alt="Approve Friend">';
$pending_friend_list_decline = '<img src="../icons/collection/x-sign.png" class="pending_decline_friend" alt="Approve Friend">';
$friends_pending_arr[] = $pending_friend_1;
$friends_pending_img_arr[] = $pending_friend_img;
$pending_friend_list = '<div class="pending-friend-list-row"><div class="pending_friend_user">'. implode
('</div><div class="pending_friend_list_decision"><div class="total-center">' . $pending_friend_list_approve . $pending_friend_list_decline .
'</div></div></div><div class="pending-friend-list-row"><div class="pending_friend_user">', $friends_pending_arr) .
'</div><div class="pending_friend_list_decision"><div class="total-center">' . $pending_friend_list_approve . $pending_friend_list_decline . '</div></div></div>';
}


Example:

$pending_friend_list = null;
$pending_friend_images_output = "";
$friends_pending_output = "";
forloop {
$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'];
$pending_friend_img = $pending_friend_row['img'];
$friends_pending_output .= $pending_friend_1;
$pending_friend_images_output .= '<img src="' . $pending_friend_img .'">';
}
$pending_friend_list =
'<div class="pending-friend-list-row"><div class="pending_friend_user"><div class="pending_friend_list_img"> ' . echo $pending_friend_images_output . '</div>
</div><div class="pending_friend_list_decision"><div class="total-center">' . $pending_friend_list_approve . $pending_friend_list_decline .
'</div></div></div>;


Desired HTML:

<div id="pending-friend-list-dropdown">
<div id="pending-friend-list-container">
<div id="pending-friend-list-summary">
<div class="pending-friend-list-row">
<div class="pending_friend_user">
<div class="pending_friend_list_img">
<img src=friend1>
</div>
<div> user_id //friend 1// </div>
</div>
<div class="pending_friend_list_decision">
<div class="total-center">
<img src="../icons/collection/checkmark.png" class="pending_approve_friend" alt="Approve Friend">
<img src="../icons/collection/x-sign.png" class="pending_decline_friend" alt="Decline Friend">
</div>
</div>
</div>
<div class="pending-friend-list-row">
<div class="pending_friend_user">
<div class="pending_friend_list_img">
<img src=friend1>
</div>
<div> user_id //friend 2// </div>
</div>
<div class="pending_friend_list_decision">
<div class="total-center">
<img src="../icons/collection/checkmark.png" class="pending_approve_friend" alt="Approve Friend">
<img src="../icons/collection/x-sign.png" class="pending_decline_friend" alt="Decline Friend">
</div>
</div>
</div>
<div class="pending-friend-list-row">
<div class="pending_friend_user">
<div class="pending_friend_list_img">
<img src=friend1>
</div>
<div> user_id //friend 3// </div>
</div>
<div class="pending_friend_list_decision">
<div class="total-center">
<img src="../icons/collection/checkmark.png" class="pending_approve_friend" alt="Approve Friend">
<img src="../icons/collection/x-sign.png" class="pending_decline_friend" alt="Decline Friend">
</div>
</div>
</div>
</div>
</div>
</div>


My updated full code:

$pending_list_output =
'<div id="pending-friend-list-dropdown">
<div id="pending-friend-list-container">
<div id="pending-friend-list-summary">
<div class="pending_friend_list_content"><?php echo $total_pending_count; ?></div>';

//Display pending friends
//$pending_friend_list = null;
$status_pending = '1';
$friends_pending_sql = "
SELECT f.*, p.*,coalesce(p.img, 'profile_images/default.jpg') as img
FROM friends f LEFT JOIN
profile_img p
ON p.user_id = f.friend_one
WHERE f.friend_two = ? AND f.status = ? AND
p.id = (select max(p2.id) from profile_img p2 where p2.user_id = p.user_id)
";
$pending_friend_count_stmt = $con->prepare($friends_pending_sql);
$pending_friend_count_stmt->execute(array($user_id, $status_pending));
$pending_friend_rows = $pending_friend_count_stmt->fetchAll(PDO::FETCH_ASSOC);

$pending_friend_list_approve = '<img src="../icons/collection/checkmark.png" class="pending_approve_friend" alt="Approve Friend">';
$pending_friend_list_decline = '<img src="../icons/collection/x-sign.png" class="pending_decline_friend" alt="Decline Friend">';
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'];
$pending_friend_img = $pending_friend_row['img'];
$total_pending_friends = $pending_friend_1 . "<br>" . $pending_friend_2;
//$friends_pending_output .= $pending_friend_1;
$pending_friend_images_output = '<img src="' . $pending_friend_img .'">';

if ($pending_friend_2 == $user_id) {
$pending_friend_list_approve;
$pending_friend_list_decline;
$pending_list_output .=
'<div class="pending-friend-list-row">
<div class="pending_friend_user">
<div class="pending_friend_list_img"> ' . $pending_friend_images_output . '</div>
<div class="pending_friend_list_username">' . $pending_friend_1 . '</div>
</div>
<div class="pending_friend_list_decision">
<div class="total-center">' . $pending_friend_list_approve . $pending_friend_list_decline . '</div>
</div>
</div>';
}
else {
$pending_list_output = "No pending friends at this time.";
}
}
if ($total_pending_count == 0) {
$total_pending_count = "No new requests.";
}
else {
$total_pending_count = "Pending Friends - " . $total_pending_count;
}
$pending_list_output .=
'</div>
</div>
</div>';
echo $pending_list_output;
?>


enter image description here

Answer

Just put your HTML structure directly into the strings, and concatenate the variables where they need to be inserted.

$output = '<div id="pending-friend-list-dropdown">
                <div id="pending-friend-list-container">
                    <div id="pending-friend-list-summary">';
foreach ($pending_friend_rows as $pending_friend_row) {
    $pending_friend_1           = $pending_friend_row['friend_one'];
    $pending_friend_img         = $pending_friend_row['img'];
    $output .= '<div class="pending-friend-list-row">
                    <div class="pending_friend_user">
                        <div class="pending_friend_list_img">
                            <img src="' . $pending_friend_img . '">
                        </div>  
                        <div>' . $pending_friend_1 . '</div>
                    </div>
                    <div class="pending_friend_list_decision">
                        <div class="total-center">
                            <img src="../icons/collection/checkmark.png" class="pending_approve_friend" alt="Approve Friend">
                            <img src="../icons/collection/x-sign.png" class="pending_decline_friend" alt="Decline Friend">
                        </div>
                    </div>
                </div>';
}
$output .= '</div>
        </div>
    </div>';

echo $output;