我试图找出在foreach之外输出数据的最佳方法,同时仍保持所有结果。
我想要获得的变量是$pending_friend_1
和$pending_friend_img
。
目前,我正在使用一个变量$pending_friend_1
完成此操作,方法是将其设置为数组$friends_pending_arr[] = $pending_friend_1;
,然后将其插入foreach
循环之外。但是,在将其输出到$pending_friend_list
时,我无法弄清楚如何使用两个不同的变量。
有没有比我更好的方法,或者我可以用implode
方法做到这一点,如果是这样,我怎么能用这两个变量做到这一点?我想要<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>';
}
示例:
$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>;
所需的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>
我更新的完整代码:
$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;
?>
答案 0 :(得分:1)
将HTML结构直接放入字符串中,并将变量连接到需要插入的位置。
$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;