Php 加载按字母顺序排列的数组
我有一组用户,如下所示:Php 加载按字母顺序排列的数组,php,html,arrays,Php,Html,Arrays,我有一组用户,如下所示: Andrew Smith Alex Williams Sam White Stuart Smith 我期待着使用创建一个列表,看起来像这样 A Andrew Smith Alex Williams S Sam White Stuart Smith 我尝试使用一个PHP Foreach循环和一个break来实现这一点,但是每个用户每次都有一个单独的ul <?php // Display user list $currentLetter = ''; fo
Andrew Smith
Alex Williams
Sam White
Stuart Smith
我期待着使用创建一个列表,看起来像这样
A
Andrew Smith
Alex Williams
S
Sam White
Stuart Smith
我尝试使用一个PHP Foreach循环和一个break来实现这一点,但是每个用户每次都有一个单独的ul
<?php
// Display user list
$currentLetter = '';
foreach($pupils['user'] as $pupil)
{
$firstLetter = $pupil->profile_name[0];
if($firstLetter !== $currentLetter)
{
// If it's not the first loop, we close the list and block containers
if($currentLetter !== '')
{
?>
</ul>
</div>
<?php
}
?>
<!-- SHOW FIRST LETTER -->
<div class="ioslist-group-container">
<div class="ioslist-group-header">
<strong><?php echo $firstLetter; ?></strong>
</div><!-- /.ioslist-group-header -->
<ul>
<?php
}
?>
<li><?php echo $pupil->profile_name; ?></li>
<?php
}
// Close the last list and block containers
?>
</ul>
</div>
<?php
// End of display
我目前的代码如下:
<?php
$currentLetter = '';
foreach($pupils['user'] as $pupil)
{
$firstLetter = substr($pupil->profile_name, 0, 1);
?>
<?php
if ($firstLetter !== $currentLetter) { ?>
<!-- SHOW FIRST LETTER -->
<div class="ioslist-group-container">
<div class="ioslist-group-header">
<strong>
<?php echo $firstLetter; ?>
</strong>
</div><!-- /.ioslist-group-header -->
<?php $currentLetter = $firstLetter; ?>
<?php } ?>
<ul>
<li>
<a href="<?php echo base_url('admin/pupil/view'); ?>/<?php echo $pupil->user_id; ?>">
<?php echo $pupil->profile_name; ?>
</a>
</li>
</ul>
</div><!-- /.iostlist-group-container -->
<?php } // END FOREACH ?>
-
我的HTML输出如下:
<div id="pupil-list">
<!-- SHOW FIRST LETTER -->
<div class="ioslist-group-container">
<div class="ioslist-group-header">
<strong>
A </strong>
</div><!-- /.ioslist-group-header -->
<ul>
<li>
<a href="http://local.wld-hub/admin/pupil/view/10">
Adam Bragg </a>
</li>
</ul>
</div><!-- /.iostlist-group-container -->
<!-- SHOW FIRST LETTER -->
<div class="ioslist-group-container">
<div class="ioslist-group-header">
<strong>
S </strong>
</div><!-- /.ioslist-group-header -->
<ul>
<li>
<a href="http://local.wld-hub/admin/pupil/view/11204">
Sam acclaiminapp </a>
</li>
</ul>
</div><!-- /.iostlist-group-container -->
<ul>
<li>
<a href="http://local.wld-hub/admin/pupil/view/11194">
Sam NormalEmail </a>
</li>
</ul>
</div>
A
-
S
-
-
最后两个元素从“学生列表”中分离出来
我哪里出了问题?我该如何纠正
干杯问题似乎是你没有在每个小组结束时正确地关闭你的元素;您可以检查
$firstLetter
是否为空,如果不是(空),则关闭
;最后也必须重复这一点
或者,构建一个中间结构:
$tmp = [];
foreach ($pupils['user'] as $pupil) {
$tmp[$pupil->profile_name[0]][] = $pupil->profile_name);
}
foreach ($tmp as $letter => $names) { ?>
<div class="ioslist-group-container">
<div class="ioslist-group-header">
<strong><?php echo $firstLetter; ?></strong>
</div>
<ul>
<?php foreach ($names as $name) { ?>
<li>
<a href="<?php echo base_url('admin/pupil/view') . '/' . $pupil->user_id; ?>"><?php echo $pupil->profile_name; ?></a>
</li>
<?php } ?>
</ul>
</div>
<?php }
$tmp=[];
foreach($schools['user']作为$school){
$tmp[$PRODUCT->profile\U name[0][]=$PRODUCT->profile\U name);
}
foreach($tmp作为$letter=>$names){?>
-
问题似乎是您没有正确地关闭每个组末尾的元素;您可以检查$firstLetter
是否为空,如果不是(空),则关闭
;这也必须在末尾重复
或者,构建一个中间结构:
$tmp = [];
foreach ($pupils['user'] as $pupil) {
$tmp[$pupil->profile_name[0]][] = $pupil->profile_name);
}
foreach ($tmp as $letter => $names) { ?>
<div class="ioslist-group-container">
<div class="ioslist-group-header">
<strong><?php echo $firstLetter; ?></strong>
</div>
<ul>
<?php foreach ($names as $name) { ?>
<li>
<a href="<?php echo base_url('admin/pupil/view') . '/' . $pupil->user_id; ?>"><?php echo $pupil->profile_name; ?></a>
</li>
<?php } ?>
</ul>
</div>
<?php }
$tmp=[];
foreach($schools['user']作为$school){
$tmp[$PRODUCT->profile\U name[0][]=$PRODUCT->profile\U name);
}
foreach($tmp作为$letter=>$names){?>
-
我不喜欢在您的示例中混合使用PHP和HTML。我更喜欢使用所有PHP代码。您的基本想法是正确的,但会变成:
function showFirstLetter($letter)
{
echo '<div class="ioslist-group-header"><strong>'.$letter.'</strong></div>';
}
function showPupilName($pupil)
{
$url = base_url('admin/pupil/view').'/'.$pupil->user_id;
echo '<li><a href="'.$url.'">'.$pupil->profile_name.'</a></li>';
}
echo '<div class="ioslist-group-container">'; // start container
$currentLetter = '';
foreach($pupils['user'] as $pupil)
{
$firstLetter = substr($pupil->profile_name,0,1);
if ($firstLetter !== $currentLetter)
{
if ($currentLetter != '') echo '</ul>'; // end list
showFirstLetter($firstLetter);
$currentLetter = $firstLetter;
echo '<ul>'; // start list
}
showPupilName($pupil);
}
echo '</ul></div>'; // end last list and container
函数showFirstLetter($letter)
{
回音“”.$letter.”;
}
函数showPupilName($pullow)
{
$url=base_url('admin/pullow/view')./'.$pullow->user_id;
回音“”;
}
echo“”;//启动容器
$currentLetter='';
foreach($schools['user']作为$school)
{
$firstLetter=substr($pullow->profile_name,0,1);
如果($firstLetter!=$currentLetter)
{
if($currentLetter!='')echo'';//结束列表
showFirstLetter($firstLetter);
$currentLetter=$firstLetter;
echo“”;//开始列表
}
showPupilName($学生);
}
echo“
”;//结束最后一个列表和容器
这段代码中有很多地方可以改进,但是当你的代码更清晰时,你可以更快地看到错误。在这种情况下,ul
列表标记就是问题所在。我不喜欢在你的示例中混合使用PHP和HTML。我更喜欢使用所有PHP代码。你的基本想法是正确的,但它会变成:
function showFirstLetter($letter)
{
echo '<div class="ioslist-group-header"><strong>'.$letter.'</strong></div>';
}
function showPupilName($pupil)
{
$url = base_url('admin/pupil/view').'/'.$pupil->user_id;
echo '<li><a href="'.$url.'">'.$pupil->profile_name.'</a></li>';
}
echo '<div class="ioslist-group-container">'; // start container
$currentLetter = '';
foreach($pupils['user'] as $pupil)
{
$firstLetter = substr($pupil->profile_name,0,1);
if ($firstLetter !== $currentLetter)
{
if ($currentLetter != '') echo '</ul>'; // end list
showFirstLetter($firstLetter);
$currentLetter = $firstLetter;
echo '<ul>'; // start list
}
showPupilName($pupil);
}
echo '</ul></div>'; // end last list and container
函数showFirstLetter($letter)
{
回音“”.$letter.”;
}
函数showPupilName($pullow)
{
$url=base_url('admin/pullow/view')./'.$pullow->user_id;
回音“”;
}
echo“”;//启动容器
$currentLetter='';
foreach($schools['user']作为$school)
{
$firstLetter=substr($pullow->profile_name,0,1);
如果($firstLetter!=$currentLetter)
{
if($currentLetter!='')echo'';//结束列表
showFirstLetter($firstLetter);
$currentLetter=$firstLetter;
echo“”;//开始列表
}
showPupilName($学生);
}
echo“
”;//结束最后一个列表和容器
这段代码中有很多地方可以改进,但是当您的代码更清晰时,您可以更快地看到错误。在这种情况下,ul
列表标签就是问题所在。您每次只使用一个项目创建用户列表
<?php
// Display user list
$currentLetter = '';
foreach($pupils['user'] as $pupil)
{
$firstLetter = $pupil->profile_name[0];
if($firstLetter !== $currentLetter)
{
// If it's not the first loop, we close the list and block containers
if($currentLetter !== '')
{
?>
</ul>
</div>
<?php
}
?>
<!-- SHOW FIRST LETTER -->
<div class="ioslist-group-container">
<div class="ioslist-group-header">
<strong><?php echo $firstLetter; ?></strong>
</div><!-- /.ioslist-group-header -->
<ul>
<?php
}
?>
<li><?php echo $pupil->profile_name; ?></li>
<?php
}
// Close the last list and block containers
?>
</ul>
</div>
<?php
// End of display
您每次只使用一项创建用户列表
<?php
// Display user list
$currentLetter = '';
foreach($pupils['user'] as $pupil)
{
$firstLetter = $pupil->profile_name[0];
if($firstLetter !== $currentLetter)
{
// If it's not the first loop, we close the list and block containers
if($currentLetter !== '')
{
?>
</ul>
</div>
<?php
}
?>
<!-- SHOW FIRST LETTER -->
<div class="ioslist-group-container">
<div class="ioslist-group-header">
<strong><?php echo $firstLetter; ?></strong>
</div><!-- /.ioslist-group-header -->
<ul>
<?php
}
?>
<li><?php echo $pupil->profile_name; ?></li>
<?php
}
// Close the last list and block containers
?>
</ul>
</div>
<?php
// End of display
列表总是按第一个字母排序吗?嗨@Jack是的。数据来自API,我要求这样做,你有什么问题。有一些无效的HTML,因为你在foreach中打开关闭divs
,但结果对我来说很好,除了这个。嗨@lolka\u bolka我怀疑HTML无效是我的问题e、 我修改了我的答案以显示输出的HTML和屏幕快照列表是否总是按第一个字母排序?Hi@Jack是的。数据来自API,我要求这样做,你有什么问题。有一些无效的HTML,因为你在foreach中打开关闭divs
,但结果对我来说很好,除了Hi@lolka_bolka我怀疑HTML无效是我的问题,我修改了我的答案以显示输出的HTML和屏幕截图