Php Search Engine

Discussion in 'PHP' started by le007, Aug 25, 2008.

  1. #1
    Hi all,

    I need to incorporate a basic search engine on a website - a simple textbox and search button. Whats the easiest way to do this? Just show results like google does - just a link and a one line description.

    I also need a similar search engine for a massive website im working on - its a complete phonebook directory with a textbox and someone can type in say something like 'painter' or whatever. Could be 10,000 links in it. its for my local community but i need to get it started and done soon.

    I was looking at spidher but i need to write something myself. Can someone please give me the steps needed eg:

    mysql db
    textbox
    etc

    Thank you:cool:
     
    le007, Aug 25, 2008 IP
  2. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    so u would be best to do a pagination script what this does is use mysql and count how many results there are in the database then share them by how many results you want per page to get how many pages it needs to create this is one i use for a few scripts i have done but u will have to modify it a little

    
    <?php
    		$var = @$_GET['q'] ;
    	$trimmed = (trim($var));
    	//   $var2 = @$_GET['Submit'] ;
    	// $trim   = (trim($var2));
      //\"$targetpage?page=$lpm1&s=$trimmed&Submit=$trim\"
      
    
    	$tbl_name="get";		//your table name
    	// How many adjacent pages should be shown on each side?
    	$adjacents = 1;
    	
    	/* 
    	   First get total number of rows in data table. 
    	   If you have a WHERE clause in your query, make sure you mirror it here.
    	*/
    	if(empty($var)){
    	$query = "SELECT COUNT(*) as num FROM $tbl_name";
    	}else{
    	$query = "SELECT COUNT(*) as num FROM $tbl_name where `name` like \"%$trimmed%\"";
    	}
    	$total_pages = mysql_fetch_array(mysql_query($query));
    	$total_pages = $total_pages[num];
    	
    	/* Setup vars for query. */
    	$targetpage = "index.php"; 	//your file name  (the name of this file)
    	$limit = 30; 								//how many items to show per page
    	$page = $_GET['page'];
    	if($page) 
    		$start = ($page - 1) * $limit; 			//first item to display on this page
    	else
    		$start = 0;								//if no page var is given, set start to 0
    	
    	/* Get data. */
    	if(strlen($var) <= 3 && isset($var) && strlen($var) != 0){
    	echo "SEARCH MUST BE 3 OR MORE";
    	$sql = "select * from $tbl_name LIMIT $start, $limit";	
    	}else{
    	$sql = "select * from $tbl_name where `name` like \"%$trimmed%\" LIMIT $start, $limit";	
    	}
    	
    	$result = mysql_query($sql);
    	$num_rows = mysql_num_rows($result); // showing amount on page
    
    	/* Setup page vars for display. */
    	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
    	$prev = $page - 1;							//previous page is page - 1
    	$next = $page + 1;	
    	$lastpage = ceil($total_pages/$limit);	
    	//lastpage is = total pages / items per page, rounded up.
    	$lpm1 = $lastpage - 1;						//last page minus 1
    	
    	/* 
    		Now we apply our rules and draw the pagination object. 
    		We're actually saving the code to a variable in case we want to draw it more than once.
    	*/
    
    
      
    	$pagination = "";
    	if($lastpage > 1)
    	{	
    		$pagination .= "<div id=\"pages\">";
    		//previous button
    		if ($page > 1) 
    			$pagination.= "<li><a href=\"$targetpage?page=$prev&q=$trimmed\">« previous - </a></li>";
    		else
    			$pagination.= "<li class=\"nolink\">« previous - </li>";	//DONE
    		
    		//pages	
    		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
    		{	
    			for ($counter = 1; $counter <= $lastpage; $counter++)
    			{
    				if ($counter == $page)
    					$pagination.= "<li class=\"nolink\">$counter - </li>";
    				else
    					$pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter - </a></a>";		//DONE			
    			}
    		}
    		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
    		{
    			//close to beginning; only hide later pages
    			if($page < 1 + ($adjacents * 2))		
    			{
    				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "<li class=\"current\">$counter - </li>";
    					else
    						$pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter</a></li>";					
    				}
    				$pagination.= "<li class=\"nolink\">...</li>";
    				$pagination.= "<li><a href=\"$targetpage?page=$lpm1&q=$trimmed&Submit=$trim\">$lpm1</a></li>";
    				$pagination.= "<li><a href=\"$targetpage?page=$lastpage&q=$trimmed&Submit=$trim\">$lastpage</a></li>";		
    			}
    			//in middle; hide some front and some back
    			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
    			{
    				$pagination.= "<li><a href=\"$targetpage?page=1&q=$trimmed&Submit=$trim\">1</a></li>";
    				$pagination.= "<li><a href=\"$targetpage?page=2&q=$trimmed&Submit=$trim\">2</a></li>";
    				$pagination.= "<li class=\"nolink\">...</li>";
    				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "<li class=\"current\">$counter - </li>";
    					else
    						$pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter</a></li>";					
    				}
    				$pagination.= "<li class=\"nolink\">...</li>";
    				$pagination.= "<li><a href=\"$targetpage?page=$lpm1&q=$trimmed&Submit=$trim\">$lpm1</a></li>";
    				$pagination.= "<li><a href=\"$targetpage?page=$lastpage&q=$trimmed&Submit=$trim\">$lastpage</a></li>";		
    			}
    			//close to end; only hide early pages
    			else
    			{
    				$pagination.= "<li><a href=\"$targetpage?page=1&q=$trimmed&Submit=$trim\">1</a></li>";
    				$pagination.= "<li><a href=\"$targetpage?page=2&q=$trimmed&Submit=$trim\">2</a></li>";
    				$pagination.= "<li class=\"nolink\">...</li>";
    				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "<li class=\"current\">$counter</li>";
    					else
    						$pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter</a></li>";					
    				}
    			}
    		}
    		
    		//next button
    		if ($page < $counter - 1) 
    			$pagination.= "<li><a href=\"$targetpage?page=$next&q=$trimmed&Submit=$trim\">next »</a></li>";
    		else
    			$pagination.= "<li class=\"nolink\">next »</li>";
    		$pagination.= "</div>\n";		
    	}
    ?>
    <br>
    <td width="530" valign="top">
        <div align="center">
    
          <center>
          <table bgcolor="#444444" border="0" cellpadding="3" cellspacing="1" width="100%">
            <tr>
              <td align="center" width="8%"><b>rate</b></td>
              <td align="center" width="74%"><b>Download Name</b></td>
              <td align="center" width="6%"><b>Hits</b></td>
              <td align="center" width="12%"><b>Search</b></td></tr>
    <?php
    	$c = 1;
    		while($row = mysql_fetch_array($result))
    		{
    		
    echo "<tr>
    <td bgcolor=\"#222222\" width=\"8%\"><div align=\"center\">".outOfFive($row['id'])."</div></td>
    <td bgcolor=\"#222222\" width=\"74%\"><div align=\"left\"><a href=\"leet.php?download=".$row['id']."\">".$row['name']."</a></div></td>
    			<td bgcolor=\"#222222\" width=\"6%\"><div align=\"center\">".$row['hits']."</div></td>
    			<td bgcolor=\"#222222\" width=\"12%\"><div align=\"center\"><a href=\"index.php?q=".$row['name']."\">Search</a></div></td>
    </tr>";
    }
    	?>
    
    PHP:
    echo pages like so

    <?= $pagination ?>
    PHP:
    hope this helps also if you are dealing with a lot a rows in your database make sure you do performance checks on your sql code as it has a big effect on your sorting/order by speed :)
    also i mite be able to help implementing stuff like this for a small fee i enjoy php and im still learning new stuff so i dont really charge alot :)
     
    cornetofreak, Aug 25, 2008 IP
  3. Panzer

    Panzer Active Member

    Messages:
    381
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Alternatively, you could always use google's custom search engine tool.
     
    Panzer, Aug 25, 2008 IP
  4. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    Thanks both of you for your help
     
    le007, Aug 26, 2008 IP
  5. knarffrank

    knarffrank Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Use the get_content("http://www.google.com"); script in php
     
    knarffrank, Aug 26, 2008 IP
  6. jdownloads

    jdownloads Active Member

    Messages:
    272
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #6
    whoa, whats this all about? how does it work? or should I just google it? ;)
     
    jdownloads, Aug 26, 2008 IP
  7. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    dunno what the get_content thing is?

    yeah google the google search for your own website. You can have a mini-search, you've probably seen them eg:

    Search digital point or search google.
     
    le007, Aug 28, 2008 IP
  8. ErectADirectory

    ErectADirectory Guest

    Messages:
    656
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Or look into a meta search engine for your 1st problem, as the biggest issue with phpdig or sphider is collecting and archiving your index. This takes months of spidering and gigabites of storage (not even mentioning bandwidth). With a meta engine you use api's to interact with the search engine to retrieve the results for your pages. Here's a free tip, when doing a meta engine it's easy to make a few bucks off of your site if you archive the results for bots (PM for examples).

    PHP is a terrible language for writing a full scale search engine. A spider, even on a dedicated server is only so fast ... and it's a big ass internet. The actual searching & retrieving of results needs to be handled multi-threaded and done with something like python or c for speed reasons when you have a massive index (think terabytes here).

    Your 2nd issue is easily fixed by just having a sql statement LIKE '%$keyword%' when the search is submitted (ex. plumber returns all records that have "plumber" in the title or description. You can store the results in an array or db table and do a custom algorithm to rank them if you want.

    If you need titles & description to display for these sites just scrape them directly and pull the title + description + 1st 200 words from the <body> using strip_tags().

    If you want to search their entire site and not just the homepage, you've probably going to find the answer with either google CSE or sphider. Which to use will depend on exactly how many sites you have have indexed. CSE is limited to the amount of domains you can search ... sphider is not but is slower and resource consuming.
     
    ErectADirectory, Aug 28, 2008 IP