Reputation: 105
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?
Upvotes: 0
Views: 44
Reputation: 6661
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 ";
Upvotes: 2