简体   繁体   中英

Select names from a column in a table using PHP

I am trying to take the business types from a table that I have, on a previous page I used this code

$sql = "SELECT * FROM directorydata WHERE CompanyName LIKE :SearchTerm ORDER BY CompanyName ";

However this relies on a search term and I just want the list of types outputted using a foreach loop. There are several businesses where they have the same business type and I do not want this to be outputted twice.

This is my old foreach loop if that helps, although this should not change anything

if ($stmt->rowcount() > 0) {
    foreach($stmt as $row) {
        $ResultsCounter++;
        $htmlResult .=  "<div class='col-md-6 well' style='margin'>" .
                            "<h4>". $row['CompanyName'] . "</h4>" .
                            "<p><strong><a href='/" . $row['UrlAlias'] .".htm'>" . $phoneDirectory->formatPhoneNumber($row['Number1']) ."</a></strong></p>" .
                        "</div>"; 

        if ($ResultsCounter == 8){
            $htmlResult .=  "<div class=''><a class='collapse-btn' href='#'>See more results</a><div class='span4 collapse-group'><div class='collapse'>"; 
        }
    }

    if ($stmt->rowcount() >= 8) $htmlResult .=  "</div></div></div>"; 


} else{
        $htmlResult .= "<h1 class='text-center'>Sorry no search results found please search again</h1>";
}
}

Any help here?

use Group by type or distinct type

$sql = "SELECT * FROM directorydata
WHERE CompanyName LIKE :SearchTerm 
GROUP BY type
ORDER BY CompanyName ";

or DISTINCT

$sql = "SELECT DISTINCT type FROM directorydata
WHERE CompanyName LIKE :SearchTerm 
ORDER BY CompanyName ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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