我想以某种方式在foreach循环中对项目进行分组,以便我可以有效地显示它们。例如,如果我有以下MySql表:
user_id username group_id
1 John 1
2 Mark 2
3 Steve 2
我希望group_id 2中的用户一起显示在<div class="group2">Username 1: Mark, Username 2: Steve</div>
中,用户显示在group_id 1 <div class="group1">Username John</div>
我希望你能理解这个问题。
答案 0 :(得分:1)
您可以通过group_id
订购查询,然后跟踪其更改时间,相当轻松地完成此操作。像这样:
$query = "SELECT * FROM table ORDER BY group_id";
$results = $mysqli->query($query);
$lastGroup = 0;
while ( $row = $results->fetch_assoc() )
{
if ( $row['group_id'] != $lastGroup )
{
// If there is already a div open we need to close it, so if
// last group > 0 then a div has been opened.
echo ($lastGroup > 0 ? '</div>' : '') . '<div class="group' . $row['group_id'] . '">';
}
$lastGroup = $row['group_id'];
echo $row['username'] . '<br>';
}
// close the last open div
echo '</div>';
或者,您可以使用MySQL GROUP_CONCAT
函数来获取组中的所有用户名。使用此:
$query = "SELECT group_id,
GROUP_CONCAT(username) as users
FROM table
GROUP BY group_id";
$results = $mysqli->query($query);
while ( $row = $results->fetch_assoc() )
{
echo '<div class="group' . $row['group_id'] . '">' . $row['users'] . '</div>';
}
答案 1 :(得分:0)
创建2D数组:
$users;
Foreach结果行$ row:
$users[$row->group_id][$row->user_id] = $row->username;
然后:
foreach($users as $group_id -> $group_users) {
echo '<div class="' . $group_id . '">';
foreach($group_users as $user_id -> $username) {
echo " Username: ";
echo $user_id;
echo " :";
echo $username;
}
echo "</div>";
}
答案 2 :(得分:0)
强烈建议您使用mysql GROUPing
函数制作查询,这样您就不必机械地执行此操作。但是,如果出于任何原因,您的结果集看起来像您发布的数据,并且您希望循环然后按照您的描述显示记录;您可以遍历行的数组,将同一组中的所有用户捆绑到一个唯一的子数组中,然后为显示运行foreach
循环。下面的代码演示了如何使用示例:
<?php
// SIMULATE AN TO REPRESENT THE RECORDS FROM THE DB TABLE...
$array = array(
array(
"user_id" => 1,
"username" => "John",
"group_id" => 1,
),
array(
"user_id" => 2,
"username" => "Mark",
"group_id" => 2,
),
array(
"user_id" => 2,
"username" => "Steve",
"group_id" => 2,
),
);
// CREATE AN ARRAY FOR USE IN GROUPING USERS WITH THE SAME group_id
$groupArray = array();
// WALK THE SIMULATED ARRAY (IN YOUR CASE YOUR ROWS)
// AND BUILD UP THE $groupArray
$result = array_walk($array, function($data) use(&$groupArray) {
if(!array_key_exists($data['group_id'], $groupArray)){
$groupArray[$data['group_id']][] = $data;
}else{
if(!in_array($data, $groupArray)){
$groupArray[$data['group_id']][] = $data;
}
}
});
// CREATE AN OUTPUT TO HOLD THE HTML CONTENT FOR DISPLAY:
$output = "";
// LOOP THROUGH THE $groups AND BUILD UP YOUR DATA...
foreach ($groupArray as $groupID=>$groups){
$output.= "<div class='group" . $groupID . "'>" . PHP_EOL;
$output.= "<h3 class='group-users'>Users in Group {$groupID}</h3>" . PHP_EOL;
foreach($groups as $group){
$output.= "<span class='user-name'>Username: " . $group['user_id'] . ": {$group['username']},</span><br />" . PHP_EOL;
}
$output.= "</div>" . PHP_EOL;
}
var_dump($output);
// echo $output;
// THE var_dump($output) PRODUCES::
string '<div class='group1'>
<h3 class='group-users'>Users in Group 1</h3>
<span class='user-name'>Username: 1: John,</span><br />
</div>
<div class='group2'>
<h3 class='group-users'>Users in Group 2</h3>
<span class='user-name'>Username: 2: Mark,</span><br />
<span class='user-name'>Username: 2: Steve,</span><br />
</div>
' (length=317)