Jump to content

3 bits of help please


supanoob

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/94257-3-bits-of-help-please/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/94257-3-bits-of-help-please/#findComment-482854
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/94257-3-bits-of-help-please/#findComment-482856
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.