/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Highlighting a Search Result that searches multiple fields

I have a simple search engine that searches 30 fields of a table. I would like to have the word hightlighted in the search results.

This is what I have but nothing happens.

[code=php]<?php
$var = @$_GET[‘q’];
$trimmed = trim($var);
if ($trimmed == “”)
{
echo “<p>Please enter a search…</p>”;
exit;
}
?>
<p class=”head”>Results</p>
<table cellpadding=”5″ cellspacing=”0″ border=”1″ bordercolor=”#cccccc”>
<tr><td>&nbsp;</td>
<td class=”orange”>PO #</td>
<td class=”orange”>Date</td>
<td class=”orange”>Company</td>
<td class=”orange”>Ordered by</td>
<td class=”orange”>Ordered for</td>
<td class=”orange”>Vendor PO #</td>
<td class=”orange”>Ledger #</td>
<td class=”orange”>Invoice #</td>
<td class=”orange”>Order</td>
</tr>

<?php
$query = “SELECT * FROM forms_po WHERE id LIKE “%$trimmed%” OR po LIKE “%$trimmed%” OR month LIKE “%$trimmed%” OR day LIKE “%$trimmed%” OR year LIKE “%$trimmed%” OR vendor LIKE “%$trimmed%” OR orderby LIKE “%$trimmed%” OR orderfor LIKE “%$trimmed%” OR ledger LIKE “%$trimmed%” OR invoice LIKE “%$trimmed%” OR company LIKE “%$trimmed%” OR code1 LIKE “%$trimmed%” OR product1 LIKE “%$trimmed%” OR code2 LIKE “%$trimmed%” OR product2 LIKE “%$trimmed%” OR code3 LIKE “%$trimmed%” OR product3 LIKE “%$trimmed%” OR code4 LIKE “%$trimmed%” OR product4 LIKE “%$trimmed%” OR code5 LIKE “%$trimmed%” OR product5 LIKE “%$trimmed%” OR code6 LIKE “%$trimmed%” OR product6 LIKE “%$trimmed%” OR code7 LIKE “%$trimmed%” OR product7 LIKE “%$trimmed%” OR code8 LIKE “%$trimmed%” OR product8 LIKE “%$trimmed%” OR code9 LIKE “%$trimmed%” OR product9 LIKE “%$trimmed%” OR code10 LIKE “%$trimmed%” OR product10 LIKE “%$trimmed%” OR code11 LIKE “%$trimmed%” OR product11 LIKE “%$trimmed%” OR code12 LIKE “%$trimmed%” OR product12 LIKE “%$trimmed%” OR code13 LIKE “%$trimmed%” OR product13 LIKE “%$trimmed%” OR code14 LIKE “%$trimmed%” OR product14 LIKE “%$trimmed%” OR code15 LIKE “%$trimmed%” OR product15 LIKE “%$trimmed%” OR code16 LIKE “%$trimmed%” OR product16 LIKE “%$trimmed%” OR code17 LIKE “%$trimmed%” OR product17 LIKE “%$trimmed%” OR code18 LIKE “%$trimmed%” OR product18 LIKE “%$trimmed%” OR code19 LIKE “%$trimmed%” OR product19 LIKE “%$trimmed%” OR code20 LIKE “%$trimmed%” OR product20 LIKE “%$trimmed%””;
$result = mysql_query($query) or die(“Couldn’t execute query: “.mysql_error());

//search highlight
function highlight($result) {
$search = $_GET[‘q’];
$replace = “<span style=”color:808080″>$search</span>”;
$result = preg_replace($search, $replace, $result);
return $result;
}

while ($rows=mysql_fetch_array($result)) {
$id=$rows[‘id’];
$month=$rows[‘month’];
$day=$rows[‘day’];
$year=$rows[‘year’];
$vendorpo=$rows[‘vendorpo’];
$orderby=$rows[‘orderby’];
$orderfor=$rows[‘orderfor’];
$vendor=$rows[‘vendor’];
$ledger=$rows[‘ledger’];
$invoice=$rows[‘invoice’];
$company=$rows[‘company’];

$monthName = array(“01” => “Jan”, “02” => “Feb”, “03” => “Mar”, “04” => “Apr”, “05” => “May”, “06” => “Jun”, “07” => “Jul”, “08” => “Aug”, “09” => “Sep”, “10” => “Oct”, “11” => “Nov”, “12” => “Dec”);

echo ‘
<tr><td><a href=”po.php?id=’.$id.'”>view</a></td>
<td>’.$id.'</td>
<td>’.$monthName[$month] .’ ‘.$day.’, ‘.$year.'</td>
<td>’.$company.'</td>
<td>’.$orderby.'</td>
<td>’.$orderfor.'</td>
<td>’.$vendor.’&nbsp;</td>
<td>’.$ledger.’&nbsp;</td>
<td>’.$invoice.’&nbsp;</td>
<td>’;

$order=array(”.$rows[‘code1′].’ – ‘.$rows[‘product1′].’, ‘, ”.$rows[‘code2′].’ – ‘.$rows[‘product2′].’, ‘, ”.$rows[‘code3′].’ – ‘.$rows[‘product3′].’, ‘, ”.$rows[‘code4′].’ – ‘.$rows[‘product4′].’, ‘, ”.$rows[‘code5′].’ – ‘.$rows[‘product5′].’, ‘, ”.$rows[‘code6′].’ – ‘.$rows[‘product6′].’, ‘, ”.$rows[‘code7′].’ – ‘.$rows[‘product7′].’, ‘, ”.$rows[‘code8′].’ – ‘.$rows[‘product8′].’, ‘, ”.$rows[‘code9′].’ – ‘.$rows[‘product9′].’, ‘, ”.$rows[‘code10′].’ – ‘.$rows[‘product10′].’, ‘, ”.$rows[‘code11′].’ – ‘.$rows[‘product11′].’, ‘, ”.$rows[‘code12′].’ – ‘.$rows[‘product12′].’, ‘, ”.$rows[‘code13′].’ – ‘.$rows[‘product13′].’, ‘, ”.$rows[‘code14′].’ – ‘.$rows[‘product14′].’, ‘, ”.$rows[‘code15′].’ – ‘.$rows[‘product15′].’, ‘, ”.$rows[‘code16′].’ – ‘.$rows[‘product16′].’, ‘, ”.$rows[‘code17′].’ ‘.$rows[‘product17′].’, ‘, ”.$rows[‘code18′].’ – ‘.$rows[‘product18′].’, ‘, ”.$rows[‘code19′].’ – ‘.$rows[‘product19′].’, ‘, ”.$rows[‘code20′].’ – ‘.$rows[‘product20’]);
$o=1;
$order[$o];
foreach ($order as $value2) {
echo $value2;
$o++;
}
echo ‘</td></tr>’;
}
mysql_close();
?>
</table>[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@beylahauthorApr 19.2012 — OK I found my mistake, I have to do the replace for each field not in general as I did above using the $result.

[code=php]
$search = $_GET["q"];
while ($rows= mysql_fetch_array($result2)) {
$id = preg_replace("/".$search."/", "<span style="background-color:#ffff00">".$search."</span>",$rows['id']);
}
[/code]

etc for every field.
×

Success!

Help @beylah spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...