/////////////////////////////////////////////////////////////////////////////////////
//
// this is the code i use for my search engine,
// the first if statement checks to see if this process has been done before or not, 
// $stringy is only passed from the previous page with the cearch box on it, not from any 'back' or next links.
// the back and next links pass the $trimm variable
//
// so, if $trimm is empty, we must run $stringy through this code.
// if trim is NOT empty, we put $trimm through instead
//
// both variables are imported with the $malteasers variable
//
/////////////////////////////////////////////////////////////////////////////////////


if ($stringy == "") $malteasers = "$trimm";
else $malteasers = "$stringy";

$trimmed = trim($malteasers);


// this determines what characters can be used as a splitter, i.e 'space (%20)'
// this is so you can search for "chicken soup", "chicken" and "soup" all in one single query!
$trimmed_array = explode(" .%20.%2C.%26.%3F.%40",$trimmed); 



// this foreach call was cut from my code (my code is on a loop)
// so this may work straight away or may need some editing,
foreach ($trimmed_array as $trimm);



// look for a slash
if (preg_match ("/\"/i", "$trimm"))
{

// do something with a function i'm sure is wrong, i may have taken a long way round when i was less experienced.
// $trimm = eregi_replace("/\"","",$trimm); might have worked instead of all the minusers and nudgers being needed!

$minuser = "2";

$trimm = eregi_replace("\"","",$trimm);

$preparetoslice = strlen("$trimm");

$sliceoffset = ($preparetoslice - $minuser);

$nudger = "1";

$trimm = substr($trimm,$nudger,$sliceoffset);
}



$query = "select count(*) from my_links where keywords LIKE '%$trimm%' AND valid='yes'";
$result = mysql_query($query);
$count = number_format(mysql_result($result,0,"count(*)"));


//
// if you wan't an ACCURATE search, let your text box pass a variable
// called $stringy
//
// then replace the database code with your own, but, keep the  "where keywords LIKE '%$trimm%'" part!
//
// and that should be it, good luck with search!
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// Random link generation:
//

$query = "SELECT * from randylinks ORDER BY RAND(" . time() . " * " . time() . ") LIMIT 1";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
$rowrandy = mysql_fetch_array($result);


$linkname = $rowrandy["linkname"];
$linkurl = $rowrandy["linkurl"];
$randylink = "<a href=\"$linkurl\">$linkname</a>";

//
//
// just alter your database details to get this one working
// you need a db table that supplies a linkurl and a linktext
//
// optionally you can easilly add an image into it to create an image link
//
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// MAKE WML COMPAT
//
// this is my make_wml_compat function, it should work though i have had issues with the carriage returns!
//


function make_wml_compat($string)
	{
	$string = ltrim($string);
	$string = ereg_replace(chr(13),"<br/>",$string);
	$string = ereg_replace(chr(10),"<br/>",$string);
	$string = ereg_replace("&#xB6;","<br/>",$string);
	$string = ereg_replace("&#xB5;","&nbsp;",$string);
	$string = ereg_replace("===","<br/>",$string);
	$string = ereg_replace("\n","<br/>",$string);
	$string = ereg_replace("\r","<br/>",$string);
	$string = ereg_replace("&","&amp;",$string);
	$string = ereg_replace("\\$","&#36;",$string);
	$string = ereg_replace("\","&#163;",$string);
	$string = ereg_replace("<","&lt;",$string);
	$string = ereg_replace(">","&gt;",$string);
	$string = ereg_replace("","-1",$string);
	$string = ereg_replace("","-2",$string);
	$string = ereg_replace("","-3",$string);
	$string = ereg_replace("","-euro",$string);
	$string = ereg_replace("","&#169;",$string);
	$string = ereg_replace("","&#174;",$string);
	$string = ereg_replace("~club","&#9827;",$string);
	$string = ereg_replace("~spade","&#9824;",$string);
	$string = ereg_replace("~heart","&#9829;",$string);
	$string = ereg_replace("~diamond","&#9830;",$string);
	$string = ereg_replace("~1","&#185;",$string);
	$string = ereg_replace("~2","&#178;",$string);
	$string = ereg_replace("~3","&#179;",$string);
	$string = ereg_replace("~half","&#189;",$string);
	$string = ereg_replace("~quart","&#188;",$string);
	$string = ereg_replace("~euro","&#8364;",$string);
	$string = ereg_replace("~dot","&#8226;",$string);
	return $string;
	}
//
//
// this SHOULD work, however, different servers run different versions of php etc, so if you get an error on
// say, line 3452345, then find the symbol on that line and google php escape %symbol% 
// an escape is to stop php parsing it, look at the dollar sign up there,
// it's escaped with two back slashes. 
//
// you can use the above function like this: make_wml_compat("$text");
// it will do nothing if you don't call it.
//
// enjoy!
///////////////////////////////////////////////////////////////////////////////////////////////////