Jump to content


Photo

Improving my search script


  • Please log in to reply
6 replies to this topic

#1 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 18 August 2006 - 11:29 PM

This piece of code searches the $info field of my delimited text file database:

foreach ($file as $line => $data)
{
    list($month, $day, $year, $info) = explode('|', trim($data) );
    if (stristr($info, $search))
    {
    echo $info;
    }
}
I have 3 Questions:

1) Lets say a person was searching for "John Smith" they could enter "John Smith" or "John" or "Smith" and it would work.  I would like to improve my search so that if a person searched for "John Smith" it would also find results for "John T. Smth" or " John Q. Smith" etc.  Is there an easy way to do this?

2)  How can I sort the $year field?

3)  For each record, the data in my $info is 2 or 3 sentences long.  When the current script returns a result, there is only 1 space between sentences even though there are 2 in my database.  Any suggestions on how I can fix this?

Thanks in advance

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 19 August 2006 - 01:33 AM

1) Lets say a person was searching for "John Smith" they could enter "John Smith" or "John" or "Smith" and it would work.  I would like to improve my search so that if a person searched for "John Smith" it would also find results for "John T. Smth" or " John Q. Smith" etc.  Is there an easy way to do this?


I would say to break the string apart and then do a search for both words and only return the results that have both in them, regarless of order or what's between them.

2)  How can I sort the $year field?


You have to create a multidimensional array, then sort it.  You can create a normal array, then use array_multisort, or you can create an array that has the year as the keys, and then sort the keys.  Just be careful with the latter because duplicate keys will over write eachother.

3)  For each record, the data in my $info is 2 or 3 sentences long.  When the current script returns a result, there is only 1 space between sentences even though there are 2 in my database.  Any suggestions on how I can fix this?


What do you mean it only returns one space but there are two in the db?  Please clarify.

#3 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 19 August 2006 - 03:03 PM

In my database, for a given record, the $info field might contain multiple sentences that have the standard 2 spaces after the period.  When that record is extracted and echoed, there is only one space after the period.  It does it in every case.  I thought the trim function was causing it, but it does it even when I remove it.

#4 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 19 August 2006 - 04:03 PM

Browsers will automatically remove multiple spaces and replace them with one single space. What you need to do is replace the two spaces with   (non-break space) if you want those two spaces to appear.

Browsers treat the non-break space as text even though you can't see it so you can do this:
echo $info.' ';
That way the browser will replace the two spaces with one single space and the non-break space will be tagged onto the end giving the illusion of two spaces.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#5 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 20 August 2006 - 03:35 AM

Ok but what if my $info contains 3 sentences?  Would this work after each one or just the last one>

#6 Jeremysr

Jeremysr
  • Members
  • PipPipPip
  • Advanced Member
  • 199 posts
  • LocationSaskatchewan, Canada

Posted 20 August 2006 - 03:45 AM

You could replace all ". "'s with ".  "

$info = str_replace(". ", ".  ", $info);


#7 biotech

biotech
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 08 December 2006 - 07:04 PM

did you make this to work,i mean the search thing..i need this badly too




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users