biotech Posted December 8, 2006 Share Posted December 8, 2006 hi, guys, or should i say freaks ;Di need help though...i have tried to make a script for searching the books and its related datai 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 badyou 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 ;Dhere 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] Quote Link to comment https://forums.phpfreaks.com/topic/29951-need-help-in-partial-string-search-advance-for-me-probably-a-piece-of-cake/ Share on other sites More sharing options...
.josh Posted December 8, 2006 Share Posted December 8, 2006 http://us3.php.net/manual/en/function.stristr.phpyou really should look into databases tho.. Quote Link to comment https://forums.phpfreaks.com/topic/29951-need-help-in-partial-string-search-advance-for-me-probably-a-piece-of-cake/#findComment-137605 Share on other sites More sharing options...
biotech Posted December 8, 2006 Author Share Posted December 8, 2006 i will look into databases when i make this to work firsti tried this approach but this doesnt workplese 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] Quote Link to comment https://forums.phpfreaks.com/topic/29951-need-help-in-partial-string-search-advance-for-me-probably-a-piece-of-cake/#findComment-137625 Share on other sites More sharing options...
.josh Posted December 8, 2006 Share Posted December 8, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/29951-need-help-in-partial-string-search-advance-for-me-probably-a-piece-of-cake/#findComment-137634 Share on other sites More sharing options...
biotech Posted December 8, 2006 Author Share Posted December 8, 2006 the point is that i dont have access to database yet but hopefully will get is soon, ok i will try you suggestion and get back to you.thanks man, for looking into it Quote Link to comment https://forums.phpfreaks.com/topic/29951-need-help-in-partial-string-search-advance-for-me-probably-a-piece-of-cake/#findComment-137661 Share on other sites More sharing options...
biotech Posted December 8, 2006 Author Share Posted December 8, 2006 it works like a charm, thanks again!greetings from belgrade Quote Link to comment https://forums.phpfreaks.com/topic/29951-need-help-in-partial-string-search-advance-for-me-probably-a-piece-of-cake/#findComment-137670 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.