views:

264

answers:

2

how do i highlight search results from mysql query using php?

this is my [modified] code:

$search_result = "";

$search_result = $_GET["q"];

$result = mysql_query('SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes WHERE cQuotes LIKE "%' . $search_result .'%" ORDER BY idQuotes DESC', $conn)
  or die ('Error: '.mysql_error());


function h($s) {
    echo htmlspecialchars($s, ENT_QUOTES);
} 


?>

    <div class="caption">Search Results</div>
<div class="center_div">
<table>
    <?php while ($row= mysql_fetch_array($result)) { ?>
    <?php $cQuotes = preg_replace($search_result, "<div class='highlight'>".$search_result."</div>", $row['cQuotes']); ?>
        <tr>
            <td style="text-align:right; font-size:15px;"><?php h($row['cArabic']) ?></td>
            <td style="font-size:16px;"><?php h($cQuotes) ?></td>
            <td style="font-size:12px;"><?php h($row['vAuthor']) ?></td>
            <td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']) ?></td>
        </tr>
    <?php } ?>
</table>
</div>
+2  A: 

You can do that by using a JavaScript library called HiLite. It works pretty well.

Nate Zaugg
thanks for this, but i'd like to do it in php. any help appreciated.
fuz3d
+2  A: 

you could use preg_replace();, when it finds a match in your text you could place a div with a class of highlight around the matching word. You would then add a background color and border to the highlight class to make it stand out

preg_replace expect 3 parameters;

  1. the first one is what you are looking for
  2. second one is what should it be changed to
  3. The string of text he should search and replace from

so for example

<div class="center_div">
    <table>
    <caption>Search Results</caption>
    <?php while ($row= mysql_fetch_array($result)) { ?>
<?php $arabic = preg_replace("/".$search_result."/", "<div class='highlight'>".$search_result."</div>", h($row['cArabic'])); ?>
            <tr>
                <td style="text-align:right; font-size:15px;"><?php $arabic ?></td>
                <td style="font-size:16px;"><?php h($row['cQuotes']) ?></td>
                <td style="font-size:12px;"><?php h($row['vAuthor']) ?></td>
                <td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']) ?></td>
            </tr>
        <?php } ?>
    </table>
    </div>

I only did it for arabic but you might need to do that for cQuotes, vAuthor and vReference as well.

krike
thanks. i did that, but for quotes. `<?php $cQuotes = preg_replace($search_result, "<div class='highlight'>".$search_result."</div>", $row['cQuotes']); ?> <tr> <td style="text-align:right; font-size:15px;"><?php h($row['cArabic']) ?></td> <td style="font-size:16px;"><?php h($cQuotes) ?></td>` . .however, this is not showing the Quotes records at all [it's displaying arabic, reference and author].
fuz3d
do you mean that it's not dipslaying the right content where it should? that's weird. Can you change the code in your question to show what you currently have? this will be easier to see what's not right
krike
it's not displaying anything from the cQuotes record. modified the code.
fuz3d
I updated the code in my answer above, you need to add / before and after the first parameter of the preg_replace function. I saw that when I tested the code and I received that error, I guess you have error reporting turned off or something hiding it.
krike
after a few more modification, it displays the record now, but still doesn't highlight the searched keyword.. `<?php $cQuotes = preg_replace('/$search_result/' , '/<span class="highlight">$search_result<\/span>/', $row['cQuotes']); ?>`
fuz3d
did you style the class highlight in your css file?
krike
yes, i did. `.highlight {background-color: #FFE066;font-weight:bold;}`
fuz3d
I added an inline style and it worked, are you sure you have linked correctly to your stylesheet? user firebug to see if the div was added to the matched word and use the web developer addon for firefox to check if it can find the style
krike
checked thru firebug. the div doesn't add to the matched word although the style is there in the css. not sure what is wrong? could it be that the css of other elements in the page conflict with the css of this one?
fuz3d
no, if the div is not added it has nothing to do with the styles. maybe it's because of your function htmlspecialchars() it might alter html tags and therefore remove or alter the div with the class.maybe run the function before replacing, see the code in my answer above, I updated it again
krike
doesn't work. plus the Quotes field records display out of div.
fuz3d
I checked back your code in the comments, preg_replace('/$search_result/' , '/<span class="highlight">$search_result<\/span>/', $row['cQuotes']); ?> <- don't add single quotes, variables won't work between single quotes, use double quotes or concatenate like I did in the code I provided above.
krike
did that. .nothing. i'm assuming there's nothing wrong with the code itself, but there must be some conflict with other elements. .
fuz3d
i executed the code independently on a test page. it works. but i cannot understand why it isnt working on this page..
fuz3d
Send me a link or send me a piece of the code and I can check it out for you (my email is [email protected])
krike