Intereting Posts

Выделите поисковый запрос в mysql php search

Может кто-то, пожалуйста, помогите мне выделить поисковик в моем поисковом коде php? Ниже приведен код, который я использую в настоящее время, и он отлично работает. Просто хотел бы добавить функцию выделения, но не знаю, как реализовать это на этом коде, не переделывая все это.

Я наткнулся на выделение текста поиска в mysql php search thispost, который выглядит очень красиво. Но я потерял попытку реализовать это. Некоторое время назад у меня был эффект <span> , но он не мог попасть в <table> чтобы выделить только поисковик и все еще цикл через таблицу.

 include("config/config.php"); $con = mysql_connect($host, $db_user, $db_pass); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($db, $con); $result = mysql_query("SELECT * FROM data WHERE `data_id` LIKE '%$_POST[searchterm]%' OR `who` LIKE '%$_POST[searchterm]%' OR `ref` LIKE '%$_POST[searchterm]%' OR `asset` LIKE '%$_POST[searchterm]%' OR `make_model` LIKE '%$_POST[searchterm]%' OR `serial` LIKE '%$_POST[searchterm]%' OR `os` LIKE '%$_POST[searchterm]%' OR `swp` LIKE '%$_POST[searchterm]%' OR `ea` LIKE '%$_POST[searchterm]%' OR `dt_in` LIKE '%$_POST[searchterm]%' OR `status` LIKE '%$_POST[searchterm]%' OR `dt_out` LIKE '%$_POST[searchterm]%' "); $num_rows = mysql_num_rows($result); echo "<center>"; echo "<BR><BR>"; echo "<a href='index.php'><button id='sblogloginbtn' name='login' type='submit'><b>BACK</b></button></a>"; echo "<BR><BR>"; echo "<h1>Your search has found&nbsp;"; echo "<b><font size='15' color='blue'>$num_rows</font></b>"; echo "&nbsp;records.</font></h1>"; echo "<BR><BR>"; echo "<table border='frame'> <tr style='color:#FF00FF'> <th>Signed in By</th> <th>Reference Number</th> <th>Asset Number</th> <th>Make Model</th> <th>Serial Number</th> <th>Operating System</th> <th>Office</th> <th>Profile</th> <th>Extra Apps</th> <th>Time IN</th> <th>Status</th> <th>Time OUT</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['who'] . "</td>"; echo "<td>" . $row['ref'] . "</td>"; echo "<td>" . $row['asset'] . "</td>"; echo "<td>" . $row['make_model'] . "</td>"; echo "<td>" . $row['serial'] . "</td>"; echo "<td>" . $row['os'] . "</td>"; echo "<td>" . $row['office'] . "</td>"; echo "<td>" . $row['swp'] . "</td>"; echo "<td>" . $row['ea'] . "</td>"; echo "<td>" . $row['dt_in'] . "</td>"; echo "<td>" . $row['status'] . "</td>"; echo "<td>" . $row['dt_out'] . "</td>"; } echo "</table>"; echo "<br /><br />"; echo "</center>"; mysql_close($con); в include("config/config.php"); $con = mysql_connect($host, $db_user, $db_pass); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($db, $con); $result = mysql_query("SELECT * FROM data WHERE `data_id` LIKE '%$_POST[searchterm]%' OR `who` LIKE '%$_POST[searchterm]%' OR `ref` LIKE '%$_POST[searchterm]%' OR `asset` LIKE '%$_POST[searchterm]%' OR `make_model` LIKE '%$_POST[searchterm]%' OR `serial` LIKE '%$_POST[searchterm]%' OR `os` LIKE '%$_POST[searchterm]%' OR `swp` LIKE '%$_POST[searchterm]%' OR `ea` LIKE '%$_POST[searchterm]%' OR `dt_in` LIKE '%$_POST[searchterm]%' OR `status` LIKE '%$_POST[searchterm]%' OR `dt_out` LIKE '%$_POST[searchterm]%' "); $num_rows = mysql_num_rows($result); echo "<center>"; echo "<BR><BR>"; echo "<a href='index.php'><button id='sblogloginbtn' name='login' type='submit'><b>BACK</b></button></a>"; echo "<BR><BR>"; echo "<h1>Your search has found&nbsp;"; echo "<b><font size='15' color='blue'>$num_rows</font></b>"; echo "&nbsp;records.</font></h1>"; echo "<BR><BR>"; echo "<table border='frame'> <tr style='color:#FF00FF'> <th>Signed in By</th> <th>Reference Number</th> <th>Asset Number</th> <th>Make Model</th> <th>Serial Number</th> <th>Operating System</th> <th>Office</th> <th>Profile</th> <th>Extra Apps</th> <th>Time IN</th> <th>Status</th> <th>Time OUT</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['who'] . "</td>"; echo "<td>" . $row['ref'] . "</td>"; echo "<td>" . $row['asset'] . "</td>"; echo "<td>" . $row['make_model'] . "</td>"; echo "<td>" . $row['serial'] . "</td>"; echo "<td>" . $row['os'] . "</td>"; echo "<td>" . $row['office'] . "</td>"; echo "<td>" . $row['swp'] . "</td>"; echo "<td>" . $row['ea'] . "</td>"; echo "<td>" . $row['dt_in'] . "</td>"; echo "<td>" . $row['status'] . "</td>"; echo "<td>" . $row['dt_out'] . "</td>"; } echo "</table>"; echo "<br /><br />"; echo "</center>"; mysql_close($con); 

