-Karl- Posted March 23, 2010 Share Posted March 23, 2010 Hello, I have a simple script I've coded: // Collect skills $skillsquery = "SELECT * FROM `skills` WHERE (`id` LIKE '%{$_GET['search']}%' OR `guidename` LIKE '%{$_GET['search']}%' OR `introduction` LIKE '%{$_GET['search']}%' OR `content` LIKE '%{$_GET['search']}%' OR `guide` LIKE '%{$_GET['search']}%')"; $runskills = mysql_query($skillsquery); if(!mysql_num_rows($runskills)==0){ if($runskills){ while($arr = mysql_fetch_assoc($runskills)){ if(mysql_num_rows($runskills) > 0){ echo <<<HTML <font color="#239dd9"><b>{$arr['**']}</b></font><br/> <font color="#a0a7ae">{$arr['**']}</font><br/> <a href="./***.php?id={$arr['id']}">***?id={$arr['id']}</a><br/><br/><br/> HTML; } } } } This will search for a keyword placed by a visitor and display any matches. However, if someone searches for two or more keywords it won't display an article if the two keywords are not next to each other in the field within the database. For example, if a field contains "hello world how are you?" and a visitor searches for "hello how" it won't display it, as it searches for %hello how%. This is how I intended it to be, however, it makes searching less accurate. I was wondering if there was a way to do this, by exploding the keywords that a visitor searches. Such as if they search for "hello how" it will search for a field which contains both "hello" AND "how". Hope I've explained this clear enough. Any help and pointers are appreciated Link to comment https://forums.phpfreaks.com/topic/196262-searching-databases/ Share on other sites More sharing options...
andrewgauger Posted March 23, 2010 Share Posted March 23, 2010 I've generally seen this done by parsing the string into separate searches and anding the results. If anyone knows of a more efficient way, please let me know too. Link to comment https://forums.phpfreaks.com/topic/196262-searching-databases/#findComment-1030660 Share on other sites More sharing options...
-Karl- Posted March 23, 2010 Author Share Posted March 23, 2010 Any ideas how I could do this? Would exploding work? If so how? Link to comment https://forums.phpfreaks.com/topic/196262-searching-databases/#findComment-1030685 Share on other sites More sharing options...
blirette Posted March 23, 2010 Share Posted March 23, 2010 Exploding should be the way. Explode the search string and construct with a 'for' or 'foreach' a serie of 'fieldName LIKE '%word1%' AND ...'. Well, you get the idea... Good luck! Link to comment https://forums.phpfreaks.com/topic/196262-searching-databases/#findComment-1030713 Share on other sites More sharing options...
-Karl- Posted March 23, 2010 Author Share Posted March 23, 2010 Alright thanks, I'll look in to it. I'm still learning so if you could give me any pointers I'd appreciate it. I haven't done a great deal with exploding, it just seemed like a logical solution Link to comment https://forums.phpfreaks.com/topic/196262-searching-databases/#findComment-1030715 Share on other sites More sharing options...
-Karl- Posted March 23, 2010 Author Share Posted March 23, 2010 Okay, I have the exploding done: $searchquery = $_GET['search']; $explode = explode(" ", $searchquery); echo $explode[0]; echo $explode[1]; However, how would I make this work in a MySQL query if there is 2 or more keywords to search for ($explode[0], $explode[1], $explode[2], etc)? How would I do a foreach? Link to comment https://forums.phpfreaks.com/topic/196262-searching-databases/#findComment-1030720 Share on other sites More sharing options...
-Karl- Posted March 23, 2010 Author Share Posted March 23, 2010 Foreach is now done: foreach ($explode as $exploded) { echo ($exploded."<br />"); } Now I'm completely stumped how I'd make it do the query per foreach. Any pointers? Link to comment https://forums.phpfreaks.com/topic/196262-searching-databases/#findComment-1030724 Share on other sites More sharing options...
-Karl- Posted March 23, 2010 Author Share Posted March 23, 2010 Okay so I have: foreach ($explode as $exploded) { // Collect skills $skillsquery = "SELECT * FROM `skills` WHERE (`id` LIKE '%{$exploded}%' OR `guidename` LIKE '%{$exploded}%' OR `introduction` LIKE '%{$exploded}%' OR `content` LIKE '%{$exploded}%' OR `guide` LIKE '%{$exploded}%')"; $runskills = mysql_query($skillsquery); if(!mysql_num_rows($runskills)==0){ if($runskills){ while($arr = mysql_fetch_assoc($runskills)){ if(mysql_num_rows($runskills) > 0){ echo <<<HTML <font color="#239dd9"><b>{$arr['guidename']}</b></font><br/> <font color="#a0a7ae">{$arr['introduction']}</font><br/> <a href="./***.php?id={$arr['id']}">***?id={$arr['id']}</a><br/><br/><br/> HTML; } } } } } But this echo's the same thing twice, if you search for "hello you" and both words are found in the same field, it will echo it twice. How could I combat this? Link to comment https://forums.phpfreaks.com/topic/196262-searching-databases/#findComment-1030727 Share on other sites More sharing options...
-Karl- Posted March 23, 2010 Author Share Posted March 23, 2010 Just in case someone else is looking how to do this, I'll post how I fixed it. // Collect skills foreach ($explode as $exploded) { $skillsquery = "SELECT * FROM `skills` WHERE (`id` LIKE '%{$exploded}%' OR `guidename` LIKE '%{$exploded}%' OR `introduction` LIKE '%{$exploded}%' OR `content` LIKE '%{$exploded}%' OR `guide` LIKE '%{$exploded}%')"; $runskills = mysql_query($skillsquery); } if(!mysql_num_rows($runskills)==0){ if($runskills){ while($arr = mysql_fetch_assoc($runskills)){ if(mysql_num_rows($runskills) > 0){ echo <<<HTML <font color="#239dd9"><b>{$arr['guidename']}</b></font><br/> <font color="#a0a7ae">{$arr['introduction']}</font><br/> <a href="./***.php?id={$arr['id']}">***?id={$arr['id']}</a><br/><br/><br/> HTML; } } } } Link to comment https://forums.phpfreaks.com/topic/196262-searching-databases/#findComment-1030728 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.