Hopefully the last question as I am not 100% sure how to solve this one. I did see a similar question , but it does not really reflect my question and the other question is quite difficult to follow , so please see this as a original question and not a duplicate..
So on my website someone carried out a search from a search bar using the 'POST' method , teh search results show all whiskies in the databse. I have a number of whiskies with the same name but with different dates and prices. I would like it just to show one of each type that was searched for rather than all of them. I have attahced a clip of the databse. Really appreciate the help
Thanks
Index.php
</head>
<?php
$page='index';
include('header.php');
include('navbar.php');
?>
<script type="text/javascript">
function active(){
var search_bar= document.getElementById('search_bar');
if(search_bar.value == 'Search for your whisky here'){
search_bar.value=''
search_bar.placeholder= 'Search for your whisky here'
}
}
function inactive(){
var search_bar= document.getElementById('search_bar');
if(search_bar.value == ''){
search_bar.value='Search for your whisky here'
search_bar.placeholder= ''
}
}
</script>
<body>
<div class="third_bar">
<div class="background_image">
</div>
<div class="form"><form action= "search.php" method="post">
<input type="text" name="search" id="search_bar" placeholder="" value="Search for your whisky here" max length="30" autocomplete="off" onMouseDown="active();" onBlur="inactive();"/><input type="submit" id="search_button" value="Go!"/>
</form>
</div> </div>
</body>
</div>
<?php include ('footer.php');
?>
Search.php
<?php
$page='search';
include('header.php');
include ('navbar.php');
echo "<br>";
include ('connect.php');
if (isset ($_POST['search'])) { //the 'search' refers to the 'search' name=search on the index page and makes does something when the search is pushed.
$search = $_POST['search'];
$search = "%" . $search . "%"; // MySQL wildcard % either side of search to get partially matching results
// No wildcard if you want results to match fully
} else {
header ('location: index.php');
}
$stmt = $conn->prepare("SELECT * FROM test_db WHERE name LIKE :name ORDER BY name ASC"); // Use = instead of LIKE for full matching
$stmt->bindParam(':name', $search);
$stmt->execute();
$count = $stmt->rowCount(); // Added to count no. of results returned
if ($count >= 1) { // Only displays results if $count is 1 or more
echo "<div class='results_found'>";
echo $count;
echo " results found<br>";
echo "</div>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<div class='results'>";
echo "<div class='result_name'>";
echo "<b>Whisky Name:</b><br>";
echo "<a href='details1.php?id={$row['lot_id']}' >{$row['name']}</a>";
echo"<br>";
echo "</div>";
echo "</div>";
}
} else {
echo " Sorry no records were found";
}
?>
</htm