Jump to content

need help in partial string search - advance for me probably a piece of cake


Recommended Posts


hi, guys, or should i say freaks  ;D
i need help though...i have tried to make a script for searching the books and its related data
i came up with something but it lacks one major feature, the user have to supply the exact name of the book, but that is just bad
you will agree i am sure. it doesnt have to be case sensitive but this is just what i need badly. i am not a php freak but want to become one in a time, since that happen i will have to bother you  ;D

here is the script i came up so far (it doesnt use sql yet, database is just simple two dimensional array):

[code]
<?php
  $books = array();
  $total_books=4;
  $m=0;  //number of matches

  //array 'title' 0,  'price us 1 and uk 2 ','ISBN' 3, 'author' 4, 'year' 5, discount 6, category 7,description 8 ,image 9 , link 10
  $titles=array(array('book title 1',10,10,'1-23456-789-0','author name','2006','55%','Religion','description','1-23456-789-0.jpg','http://www.somelink.com/','http://www.somelink2.com/'),
                array('book title 1',10,10,'1-23456-789-0','author name','2006','55%','Religion','description','1-23456-789-0.jpg','http://www.somelink.com/','http://www.somelink2.com/'),
                array('book title 1',10,10,'1-23456-789-0','author name','2006','55%','Religion','description','1-23456-789-0.jpg','http://www.somelink.com/','http://www.somelink2.com/'),
                array('book title 1',10,10,'1-23456-789-0','author name','2006','55%','Religion','description','1-23456-789-0.jpg','http://www.somelink.com/','http://www.somelink2.com/'));

$titleValue=$_GET['titleValue'];  //the title name passed from other *.php

//check is there any match
for($i=0;$i<$total_books;$i++){
//ignore case sensitive strings
        if(strcasecmp($titleValue,$titles[$i][0])==0) {
              //fill out our arrray with matches
             
$book_title=$titles[$i][0];
$book_price_us=$titles[$i][1];
$book_price_uk=$titles[$i][2];
$book_isbn=$titles[$i][3];
$book_author=$titles[$i][4];
$book_year=$titles[$i][5];
$book_discount=$titles[$i][6];
$book_category=$titles[$i][7];
$book_description=$titles[$i][8];
$book_image=$titles[$i][9];
$book_link=$titles[$i][10];
$book_link2=$titles[$i][11];

$books [] = array('title' => $book_title,
  'USprice' => $book_price_us,
  'UKprice' => $book_price_uk,
  'isbn' => $book_isbn,
  'author' => $book_author,
  'year' => $book_year,
  'discount' => $book_discount,
  'category' => $book_category,
  'description' => $book_description,
  'image' => $book_image,
  'link' => $book_link,
  'link2' => $book_link2);  
$m++;  //track number of matches
}
}


//this value will be pased back in order to see is there any matches
if($book_title==""){
$query_match='0';
}
else
$query_match='1';


//make xml to pass back
header('Content-Type: text/xml');
$dom=new DOMDocument();
$response=$dom->createElement('response');//root
$dom->appendChild($response);

$books_dom=$dom->createElement("books");

foreach( $books as $book )
  {
  $b = $dom->createElement( "book" );
 
  $title = $dom->createElement( 'title' );
  $title->appendChild(
  $dom->createTextNode( $book['title'] )
  );
  $b->appendChild( $title );
 
  $USprice = $dom->createElement( "USprice" );
  $USprice->appendChild(
  $dom->createTextNode( $book['USprice'] )
  );
  $b->appendChild( $USprice );
 
  $UKprice = $dom->createElement( "UKprice" );
  $UKprice->appendChild(
  $dom->createTextNode( $book['UKprice'] )
  );
  $b->appendChild( $UKprice );
 
  $isbn = $dom->createElement( "isbn" );
  $isbn->appendChild(
  $dom->createTextNode( $book['isbn'] )
  );
  $b->appendChild( $isbn );
 
  $author = $dom->createElement( "author" );
  $author->appendChild(
  $dom->createTextNode( $book['author'] )
  );
  $b->appendChild( $author );
 
  $year = $dom->createElement( "year" );
  $year->appendChild(
  $dom->createTextNode( $book['year'] )
  );
  $b->appendChild( $year );
 
  $discount = $dom->createElement( "discount" );
  $discount->appendChild(
  $dom->createTextNode( $book['discount'] )
  );
  $b->appendChild( $discount );
 
  $category = $dom->createElement( "category" );
  $category->appendChild(
  $dom->createTextNode( $book['category'] )
  );
  $b->appendChild( $category );
 
  $description = $dom->createElement( "description" );
  $description->appendChild(
  $dom->createTextNode( $book['description'] )
  );
  $b->appendChild( $description );
 
  $image = $dom->createElement( "image" );
  $image->appendChild(
  $dom->createTextNode( $book['image'] )
  );
  $b->appendChild( $image );
 
  $link = $dom->createElement( "link" );
  $link->appendChild(
  $dom->createTextNode( $book['link'] )
  );
  $b->appendChild( $link );
 
  $link2 = $dom->createElement( "link2" );
  $link2->appendChild(
  $dom->createTextNode( $book['link2'] )
  );
  $b->appendChild( $link2 );
 
  $books_dom->appendChild( $b );
  }
 