Самое простое решение – использовать str_replace() чтобы заменить поисковый str_replace() тегами <span> обернутыми вокруг них, в стиле.

Предупреждение. Как вы настроили свой скрипт, вы уязвимы для инъекционных атак. Это просто пример, показывающий вам, как передавать переменные.

См .: Как я могу предотвратить SQL-инъекцию в PHP?

 <?php include("config/config.php"); $con = mysql_connect($host, $db_user, $db_pass); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($db, $con); $term = $_POST[searchterm]; $result = mysql_query("SELECT * FROM data WHERE `data_id` LIKE '%$_POST[searchterm]%' OR `who` LIKE '%$_POST[searchterm]%' OR `ref` LIKE '%$_POST[searchterm]%' OR `asset` LIKE '%$_POST[searchterm]%' OR `make_model` LIKE '%$_POST[searchterm]%' OR `serial` LIKE '%$_POST[searchterm]%' OR `os` LIKE '%$_POST[searchterm]%' OR `swp` LIKE '%$_POST[searchterm]%' OR `ea` LIKE '%$_POST[searchterm]%' OR `dt_in` LIKE '%$_POST[searchterm]%' OR `status` LIKE '%$_POST[searchterm]%' OR `dt_out` LIKE '%$_POST[searchterm]%' "); $num_rows = mysql_num_rows($result); echo "<center>"; echo "<BR><BR>"; echo "<a href='index.php'><button id='sblogloginbtn' name='login' type='submit'><b>BACK</b></button></a>"; echo "<BR><BR>"; echo "<h1>Your search has found&nbsp;"; echo "<b><font size='15' color='blue'>$num_rows</font></b>"; echo "&nbsp;records.</font></h1>"; echo "<BR><BR>"; echo "<table border='frame'> <tr style='color:#FF00FF'> <th>Signed in By</th> <th>Reference Number</th> <th>Asset Number</th> <th>Make Model</th> <th>Serial Number</th> <th>Operating System</th> <th>Office</th> <th>Profile</th> <th>Extra Apps</th> <th>Time IN</th> <th>Status</th> <th>Time OUT</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['who']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['ref']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['asset']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['make_model']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['serial']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['os']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['office']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['swp']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['ea']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['dt_in']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['status']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['dt_out']) . "</td>"; } echo "</table>"; echo "<br /><br />"; echo "</center>"; mysql_close($con); ?> в <?php include("config/config.php"); $con = mysql_connect($host, $db_user, $db_pass); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($db, $con); $term = $_POST[searchterm]; $result = mysql_query("SELECT * FROM data WHERE `data_id` LIKE '%$_POST[searchterm]%' OR `who` LIKE '%$_POST[searchterm]%' OR `ref` LIKE '%$_POST[searchterm]%' OR `asset` LIKE '%$_POST[searchterm]%' OR `make_model` LIKE '%$_POST[searchterm]%' OR `serial` LIKE '%$_POST[searchterm]%' OR `os` LIKE '%$_POST[searchterm]%' OR `swp` LIKE '%$_POST[searchterm]%' OR `ea` LIKE '%$_POST[searchterm]%' OR `dt_in` LIKE '%$_POST[searchterm]%' OR `status` LIKE '%$_POST[searchterm]%' OR `dt_out` LIKE '%$_POST[searchterm]%' "); $num_rows = mysql_num_rows($result); echo "<center>"; echo "<BR><BR>"; echo "<a href='index.php'><button id='sblogloginbtn' name='login' type='submit'><b>BACK</b></button></a>"; echo "<BR><BR>"; echo "<h1>Your search has found&nbsp;"; echo "<b><font size='15' color='blue'>$num_rows</font></b>"; echo "&nbsp;records.</font></h1>"; echo "<BR><BR>"; echo "<table border='frame'> <tr style='color:#FF00FF'> <th>Signed in By</th> <th>Reference Number</th> <th>Asset Number</th> <th>Make Model</th> <th>Serial Number</th> <th>Operating System</th> <th>Office</th> <th>Profile</th> <th>Extra Apps</th> <th>Time IN</th> <th>Status</th> <th>Time OUT</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['who']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['ref']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['asset']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['make_model']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['serial']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['os']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['office']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['swp']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['ea']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['dt_in']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['status']) . "</td>"; echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['dt_out']) . "</td>"; } echo "</table>"; echo "<br /><br />"; echo "</center>"; mysql_close($con); ?> 

И некоторые образцы стилей:

 <style type="text/css"> .highlight { background-color: yellow; } </style> 

Я не вижу, где вы печатаете поисковик на своей странице. Кроме того, я бы использовал таблицы стилей CSS, избегая тегов шрифтов, например

  <style> .searchTerm{ background-color:red; } </style> <table> <tr><th>You searched for<div class='searchTerm'><?php echo $_POST[searchterm];?></div></th></tr> //rest of page 

Вы захотите использовать регулярное выражение ( preg_replace ) для поиска вашего термина и заменить его на указанный термин, окруженный <span> </span> .

Посмотрите документацию по preg_replace для ее использования: http://us3.php.net/preg_replace