我遇到了一个包含爆炸列表的问题。数据是正确的,但我无法弄清楚如何把它放到一个格式,每个结果(数据)被包装到div我试图用它包围它:
$pending_friend_list = '<div class="pending-friend-list-row">'. implode($friends_pending_arr) . '</div>';
我希望它看起来像这样:
<div class="pending-friend-list-row">1</div>
<div class="pending-friend-list-row">2</div>
<div class="pending-friend-list-row">3</div>
现在所做的就是将所有内爆数据放入一个div中,例如<div class="pending-friend-list-row">123</div>
有什么想法吗?
完整代码:
$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;
}
}
//echo implode("<br>",$friends_pending_arr);
//$pending_friend_list = implode("<br>",$friends_pending_arr);
$pending_friend_list = '<div class="pending-friend-list-row">'. implode($friends_pending_arr) . '</div>';
echo $friend_status_button . "<br>";
echo $profile_viewer_message;
?>
<div id="main">
<div id="pending-friend-list-container">
<div id="pending-friend-list-summary">
<?php //echo '<div class="pending-friend-list-row">'. $pending_friend_list . '</div>';
echo $pending_friend_list;
?>
</div>
CSS
.pending-friend-list-row {
height: 50px;
width: 100%;
border: 1px solid green;
background: gray;
}
答案 0 :(得分:3)
你希望你的胶水结束一个div并开始下一个这样的:
$pending_friend_list = '<div class="pending-friend-list-row">'. implode('</div><div class="pending-friend-list-row">', $friends_pending_arr) . '</div>';
有关如何使用implode
粘合剂的更多信息答案 1 :(得分:1)
你可以在foreach循环中将你的html放在一个变量中,然后打印出来,就像这样
$output = '';
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) {
$pending_friend_list .= <div class="pending-friend-list-row">'. $pending_friend_1 . '</div>'
}
}
您现在可以在任何地方回复$pending_friend_list
。
echo $friend_status_button . "<br>";
echo $profile_viewer_message;
?>
<div id="main">
<div id="pending-friend-list-container">
<div id="pending-friend-list-summary">
<?php echo $pending_friend_list;?>
</div>
希望有所帮助
答案 2 :(得分:1)
$myArray = ['foo', 'bar', 'baz'];
$output = implode("\n", array_map(
function($a){ return sprintf('<div class="divvy-div">%s</div>', $a); },
$myArray
));
echo $output;
输出:
<div class="divvy-div">foo</div>
<div class="divvy-div">bar</div>
<div class="divvy-div">baz</div>