I want to make a link when you click on one of the search results, you go to that product's page.
To make a link to a product page, you need to use html a tag, just wrap it around your product name like this:
echo '<a href="index.php?id=' . $row["ProductID"] . '">' . $row["ProductName"]. "</a><br>";
The href
attribute contains your php file name (e.g. index.php) and a GET
parameter id
to send the product id to the php page.
Full example:
Assuming you want to extend your code to display a single product when a product id (ProductID
) is provided or all products otherwise.
This is a simple example how you could extend your code, look at the comments:
<?php
// take id from the request, otherwise set default to null
$productId = isset($_GET['id']) ? intval($_GET['id']) : null;
// when we have an id in the url, then we display a product page
if (!is_null($productId)) {
$sql = 'SELECT ProductID, ProductTags, ProductName FROM producttabel WHERE ProductID = ' . $productId;
$result = $mysqli->query($sql);
if ($result && $result->num_rows == 1) {
$row = $result->fetch_assoc();
$result->free(); // free result set
// output data of each row
echo '<div class="allepccskop">';
echo "Product page: #" . $productId . "<br>";
echo '</div>';
echo '<div class="allepccs">';
echo $row["ProductName"]. "<br>";
echo '</div>';
} else {
echo "Product not found";
}
// otherwise we show All products page
} else {
// your code
$sql = 'SELECT ProductID, ProductTags, ProductName FROM producttabel';
$result = $mysqli->query($sql);
if ($result && $result->num_rows > 0) {
// output data of each row
echo '<div class="allepccskop">';
echo "Onze producten: " . "<br>";
echo '</div>';
while ($row = $result->fetch_assoc()) {
echo '<div class="allepccs">';
echo '<a href="index.php?id=' . $row["ProductID"] . '">' . $row["ProductName"]. "</a><br>";
echo '</div>';
}
$result->free(); // free result set
} else {
echo "0 results";
}
}