Project Structure:
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = "183" alt = "\" width = "274" src = "http://www.bkjia.com/uploadfile/2013/0904/20130904095406958.png"/>
Start searching: Search for keywords here ("big" "this ")
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = "269" alt = "\" width = "640" src = "http://www.bkjia.com/uploadfile/2013/0904/20130904095406563.png"/>
Search Results: highlighted
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = "480" alt = "\" width = "565" src = "http://www.bkjia.com/uploadfile/2013/0904/20130904095406399.png"/>
Database structure required by the project:
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = "187" alt = "\" width = "640" src = "http://www.bkjia.com/uploadfile/2013/0904/20130904095406831.png"/>
Implementation Code:
Conn. php
- <? Php
- $ Conn = @ mysql_connect ("localhost", "root", "") or die ("Database Link error ");
- Mysql_select_db ("form", $ conn );
- Mysql_query ("set names 'gbk '");
- ?>
SearchAndDisplayWithColor. php
- <? Php
- Include 'conn. php ';
- ?>
- <Table width = 500 align = "center">
- <Form action = "" method = "get">
- <Tr>
- <Td> keyWord: <input type = "text" name = "keyWord"/>
- <Input type = "submit" value = "Search"/> </td>
- </Tr>
- </Form>
- </Table>
- <Tablewidth = 500 border = "0" align = "center" cellpadding = "5"
- Cellspacing = "1" bgcolor = "# add3ef">
- <? Php
- // Search is performed only when the keyword is not empty.
- If ($ _ GET ['keyword']) {
- // Use a space character to separate keywords
- $ 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 );
- While ($ row = mysql_fetch_array ($ query )){
- // Replace the keyword and highlight it.
- $ 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]);
- ?>
- <Tr bgcolor = "# eff3ff">
- <Td> title: <font color = "black"> <? = $ Row [title]?> </Font> User: <font color = "black"> <? = $ Row [user]?> </Font>
- <Div align = "right"> <a href = "preEdit. php? Id = <? = $ Row [id]?> "> Edit </a> & nbsp; | & nbsp; <
- Href = "delete. php? Id = <? = $ Row [id]?> "> Delete </a> </div>
- </Td>
- </Tr>
- <Tr bgColor = "# ffffff">
- <Td> content: <? = $ Row [content]?> </Td>
- </Tr>
- <Tr bgColor = "# ffffff">
- <Td>
- <Div align = "right"> posting date: <? = $ Row [lastdate]?> </Div>
- </Td>
- </Tr>
- <? Php}
- }
- ?>
- </Table>
Note: In this small program, one disadvantage is that you can only search for two keywords at the same time and separate them with spaces. If you only search for a keyword, for example, "big"
Garbled characters are displayed ...... ^ | _ | ^, The result of the following code:
- // Use a space character to separate keywords
- $ Key = explode ('', $ _ GET [keyWord]);
If you want to improve the performance, you need to make a judgment later.