PHP highlight search terms in MySQL -
i have search code:
<?php //members $sql="select * members "; $sql.="forename '%".$_get["s"]."%' or "; $sql.="surname '%".$_get["s"]."%' "; $rs=mysql_query($sql,$conn); if(mysql_num_rows($rs) > 0) { echo '<table width="100%" border="0" cellspacing="5" cellpadding="5"> <tr> <td colspan="3"><h3>members</h3></td> </tr> <tr> <td><strong>member name</strong></td> <td><strong>d.o.b</strong></td> <td><strong>age</strong></td> </tr>'; while($result=mysql_fetch_array($rs)) { //work out age of each member $sql2="select timestampdiff(year,concat(dob_year, '-', dob_month, '-', dob_day),curdate()) years, (timestampdiff(month,concat(dob_year, '-', dob_month, '-', dob_day),curdate()) - timestampdiff(year,concat(dob_year, '-', dob_month, '-', dob_day),curdate())*12) months members sequence = '".$result["sequence"]."' "; $rs2=mysql_query($sql2,$conn); $result2=mysql_fetch_array($rs2); //date in mm/dd/yyyy format; or can in other formats $birthdate = "12/17/1983"; //explode date month, day , year $birthdate = explode("/", $birthdate); //get age date or birthdate $age = (date("md", date("u", mktime(0, 0, 0, $birthdate[0], $birthdate[1], $birthdate[2]))) > date("md") ? ((date("y")-$birthdate[2])-1):(date("y")-$birthdate[2])); $forename = implode('<strong>'.$_get['s'].'</strong>',explode($_get['s'],$result['forename'])); $surname = str_replace($_get["s"], '<font color="#ff0000">'.$_get["s"]."</font>", $result['surname']); echo '<tr class="notfirst" style="cursor:pointer;" onclick="document.location=\'/members/edit_member.php?seq='.$result["sequence"].'\'"> <td> '.$forename.' '.$surname.'</td> <td>'.$result["dob_day"].' / '.$result["dob_month"].' / '.$result["dob_year"].'</td> <td>'.$result2["years"].' years / '.$result2["months"].' months</td> </tr>'; } echo '</table>'; } ?>
i want highlight search terms in results
as can see have tried using:
$forename = implode('<strong>'.$_get['s'].'</strong>',explode($_get['s'],$result['forename']));
and
$surname = str_replace($_get["s"], '<font color="#ff0000">'.$_get["s"]."</font>", $result['surname']);
but not working - whats best way achieve this. want work on both forename , surname results
i tried using code from: highlight search text in mysql php search
first fix sql injection problem then.
$search = mysql_real_escape_string($_get['s']); $text = str_replace($search,"<b>$search</b>",$textfrommysqlresult); echo $text;
Comments
Post a Comment