简体   繁体   English

如何以字母顺序列出带有标题的项目,如果找不到匹配项,则显示一条消息

[英]How to list items in alphabetical order with a heading and if no match is found display a message

My plan of action was: 我的行动计划是:

  1. Loop through an array of the alphabet 遍历字母数组
  2. While inside of that loop, print the current letter 在该循环内,打印当前字母
  3. While inside of that loop cycle through a list of city objects stored in an array. 在循环内部循环浏览存储在数组中的城市对象列表。
  4. Check and see if the first letter of the $city->city_name property matches the current letter in the loop. 检查并查看$ city-> city_name属性的首字母是否与循环中的当前字母匹配。 If it matches, print it. 如果匹配,请打印。
  5. If there are no matches, print a message saying so. 如果没有匹配项,请打印一条消息,说明是这样。

This is all I can get it to display: 这就是我可以显示的全部内容:

A 一种

There are no results to display. 没有要显示的结果。

B

There are no results to display. 没有要显示的结果。

C C

City

There are no results to display. 没有要显示的结果。

D d

There are no results to display. 没有要显示的结果。

E Ë

City

There are no results to display. 没有要显示的结果。

As you can see, the error displays even if there is a match. 如您所见,即使存在匹配项,也会显示错误。 I cannot seem to figure out where the flaw in my logic is and I've searched through every question on the topic. 我似乎无法弄清楚我的逻辑缺陷在哪里,并且已经搜索了有关该主题的每个问题。 Is there something obvious that I am missing? 有什么明显的我想念的东西吗?

Code: 码:

<?php
function printValues($cities, $county) 
{
    $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $letters = str_split($str);
    $lettersMatched = FALSE;

    foreach ($letters as $letter) 
    {
        echo "<h5 class=\"letter\">".$letter."</h5><hr>";

        foreach ($cities as $city) 
        {
            if(substr($city->city_name, 0, 1) == $letter) 
            {
                $lettersMatched = TRUE;
                $result = $city->city_name;
                echo "<p><a href=\"".site_url("/listings/county/".$county."/city/".$result)."\">".$result."</a></p>";
                $lettersMatched = FALSE;
            }
        } 
        if (!$lettersMatched) 
        {
            echo "<p>There are no results to display.</p>";
        }
    }
}
?>

You're resetting $lettersMatched to false inside the if statement. 您将在if语句中将$ lettersMatched重置为false。 Try this: 尝试这个:

<?php
function printValues($cities, $county) 
{
    $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $letters = str_split($str);
    //$lettersMatched = FALSE;

    foreach ($letters as $letter) 
    {
        echo "<h5 class=\"letter\">".$letter."</h5><hr>";
        $lettersMatched = FALSE;

        foreach ($cities as $city) 
        {
            if(substr($city->city_name, 0, 1) == $letter) 
            {
                $lettersMatched = TRUE;
                $result = $city->city_name;
                echo "<p><a href=\"".site_url("/listings/county/".$county."/city/".$result)."\">".$result."</a></p>";
                // $lettersMatched = FALSE;
            }
        } 
        if (!$lettersMatched) 
        {
            echo "<p>There are no results to display.</p>";
        }
    }
}
?>
$matches = false;
foreach(range('A', 'Z') AS $letter)
{
    echo $letter . '<hr>';

    foreach( $cities AS $city )
    {
        if( strtoupper(substr($city->city_name, 0, 1)) ===  $letter)
        {
            $matches = true;
            echo "<p>" . $city->city_name . "</p>";
        }
    }

    if(!$matches)
        echo "<p>No matches here</p>";

    $matches = false;

}

What you did was to set the matches to false right after you set it to true in the inner loop. 您要做的是在内部循环中将matchs设置为true之后立即将matchs设置为false。 So when your code reaches the condition outside the loop it will always be false . 因此,当您的代码达到循环外的条件时,它将始终false

What I did was to set the matches to false after the condition, so it will be checked again for each new letter. 我要做的是在条件之后将匹配项设置为false,因此将针对每个新字母再次检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM