Hi, I'm having a problem with a search engine I built with PHP to search my mySQL database. There is a form on top with a text field and a drop down. The text field is to enter the search query and the dropdown is to select the SQL-field in which the search should be commited. The textfield is named "q" (for query) and the dropdown is called "cat" (category of search).
This is my source code so far:
if(!session_id()){
session_start(); }
// Get the search variable from URL
$var = @$_GET['q'] ;
$ddown = @$_GET['cat'];
$trimmed = trim($var); //trim whitespace from the stored variable
// rows to return
$limit=10;
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<span class=\"style1\">Er is geen zoekterm opgegeven...</span><br>";
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<span class=\"style1\">Er is geen zoekterm opgegeven...</span><br>";
exit;
}
//connect to database
mysql_connect("localhost","zoek","pass"); //(host, username, password)
//specify database
mysql_select_db("database") or die("<span style=\"style1\">Unable to select database</span><br>"); //select which database we're using
// Build SQL Query
$query = "select * from Producten where \"$ddown\" like \"%$trimmed%\"
order by \"$ddown\"";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
if ($numrows == 0)
{
echo "<span class=\"style1\"><h3>Resultaten</h3></span>";
echo "<span class=\"style1\">Sorry, uw zoekopdracht: "" . $trimmed . "" leverde geen resultaten op</span><br>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("<span class=\"style1\">Kon de zoekopdracht niet uitvoeren, probeer het nogmaals</span><br>");
// display what the person searched for
echo "<span class=\"style1\">U zocht naar: "" . $var . ""</span>";
// begin to show results set
echo "Resultaten";
$count = 1 + $s ;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["\"$ddown\""];
echo "$count.) $title" ;
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "<span class=\"style1\"> <a href=\"$PHP_SELF?s=$prevs&q=$var&cat=$ddown\"><<
Vorige 10</a>  </span>";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo "<span class=\"style1\"> <a href=\"$PHP_SELF?s=$news&q=$var&cat=$ddown\">Volgende 10 >></a></span>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<span class=\"style1\">Resultaten $b tot $a van $numrows</span>";
I get these errors with ANY search, existing or not existing in the database.
- Notice: Undefined variable: PHP_SELF in c:\inetpub\wwwroot\TuningCenter\zoek.php on line 102
Line 102 is:
echo "<span class=\"style1\"> <a href=\"$PHP_SELF?s=$news&q=$var&cat=$ddown\">Volgende 10 >></a></span>";
- It shows the option to get to the next 10 results why 0 results are found 🙁