GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Jan-2005, 09:57
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough

simple fake search


I'm currently working with a friend on a website..just doing the small things. One of the things he asked me to do was to create a search engine for the site. Now he currently wants to change the setup of his site (html docs to php cms) so I couldn't just use a meta tag search, so I decided to make one myself to use just for the meantime. It's a script that fakes searching the site.
Basically you specify the url, title, description and keywords in a multi-dimensional array.
The searched words are then compared to the entires in the array and returns the url, title and description of the found entries.

goto www.exzibit.co.za
to view the script in action.

Search for 'products' and 'home' to view the script retrieving more than one entry.
Heres da code:

PHP Code:

<?
// Lets make a "fake" search.
// If the searchstring is found in the multi-dimensional array then return
// the page url, title and description which is also stored in the multi-dimensional array
// The searchstring can be matched more than once.
// 2 stage script. 1st display form, 2nd display results

$searcharray = array(
               array('home.html','Home','The homepage, this page contact menu and cotact details','home','contact'),
               array('products.html','Products','All the products are shown in this page','products','caps','shirts','home'),
               array('guestbook.html','The guestbook','sign out guestbook or just surf the current entires','guestbook'),
               );

function displayform()
{
 echo '<form action="search.php" method="post">
 <input type="hidden" name="stage" value="2">
 <input type="text" name="searchstring">
 <input type="submit" value="search"></form>';
}

function searchresults($resultsarray,$word,$originalarray)
{
 print "Searched phrase: $word<br>";
 print "Search Results: (".(count($resultsarray)).") pages found.";
 print "<br><br>Pages:<br><br>";
 // display pages
 for ($counter=0;$counter<(count($resultsarray));$counter++)
 {
  $temp = $resultsarray[$counter];
  print "<a href=\"".$originalarray[$temp][0]."\">".$originalarray[$temp][1]."</a><br>Description: ".$originalarray[$temp][2]."<br><br>";
 }
}

function search($whicharray,$word)
{
 $numberarray = array();
 $tempcount = 0;
 foreach ($whicharray as $number => $data)
 {
  //print $number;
  foreach ($data as $value)
  {
   //print $value;
   if ((trim(strtolower($value)) == trim(strtolower($word))) && ($tempcount > 2))
       {
       array_push($numberarray, $number);
       }
   $tempcount ++;
  }
  $tempcount=0;
 }
 return $numberarray;
}

if (empty($stage)) $stage = 1;

switch ($stage)
{
 case 1:
 displayform();
 break;
 case 2:
 $results = (search($searcharray,$searchstring));
 //print $results[0];
 //print $searcharray[$results][0];
 if (!empty($results))
 {
  searchresults($results,$searchstring,$searcharray);
 }
 else print "No results found for ".$searchstring;
}
?>


So what ya think of my methods? Any comments, suggestions are most welcome. I just did this today there's still a lot of tweaking I gotta do..
Thanx in advance
Rich
  #2  
Old 15-Jan-2005, 04:57
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Must be a lot of work but it might work (if your intention is to fake it)!

One quick comment I have is for you to look into using the ARRAY functions, specifically the in_array() function in your example code above.
  #3  
Old 17-Jan-2005, 02:15
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
Well that's basically it. All I gotta do now is add more keywords and pages in the array. I guess I will be able to use this code for full text searching. Just gotta add the contents of the files or records searched into the array.

Quote:
ARRAY functions, specifically the in_array() function in your example code above.
There is always a way around in coding...I seem to always take the longer route. I have the worst textbook from which I teach myself...so there's a lot of functions I have no idea about. I created a full function myself whereas i'm sure in_array() would search the array for u and return a boolean? I'm getting there...thanx for the advice JdS
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
database search skyloon MySQL / PHP Forum 6 04-Jun-2006 12:09
Using meta tags help in ranking on some search engine pcx Search Engine Optimization Forum 8 29-Mar-2005 15:42
Search Engine Positioning 101 and 201 "How To" Tips... 000 Search Engine Optimization Forum 0 29-May-2003 10:34
[class] 404 search function code jrobbio MySQL / PHP Forum 6 22-Apr-2003 09:32
Generating simple URLs for search engines jrobbio MySQL / PHP Forum 1 17-Mar-2003 11:22

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.