supanoob Posted March 4, 2008 Share Posted March 4, 2008 Basically i have a forum and i am going to make it so people can search for lines of text, i was wonder 3 things: 1) How would i allow them to search certain Database fields, using that phase? 2) How would i limit the result to show the first 50 WORDS of that post? 3) Is it possible to highlight the searched phase? or would that be done in JS? Thanks in advance... Quote Link to comment https://forums.phpfreaks.com/topic/94257-3-bits-of-help-please/ Share on other sites More sharing options...
bbaker Posted March 4, 2008 Share Posted March 4, 2008 1) SELECT * FROM table WHERE fieldname LIKE '%$searchphrase%' 2) echo substr($string, 0, 50); 3) Can be done in PHP too. $string= str_replace($searchphrase,'<span style="background-color: #ffff00">' . $searchphrase . '</span>',$string); These are pretty basic ways to achieve this. They are more advanced ways. Quote Link to comment https://forums.phpfreaks.com/topic/94257-3-bits-of-help-please/#findComment-482854 Share on other sites More sharing options...
Psycho Posted March 4, 2008 Share Posted March 4, 2008 1) How would i allow them to search certain Database fields, using that phase? If you want to the user to select the fields to be searched just create a form field(s) for selecting the fields: multi-select list, checkboxes, whatever. If multiple fields are being searched you would just need to use an OR clause in your query "field1 LIKE '%searchvalue%' or field2 LIKE '%searchvalue%'", etc. 2) How would i limit the result to show the first 50 WORDS of that post? $words = preg_split('%[\s,]+%', $string); $first_fifty = implode(' ', array_slice($words, 0, 50)); 3) Is it possible to highlight the searched phase? or would that be done in JS? $search = 'text to be highlighted'; $replace = '<span style="background-color:yellow;>'.$search.'</span>'; $output = str_replace($search, $replace, $input); Quote Link to comment https://forums.phpfreaks.com/topic/94257-3-bits-of-help-please/#findComment-482856 Share on other sites More sharing options...
supanoob Posted March 4, 2008 Author Share Posted March 4, 2008 Thanks for the swift replies guys, i will give those a go Quote Link to comment https://forums.phpfreaks.com/topic/94257-3-bits-of-help-please/#findComment-483225 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.