Project structure:
Start Search: Search for keywords here ("big" "This")
Search results: Highlighting
Database structure required for the project:
================================================================
Implementation code:
================================================================
conn.php
1 <?php2 $conn = @ mysql_connect ("localhost", "root", "") or Die ("Database link Error"), 3 mysql_select_db ("form", $conn); 4 mysql_qu Ery ("Set names ' GBK '"); 5?>
searchanddisplaywithcolor.php
1 <?php 2 include ' conn.php '; 3?> 4 5 <table width=500 align= "center" > 6 <form action= "" method= "get" > 7 <tr> 8 & lt;td> keyword: <input type= "text" name= "KeyWord"/> 9 <input type= "Submit" value= "Search"/></td>10 </tr>11 </form>12 </table>13 <table width=500 border= "0" align= "center" cellpadding= "5" 15 cellspacing= "1" bgcolor= "#add3ef" >16 <?php17//keyword is not empty when the relevant search is performed ($_get[' KeyWord ']) {19//with space characters Key word split open $key =explode ("', $_get[keyword]); $sql =" SELECT * from message where title like ' $key [0] ' or title like ' $key [1] ' or content like ' $key [0] ' or content like '% $key [1]% '], $query =mysql_query ($sql), and while ($row =mysql_ Fetch_array ($query)) {24//replace keyword and highlight keywords $row [Title]=preg_replace ("/$key [0]/i", "<font Color=red> ;<b> $key [0]</b></font> ", $row [title]), $row [Title]=preg_replace ("/$key [0]/i "," <fOnt color=red><b> $key [1]</b></font> ", $row [title]), $row [Content]=preg_replace ("/$key [0]/i "," <font color=red><b> $key [0]</b></font> ", $row [content]; $row [Content]=preg_replace (" /$key [1]/i], "<font color=red><b> $key [1]</b></font>", $row [content]; >30 < ; TR bgcolor= "#eff3ff" >32 <td> title: <font color= "Black" ><?= $row [title]?></font> Users: <font Color= "BLACK" ><?= $row [user]? ></font>33 <div align= "right" ><a href= "preedit.php?id=<?= $row [id]?> "> Edit </a> | <a34 href=" delete.php?id=<?= $row [ id]?> "> Delete </a></div>35 </td>36 </tr>37 <tr bgcolor=" #ffffff ">38 <td> content: <?= $row [content]?></td>39 </tr>40 <tr bgcolor= "#ffffff" >41 <td> ; <div Align= "Right" > Date Published: <?= $row [lastdate]?></div>43 </td>44 </tr>45 <?php}46}47 ? >48 </table>
Note: In this small program, a little bit of a disadvantage is that only two keywords can be searched at the same time, and the middle with a space "" separated, if you just search a keyword, such as: "Big"
The display will appear garbled ... ^|_|^, this is due to the result of the following code:
1//Use a space character to split the key word 2 $key =explode (', $_get[keyword]);
If we want to improve, we should make a judgment in the back of this place.
PHP Development _ Multiple keywords, highlighting