Thank you for takign the time...
I've made the webpage
http://www.paradeigmatolexikon.dk, whitch gives examples when you search a word.
What I would like is first of all the serchterms to be highlighted, when I search for more than one word. Second, whitch not so urgent, is the searched word or sentence to stay in the searchbox, when quoted "".
I just give you the whole code:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PARADEIGMATOLEXIKON</title>
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
<script language="javascript" type="text/javascript">
window.onload=function(){
if(document.getElementById&&document.
getElementsByTagName&&document.createElement){
var sfield=document.getElementsByTagName('form')[0].elements[0];
//if(!sfield){return};
//sfield.onfocus=function(){this.value=''};
//sfield.onblur=function(){
//if(!this.value){this.value='Søg og du skal finde!'};
//}
}
}
</script>
</head>
</html>
Code: Select all
<?php
// include MySQL-processing classes
require_once 'mysql.php';
try{
// connect to MySQL
$db=new MySQL(array('host'=>'www.paradeigmatolexikon.dk','user'=>'user','password'=>'password','database'=>'database'));
$searchterm=$db->escapeString($_GET['searchterm']);
$searchterm2=stripslashes($searchterm);
$db->query("set names utf8");
?>
Code: Select all
<body style="margin-top: 5px; margin-bottom: 5px">
<h1>PARADEIGMATOLEXIKON</h1>
<div class="maincontainer">
<form method="get" action="processform.php" class="style4">
<input type="text" name="searchterm" title="Søg og du skal finde!" value="<?php echo $searchterm2; ?>"
class="searchbox" />
<input type="submit" name="search" title="Search Now!
"value="Søg" class="searchbutton" />
</form>
</div>
</body>
Code: Select all
<?php
$result=$db->query("SELECT * FROM anabasis WHERE MATCH (kilde, graesk, dansk, engelsk) AGAINST ('$searchterm' IN BOOLEAN MODE) ORDER BY RAND()");
if (!$result->countRows()){
$result=$db->query("SELECT *
FROM `anabasis`
WHERE `Kilde` REGEXP '\[[:space:]]$searchterm\[ .,;:?]'
OR `Graesk` LIKE '% $searchterm%'
OR `Dansk` REGEXP '\[[:space:]]$searchterm\[ .,;:?]'
OR `Engelsk` REGEXP '\[[:space:]]$searchterm\[ .,;:?]' ORDER BY RAND()");
}
if (!$result->countRows()){
echo '<br><div class="maincontainer"><h2 style="margin-bottom: 1px; margin-top: 1px;">'.$searchterm2.' GAV INGEN RESULTATER. PRØV IGEN!</h2></div>';
}
elseif($result->countRows()==1){
function highlightWords($text, $words, $colors=null)
{
if(is_null($colors) || !is_array($colors))
{
$colors = array('yellow', 'pink', 'green');
}
$i = 0;
/*** the maximum key number ***/
$num_colors = max(array_keys($colors));
/*** loop of the array of words ***/
foreach ($words as $word)
{
/*** quote the text for regex ***/
$word = preg_quote($word);
/*** highlight the words ***/
$text = preg_replace("/\b($word)\b/i", '<span class="highlight_'.$colors[$i].'">\1</span>', $text);
if($i==$num_colors){ $i = 0; } else { $i++; }
}
/*** return the text ***/
return $text;
}
echo '<br><div class="maincontainer"><p><strong>DU FIK '.$result->countRows().' RESULTATER</p></strong>
<table style="width: 800px"><tr>
<td style="width: 72px" class="rowcontainer"><h3>KILDE</td>
<td style="width: 200px" class="rowcontainer"><h3>GRÆSK</td>
<td style="width: 200px" class="rowcontainer"><h3>DANSK</td>
<td style="width: 200px" class="rowcontainer"><h3>ENGELSK</td>
</tr>';
while($row=$result->fetchRow()){
$words= array(preg_replace('/["]/','',$searchterm2));
$kilde = $row['kilde'];
$kilde = highlightWords($kilde, $words);
$graesk = $row['graesk'];
$graesk = highlightWords($graesk, $words);
$dansk = $row['dansk'];
$dansk = highlightWords($dansk, $words);
$engelsk = $row['engelsk'];
$engelsk = highlightWords($engelsk, $words);
echo '
<table style="width: 800px"><tr>
<td style="width: 80px" valign="top" class="rowcontainer2"><p>'.$kilde.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$graesk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$dansk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$engelsk.'</p></td>
</tr>';
}
}
else{
function highlightWords($text, $words, $colors=null)
{
if(is_null($colors) || !is_array($colors))
{
$colors = array('yellow', 'pink', 'green');
}
$i = 0;
/*** the maximum key number ***/
$num_colors = max(array_keys($colors));
/*** loop of the array of words ***/
foreach ($words as $word)
{
/*** quote the text for regex ***/
$word = preg_quote($word);
/*** highlight the words ***/
$text = preg_replace("/\b($word)\b/i", '<span class="highlight_'.$colors[$i].'">\1</span>', $text);
if($i==$num_colors){ $i = 0; } else { $i++; }
}
/*** return the text ***/
return $text;
}
echo '<br><div class="maincontainer"><p><strong>DU FIK '.$result->countRows().' RESULTATER</p></strong>
<table style="width: 800px"><tr>
<td style="width: 72px" class="rowcontainer"><h3>KILDE</td>
<td style="width: 200px" class="rowcontainer"><h3>GRÆSK</td>
<td style="width: 200px" class="rowcontainer"><h3>DANSK</td>
<td style="width: 200px" class="rowcontainer"><h3>ENGELSK</td>
</tr>';
while($row=$result->fetchRow()){
$words = array(preg_replace('/["]/','',$searchterm2));
$kilde = $row['kilde'];
$kilde = highlightWords($kilde, $words);
$graesk = $row['graesk'];
$graesk = highlightWords($graesk, $words);
$dansk = $row['dansk'];
$dansk = highlightWords($dansk, $words);
$engelsk = $row['engelsk'];
$engelsk = highlightWords($engelsk, $words);
echo '
<table style="width: 800px"><tr>
<td style="width: 80px" valign="top" class="rowcontainer2"><p>'.$kilde.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$graesk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$dansk.'</p></td>
<td style="width: 200px" valign="top" class="rowcontainer2"><p>'.$engelsk.'</p></td>
</tr>';
}
}
echo '</div></table>';
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
?>