What I'd like is to create a new unordered list for each new value in a mysql result column.
我想要的是为mysql结果列中的每个新值创建一个新的无序列表。
My query looks like this and i'm throwing it into a data array:
我的查询看起来像这样,我把它扔进一个数据数组:
$connection = dbconnect();
$getusers = "SELECT users.usr_id, users.usr_firstname, users.usr_lastname, user_groups.group_name FROM users INNER JOIN user_groups ON users.usr_group = user_groups.group_id ORDER BY group_name ASC, users.usr_firstname ASC, users.usr_lastname ASC";
$result = mysql_query($getusers);
$data_array = array();
while($data = mysql_fetch_array($result)){
$data_array[] = $data;
}
Now I need to display that data so that each new user_group is an unordered list, and each row with that same group, comes up as a list item of that unordered list.
现在我需要显示该数据,以便每个新的user_group是一个无序列表,并且每个具有相同组的行将作为该无序列表的列表项出现。
it's very similar to this question ( PHP/MySQL Query Results ), but i'm having trouble getting the closing ul's in the right place. Here's my code for outputting, though I know it's wrong because the li's aren't really children of the ul's.
它与这个问题(PHP / MySQL查询结果)非常相似,但是我无法在正确的位置获得结束的ul。这是我输出的代码,虽然我知道这是错的,因为li不是ul的孩子。
$previousgroup = "";
foreach($data_array as $users){
if($users['group_name'] != $previousgroup){
echo "<ul class=\"group\">" . $users['group_name'] . "</ul>";
}
else{
echo "<li id=\"m" . $users['usr_id'] . "\"><h4>" . $users['usr_firstname'] . " " . $users['usr_lastname'] . ", " . $users['cred_name'] . "</h4></li>";
}
}
Is there a more efficient way of doing this? Thanks for your help.
有没有更有效的方法呢?谢谢你的帮助。
2 个解决方案
#1
1
You could change the while loop this way:
您可以通过以下方式更改while循环:
$data_array = array();
while($data = mysql_fetch_array($result)){
$data_array[$data['group_name']][] = $data;
}
and you would get an array that contains your group_name's as index of other arrays which contain your data. To create the list, you could do the following:
你会得到一个数组,其中包含你的group_name作为包含你的数据的其他数组的索引。要创建列表,您可以执行以下操作:
<ul>
<?php
foreach ($data_array as $temp_key => $temp_array)
{
?>
<li>
<?php echo $temp_key; ?>
<ul>
<?php
foreach ($temp_array as $datum)
{
echo ("<li>" . $datum['usr_firstname'] . "</li>";
}
?>
</ul>
</li>
<?php
}
?>
</ul>
Hope it works for you
希望对你有帮助
#2
1
if($users['group_name'] != $previousgroup){
echo "</ul><ul class=\"group\"><li>" . $users['group_name'] . "</li>";
}
echo "<li id=\"m" . $users['usr_id'] . "\"><h4>" . $users['usr_firstname'] . " " . $users['usr_lastname'] . ", " . $users['cred_name'] . "</h4></li>";
The code in your loop should look like this, and you need to open and close a <ul>
from outside the loop.
循环中的代码应如下所示,您需要从循环外部打开和关闭
-
。
echo '<ul>';
foreach () { // here is the loop }
echo '</ul>';
THis is doing it the "dirty" way with printing out as you go, at least.
至少在你去的时候打印它是“脏”的方式。
The way I would do it would be to build a 2D array indexed first by group_name and then by user ID. It would mean making two passes at the data as a second loop would be needed to display the data, but generally speaking you're not going to feel the difference.
我这样做的方法是先构建一个由group_name索引的二维数组,然后再按用户ID编制索引。这意味着要对数据进行两次传递,因为显示数据需要第二个循环,但一般来说,你不会感觉到差异。
#1
1
You could change the while loop this way:
您可以通过以下方式更改while循环:
$data_array = array();
while($data = mysql_fetch_array($result)){
$data_array[$data['group_name']][] = $data;
}
and you would get an array that contains your group_name's as index of other arrays which contain your data. To create the list, you could do the following:
你会得到一个数组,其中包含你的group_name作为包含你的数据的其他数组的索引。要创建列表,您可以执行以下操作:
<ul>
<?php
foreach ($data_array as $temp_key => $temp_array)
{
?>
<li>
<?php echo $temp_key; ?>
<ul>
<?php
foreach ($temp_array as $datum)
{
echo ("<li>" . $datum['usr_firstname'] . "</li>";
}
?>
</ul>
</li>
<?php
}
?>
</ul>
Hope it works for you
希望对你有帮助
#2
1
if($users['group_name'] != $previousgroup){
echo "</ul><ul class=\"group\"><li>" . $users['group_name'] . "</li>";
}
echo "<li id=\"m" . $users['usr_id'] . "\"><h4>" . $users['usr_firstname'] . " " . $users['usr_lastname'] . ", " . $users['cred_name'] . "</h4></li>";
The code in your loop should look like this, and you need to open and close a <ul>
from outside the loop.
循环中的代码应如下所示,您需要从循环外部打开和关闭
-
。
echo '<ul>';
foreach () { // here is the loop }
echo '</ul>';
THis is doing it the "dirty" way with printing out as you go, at least.
至少在你去的时候打印它是“脏”的方式。
The way I would do it would be to build a 2D array indexed first by group_name and then by user ID. It would mean making two passes at the data as a second loop would be needed to display the data, but generally speaking you're not going to feel the difference.
我这样做的方法是先构建一个由group_name索引的二维数组,然后再按用户ID编制索引。这意味着要对数据进行两次传递,因为显示数据需要第二个循环,但一般来说,你不会感觉到差异。