$response->appendChild($books_dom);
 
$match=$dom->createElement('match');
$matchText=$dom->createTextNode($query_match);
$match->appendChild($matchText);

$number_of_matches=$dom->createElement('number_of_matches');
$number_of_matches_text=$dom->createTextNode($m);
$number_of_matches->appendChild($number_of_matches_text);
 
$books_dom->appendChild($match);
$books_dom->appendChild($number_of_matches);

$xmlString=$dom->saveXML();
echo $xmlString;

?>
[/code]
i will look into databases when i make this to work first
i tried this approach but this doesnt work
plese help

[code]

$titleValue=$_GET['titleValue'];

for($i=0;$i<$total_books;$i++){
if(strcasecmp($titleValue,$titles[$i][0])==0) {
//this is the place we have to add partial match
if(stristr($titles[$i][0],$titleValue) === TRUE) {
$book_title=$titles[$i][0];
$book_price_us=$titles[$i][1];
$book_price_uk=$titles[$i][2];
$book_isbn=$titles[$i][3];
$book_author=$titles[$i][4];
$book_year=$titles[$i][5];
$book_discount=$titles[$i][6];
$book_category=$titles[$i][7];
$book_description=$titles[$i][8];
$book_image=$titles[$i][9];
$book_link=$titles[$i][10];
$book_link2=$titles[$i][11];

$books [] = array('title' => $book_title,
  'USprice' => $book_price_us,
  'UKprice' => $book_price_uk,
  'isbn' => $book_isbn,
  'author' => $book_author,
  'year' => $book_year,
  'discount' => $book_discount,
  'category' => $book_category,
  'description' => $book_description,
  'image' => $book_image,
  'link' => $book_link,
  'link2' => $book_link2);  
$m++;
}
}
}

[/code]
it doesn't work because you put it inside your strcasecmp condition.  the stristr will only be checked if your string passes your strcasecmp, making it a 100% match in the first place.  So it is indeed passing as true, but by the time you get to that condition, $a==$b already. 

The stristr should come before the strcasecmp.  But actually,  depending on what you are trying to accomplish, you should get rid of the strcasecmp altogether.  For instance, do you want your script to return a list of partial matched results, even if there is a 100% match? or do you want it to only return the 100% match and disregard the partial matches if it found a 100% match?

If you want it to just return everything, then get rid of the strcasecmp altogether.  If you want it to only return the 100% if found, then add the strcasecmp into your condition somewhere, where it checks if strcasecmp is true and disregards partial matches and keeps it if true.  Personally, I would have it keep all of them and then sort it by % when displaying it later, but that's just me.

But just so you know, if you put your info into a db you can run queries on it that would get rid of almost 100% of all of this code you are trying to do.  That's the whole point of databases.  For instance, if you had a table with 'title' 'author' etc.. columns all you would have to do is run this simple little query and it will return everything with $titleValue in the title.

query:
"select * from table where title like '%$titleValue%'"

And you can sort it by any way you want. Auther? Title? % returned? Just add a sortby to it, etc.. you say you want to 'look into it' as soon as you get this working. hey, more power to you, but you'd be saving yourself a lot of grief.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.