A search site script

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

brand_beatrice
Forum Newbie
Posts: 10
Joined: Tue Aug 30, 2005 5:43 am
Location: London
Contact:

A search site script

Post by brand_beatrice »

hi everybody,

i am looking for a good search script for my portal: http://www.bicestertown.co.uk
i have tried Google before but it doesn't work the way it should be.
Any idea is welcome.
Thank you?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Re: A search site script

Post by patrikG »

brand_beatrice wrote:hi everybody,

i am looking for a good search script for my portal: http://www.bicestertown.co.uk
i have tried Google before but it doesn't work the way it should be.
Any idea is welcome.
Thank you?
What's "the way it should be"? Please be more specific.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

brand_beatrice
Forum Newbie
Posts: 10
Joined: Tue Aug 30, 2005 5:43 am
Location: London
Contact:

Post by brand_beatrice »

As I can see this is the most discussed subject into this forum so far. Nobody has responded to this subject the way it should:

-free scripts, simple to setup without a MySQL necessity.

I am myself looking for a good search script. Haven’t found a good one so far. Don't want to see or hear about that Google search script. Is not good. If you come up with an idea or a script that you have used before please, make sure that is an easy to install one, simple, not MySQL(as 80% of us do not poses MySQL knowledge), free of advertisements and please, please, not Google.
Thank you.
PS
This is the portal where I try to implement that search box.
http://www.bicestertown.co.uk
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

like they said: look around for one on http://www.hotscripts.com
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

So I've been to hotscripts, and I see all these interfaces that you can download, but is there not a script you can write into a page?

I know you can write a SELECT * WHERE * LIKE * statement that searches a table in the database, which you can display on a second results page. I have pieces of this code, but nothing that has ever turned out results...

But I have seen it in action... and at one point in time, it worked... but I lost the results page...

Do any MySQL wizzes out there know about this?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

//get this string from form?
$search_terms = 'Some String Goes here';

//should allow user to match all terms of any terms
$match_all = true;

//put terms into array
$search_array = explode(' ',$search_terms);

//setup query
$sql = 'SELECT * FROM `table` WHERE ';

//Get number of elements searching for, for the for() clause
$count = $search_array;

//loop through elements
for ($x=0; $x <= $count; $x++) {
   //check if element is more than 2 characters
   //if this check is not performed likely you'll get unwanted results
   //because the 1-2 letter combination may come up frequently
   if (strlen($search_array[$x]) > 2) { 
      //have to control AND's or OR's because last element should have it added
      $sql .= '`column_that_your_searching_for` LIKE %'.$search_array[$x].'%'.($x == $count ? '',($match_all ? 'AND ', 'OR '));
   }
}

echo $sql;
Last edited by John Cartwright on Tue Sep 13, 2005 12:36 pm, edited 3 times in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

If you're using a MySQL database, it's quite easy, and can be done with just one function. Here it is roughly. Some of this is pseudo-code, so adapt it to your needs

Code: Select all

function do_search($search_string)
{
   create_connection_to_database()

   //this is the "SELECT * WHERE ..." part
   $query_string = make_query($search_string);

   //this does the query and returns the results
   $query_results = do_query($query_string);

   loop_through_each_result
   {
      output_each_row();
   }
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

so, to sum this up:

Assuming I have a button called "search", this is what the code should look like?

Code: Select all

<? 
	if($_POST['bpress']=="search")
	{
		function do_search($search_string)
		{
	   create_connection_to_database()
	
	   //this is the "SELECT * WHERE ..." part
	   $query_string = make_query($search_string);

	   //this does the query and returns the results
	   $query_results = do_query($query_string);

	   loop_through_each_result
			{
    	  output_each_row();
	  		}
		}
	} 
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

That's the jist of it, yes. However, the code I gave you isn't actual PHP code, it's just pseudo-code (more to convey the logic behind what you need to do, rather than working code). You'll have to change that to actual code before it'll work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

Howdy,

I'm going to rehash this topic once more, just to um... get results.

Code: Select all

<?
    if($_POST['bpress']=="search")
    {
        function do_search($search_string)
        {
       	
		//setup query
		$sql = 'SELECT * FROM articles WHERE Article_Active = 0 ';
		$numrecord = mysql_query($sql);
		$numrecords = mysql_fetch_array($numrecord);
		$result = mysql_query($sql) or die (mysql_error()); 
		$searchresults = mysql_fetch_array($result);		
       
		//search from where?
	   	$search_string = $searchresults("Article_Text");
	   
	   //get this string from form?
		$search_terms = $search_string;

		//should allow user to match all terms of any terms
		$match_all = true;
		
		//put terms into array
		$search_array = explode(' ',$search_terms);
		
        $totalpages = intval(($numrecords[0])/$intRecsPerPage);
            {
				if($x >= $numrecords[0])
					{
						break;
					}
						print($search_array);
        	}
 		}
	}
?>

Code: Select all

<!-- SITE SEARCH -->
				<form method="GET" action="http://www.advantageboard.com/includes/search.php">
					  <input class="text" type=text name=q size=20 maxlength=255 value="">
					  <input class="text" type="submit" name="search" value="Search">     	  
				</form>		
				<!-- END SiteSearch  -->
This is the address that displays in the address bar:

Code: Select all

http://www.advantageboard.com/includes/search.php?q=executives&search=Search
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

and the problem being?

Looking at your code I would think that $search_string is empty. Reason: $searchresults is an array (as you're using mysql_fetch_array), yet you treat it as a function call with one parameter "Article_Text".

In your code change

Code: Select all

//search from where?
           $search_string = $searchresults("Article_Text");
to

Code: Select all

//search from where?
           $search_string = $searchresults["Article_Text"];
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

sorry, the problem being that nothing returns... even when i changed the string to the word 'executive' and searched executive.... nothing displayed.

$search_string = $searchresults("Article_Text");
$search_string = $searchresults["Article_Text"];

still doesn't do it... would you suspect that it has to do with they way it is being output, and that it just isn't reading the results?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Try:

Code: Select all

<?php
        function do_search($search_string){
        if($_POST['bpress']=="search"){
//modified the query as you were doing a simple select. This query should do a search for $search_string
            $sql = 'SELECT Article_Text FROM articles WHERE Article_Active = 0 AND Article_Text LIKE %'.mysql_real_escape_string($search_string).'%';
            $result = mysql_query($sql) or die (mysql_error());
            $numRows = mysql_num_rows($result);
            $totalpages = integer($numRows/$intRecsPerPage);
            while($searchresults = mysql_fetch_array($result)){
                        print($searchresults);
            }
        }
?>
(untested)
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

hi again...

it returns a similar blank page.
the script is much neater looking though :)

Here's a question:

It's returning to the same page, should there be a seperate results page,
or like, a duplicated Search page that it could send to?

My limited understanding of Search scripts is that it usually passes the info to another page,
but I would have thought that that would just be for organization.
Post Reply