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
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
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
Share on other sites

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.