PHP只在循环中显示一次文本

时间:2016-10-28 05:04:44

标签: php string loops

我有一个网页,它从字符串$ details中获取信息,并通过搜索和显示结果查找“displayname”。这是LDAP Active Directory查询。

我需要“$ search。=”NameTitlePhoneMobileEmailDept \ n“;”仅在搜索完成后显示。在它的当前状态下,它会为每个结果重复此行。我怎样才能使这条线只显示一次,顶部?

    // Address Book Search
    $search .= "<div class='border'>\n";
    $search .= '<form class="search-form-wrapper" method="GET">
                    <input type="text" name="q" id="search" placeholder="Search for Staff..." required>
                    <input type="submit" value="go" id="submit">
                </form><br><br>';


    if (count($staff)) {
    if (isset($_GET['q'])) {
        $query = rawurlencode( strip_tags($_GET['q']));
        $query = ucfirst($query);
        foreach ($staff as $key => $details) {
            if(substr_count($details['displayname'], $query)){
            $search .= "<table class='address_book'><tr><th>Name</th><th>Title</th><th>Phone</th><th>Mobile</th><th>Email</th><th>Dept</th></tr>\n";
            $search .= "<tr><td>{$details['displayname']}</td>\n";
            $search .= "<td>{$details['title']}</td>\n";
            $search .= "<td>{$details['telephonenumber']}</td>\n";
            $search .= "<td>{$details['mobile']}</td>\n";
            $search .= "<td><a href='mailto:{$details['mail']}'>{$details['mail']}</a></td>\n";
            $search .= "<td>{$details['department']}</td>\n";
            $search .= "</tr>\n";
            $search .= "</table>\n";
            }
        }       
    }

    }

    $search .= "</div><br>";

$layout->content($search);

1 个答案:

答案 0 :(得分:1)

  

要获得所需的功能,您可能必须在循环之外移动行$search .= "<table class='address_book'><tr><th>Name</th><th>Title</th><th>Phone</th><th>Mobile</th><th>Email</th><th>Dept</th></tr>\n"(在循环开始之前)。您只在循环中生成表的动态行。下面的代码段说明了如何:

   <?php

        // Address Book Search
        $search  = "";
        $search .= "<div class='border'>\n";
        $search .= '<form class="search-form-wrapper" method="GET">
                        <input type="text" name="q" id="search" placeholder="Search for Staff..." required>
                        <input type="submit" value="go" id="submit">
                    </form><br><br>';


        if (count($staff)) {
            if (isset($_GET['q'])) {
                $search .= "<table class='address_book'><tr><th>Name</th><th>Title</th><th>Phone</th><th>Mobile</th><th>Email</th><th>Dept</th></tr>\n";
                $query   = rawurlencode( strip_tags($_GET['q']));
                $query   = ucfirst($query);
                foreach ($staff as $key => $details) {
                    if(substr_count($details['displayname'], $query)){
                        $search .= "<tr><td>{$details['displayname']}</td>\n";
                        $search .= "<td>{$details['title']}</td>\n";
                        $search .= "<td>{$details['telephonenumber']}</td>\n";
                        $search .= "<td>{$details['mobile']}</td>\n";
                        $search .= "<td><a href='mailto:{$details['mail']}'>{$details['mail']}</a></td>\n";
                        $search .= "<td>{$details['department']}</td>\n";
                        $search .= "</tr>\n";
                    }
                }
                $search .= "</table>\n";
            }   
        }

        $search .= "</div><br>";

        $layout->content($search);