I've basically created a search engine for my website and it seems to work and give results.
However i get this error message:
Notice: Undefined variable: i on line 39
How can I solve this error?
Here is the line in question:
foreach ($terms as $each){
$i++;
The i++ bit is giving the error.
Here is the code in question:
<?php include "storescripts/connect_to_mysql.php"; ?>
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM products WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "details LIKE '%$each%' ";
else
$query .= "OR details LIKE '%$each%' ";
}
$query = mysqli_query($link, $query);
$numrows = mysqli_num_rows ($query);
if ($numrows > 0) {
while($row = mysqli_fetch_assoc($query)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
echo "<a href='http://localhost/web/product.php?id=$id'>$product_name</a><br />
<br />
$details<br /><br />
";
}
}
else
echo "No results found for \"<b>$k</b>\"";
?>
Simply initialize $i
before you use it :-)
$i = 0;
foreach ($terms as $each){
$i++;
// ...
}
But be intentional about the use of 0 vs. 1 as your initial value and where else you use that in the code.