Highlight search term in mysql php search

前端 未结 3 448
借酒劲吻你
借酒劲吻你 2020-12-18 16:04

Could someone please help me to highlight the searchterm in my php search code? Below is the code that I am currently using, and it works fine. Would just like to add a high

相关标签:
3条回答
  • 2020-12-18 16:13

    The simplest solution is to use str_replace() to replace the search term with <span> tags wrapped around them, styled.

    Warning: The way you have your script set up, you're vulnerable to injection attacks. This is just an example to show you how to pass in variables.

    See: How can I prevent SQL injection in 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);
    
    ?>
    

    And some sample styling:

    <style type="text/css">
    .highlight { background-color: yellow; }
    </style>
    
    0 讨论(0)
  • 2020-12-18 16:24

    i dont see where you are printing the searchterm on your page. also, I would use css style sheets, avoid font tags, for example

     <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
    
    0 讨论(0)
  • 2020-12-18 16:34

    You will want to use a regex (preg_replace) to search for your term and replace it with said term surrounded with <span> </span>.

    Look at the documentation of preg_replace for how to use it: http://us3.php.net/preg_replace

    0 讨论(0)
提交回复
热议问题