1

I have a webpage which is grabbing information from the string $details and looking for 'displayname' through search and displaying the results. This is LDAP Active Directory querying.

I need "$search .= "NameTitlePhoneMobileEmailDept\n";" to only show once a search has been completed. In it's current state, it repeats this line for every result. How can i make this line only show the once, up the top?

    // 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);
5
  • 1
    PHP != jQuery. Commented Oct 28, 2016 at 5:07
  • Move the <table ... line to before the loop, and the </table> after the loop. Commented Oct 28, 2016 at 5:08
  • I need to only show while the loop is active. I don't want the text showing on the non-searched page. Commented Oct 28, 2016 at 5:09
  • then you could use a simple counter variable, ie. $show = 0;. If it is 0 you echo the line and increase to 1. If not 0 you don't echo. Commented Oct 28, 2016 at 5:13
  • That would be unnecessary, If the <table... row was above the foreach loop it would still only fire when if (isset($_GET['q'])) and instead only fire once. Also move the </table> below the closing bracket for the same reason. Commented Oct 28, 2016 at 5:28

1 Answer 1

1

To get the Functionality that you want, you may have to move the line: $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" outside of the loop (just before the loop begins). You only generate the dynamic rows of the Table within the Loop. The snippet below illustrates just how:

   <?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);
Sign up to request clarification or add additional context in comments.

3 Comments

How does this met the OP's to only show once a search has been completed.?
@Sean What OP wants is to show the Table header once. In his code, those were within the Loop. This just moves out that header-section out from the Loop to only make it display just once.
In you original version you had it outside the if (isset($_GET['q'])) so it would have been printed before a search was done. Your edit corrected that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.