3

How can I make BOLD just the query terms that users have typed in the search box?

I am referring to every single keywords in every single search results.

Like when you type a search terms, and the result page will give you all the results but the term that you have typed is in bold inside every single result.

As you can see, I do have already a code inside the page that suppose to make bold the terms but it does not work.

This is my complete code of my search page:

 <!DOCTYPE html>
<html lang="en">
<head>
<title>CLIHELP - Help for Command Line Interface</title>
<!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<meta charset="utf-8">
<meta name="description" content="Help for Command Line Interface">
<meta name="author" content="clihelp.org">
<style>
/*    Generated by http://www.cssportal.com    */
@import url("reset.css");
body {
    font-family: Calibri, Candara, Segoe, 'Segoe UI', Optima, Arial, sans-serif;
    font-size: 13px;
    color:#333
}
p {
    padding: 10px;
}
#wrapper {
    width: 1000px;
}
#navigation {
    float: left;
    height: 30px;
    width: 1000px;
    background: #F2F2F2;
}
#content {
    float: left;
    background: #FFFFFF;
    width: 600px;
    padding: 5px 20px 5px 20px;
    line-height: 18px;
}
#rightcolumn {
    width: 336px;
    float: left;
}
#footer {
    height: 30px;
    width: 1000px;
    background: #F2F2F2;
    clear: both;
}
.navmenu {
    background-color: #F2F2F2;
}
.navmenu ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    display: flex;
    align-items: center;
}
.navmenu li:last-child {
    margin-left: auto;
}
.navmenu ul li a {
    text-decoration: none;
    margin: 4px;
    padding: 5px 20px 5px 20px;
    color: #FFFFFF;
    background: #4285F4;
    display: inline-block;
}
.feedback-search {
    font-size: 13px;
 }

.feedback-search a {
    text-decoration: none;
}

.feedback-search a:hover {
    text-decoration: underline;
}
.feedback-search a:visited {
    color: blue;
}

.title {
    font-size: 18px;
}

.title a {
   text-decoration: none;
}

.title a:hover {
   text-decoration: underline;
}
.title a:visited {
   color: blue;
}

.tags {
   font-size: 13px;
   color: #006621;
 }
.script {       
   font-size: 13px;
}
ul.dashboard {
   list-style-type: square;
   line-height: 18px;
   list-style-position:inside;
}
.dashboard a {
   text-decoration: none;
}
.dashboard a:hover {
   text-decoration: underline;
}
.dashboard a:visited {
   color: blue;
}
p.dashboard {
    font-weight: bold;
}
.footermenu ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    display: flex;
    align-items: center;
}
.footermenu ul li a {
    text-decoration: none;
    margin: 4px;
    padding: 5px 20px 5px 20px;
    display: inline-block;
}
.your-search {
    font-weight: bold;
}
input:focus {
    background-color: yellow;
}
</style>
</head>
<body>
    <?php include_once("analyticstracking.php") ?>
    <div id="wrapper">
        <div id="navigation">
            <div class="navmenu">
        <ul id=menu>
            <li><a href="http://www.clihelp.org">Clihelp</a></li>
            <li><a href="https://github.com/clihelp/dev_clihelp">GitHub</a></li>
            <li><form action='q.php' method='GET'>
                <input type='text' size='25' name='search' autofocus>
                <input type='submit' style='position: absolute; left: -9999px' name='submit'/>
                    </form></li>
        </ul>
    </div>
        </div>
        <div id="content">
            <?php
        $button = $_GET ['submit'];
        $search = $_GET ['search'];

        if (!$button)
        echo "you didn't submit a keyword";
        else {
        if (strlen($search) <= 1)
        echo "Search term too short";
        else {

        echo "<p>Your search - <span class='your-search'>$search</span> ";

        mysql_connect("localhost", "username", "password");
            mysql_select_db("DB");
            $search_exploded = explode(" ", $search);
            foreach ($search_exploded as $search_each) {
             $x++;
                if ($x == 1)
                $construct .= "(CONCAT(code,cliCommandId) LIKE '%$search_each%' OR  os LIKE '%$search_each%' OR title LIKE '%$search_each%' OR tags LIKE '%$search_each%' OR script LIKE '%$search_each%') ";
            else
                $construct .= "AND (CONCAT(code,cliCommandId) LIKE '%$search_each%' OR  os LIKE '%$search_each%' OR title LIKE '%$search_each%' OR tags LIKE '%$search_each%' OR script LIKE '%$search_each%')";
            }
            $construct = "SELECT * FROM cliCommand WHERE $construct";
            $run = mysql_query($construct);
            $foundnum = mysql_num_rows($run);
            if ($foundnum == 0)
                echo "- did not match any documents.</br></br>Suggestions:</br></br>- Make sure all words are spelled correctly.</br>- Try different keywords.</br>- Try more general keywords.</br>- Search by id.";
            else {

                echo "- About $foundnum results - <span class='feedback-search'><a href=''>Give us Feedback about this result</a></span><br><br>";
            while ($runrows = mysql_fetch_assoc($run)) {
                $cliCommandId = $runrows ['cliCommandId'];
                $code = $runrows ['code'];
                $os = $runrows ['os'];
                $title = $runrows ['title'];
                $tags = $runrows ['tags'];
                $script = $runrows ['script'];
                if (strlen($script) > 160) {
                    $script = substr($script, 0, 160) . "..."; // do we need to add ...
                }
                $script = str_bold($script);
                echo "
        <div class='title'><a href='script.php?cliCommandId=$cliCommandId&code=$code'>$title</a></div>
        <span class='tags'>$tags</span><br>
        $script<br>
        <p>
        ";
                            }
                    }
                }
        }
        function str_bold($str, $text)
        {
            $search_exploded = explode(" ", $_GET ['search']);
            foreach ($search_exploded as $search_each) {
            $str = preg_replace("/$search_each/i", '<b>$0</b>', $str);
            }
            return $str;
        }
            ?>          
       </div>
        <div id="rightcolumn">
            <p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
        <!-- 336x280 Large Rectangle -->
        <ins class="adsbygoogle"
            style="display:inline-block;width:336px;height:280px"
            data-ad-client="-------"
            data-ad-slot="--------"></ins>
        <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
        </script></p>
        <p class="dashboard">Latest Linux updates: under construction</p>
        <ul class="dashboard">
            <li><a href="">Configure Your Existing AD Forest and Domain</a></li>
            <li><a href="">Install Other Roles and Features on Windows Server 2008 R2 Server Core</a></li>
            <li><a href="">Start the DNS service</a></li>
        </ul>
        <br>
        <p class="dashboard">Latest macOS updates: under construction</p>
        <ul class="dashboard">
            <li><a href="">Configure Your Existing AD Forest and Domain</a></li>
            <li><a href="">Install Other Roles and Features on Windows Server 2008 R2 Server Core</a></li>
            <li><a href="">Start the DNS service</a></li>
        </ul>
        <br>
        <p class="dashboard">Latest Windows updates: under construction</p>
        <ul class="dashboard">
            <li><a href="">Configure Your Existing AD Forest and Domain</a></li>
            <li><a href="">Install Other Roles and Features on Windows Server 2008 R2 Server Core</a></li>
            <li><a href="">Start the DNS service</a></li>
        </ul>
        <br>
        </div>
        <div id="footer">
            <div class="footermenu">
        <ul id=menu>
            <li><a href="#">About</a></li>
        </ul>
        </div>  
        </div>
    </div>
</body>
</html>
Fabio
  • 237
  • 2
  • 13
  • 1
    If this is new code, please don't use the `mysql_*` functions. They were deprecated in PHP 5.5, which is so old it no longer even receives security updates, and completely removed in PHP 7. Instead, use PDO or `mysqli_*`. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Nov 26 '16 at 23:54
  • Chris, thank you so much for your tips. I will change the code. Thanks again. Also, do you have any idea how to solve the above? Thanks a lot again. – Fabio Nov 26 '16 at 23:56

1 Answers1

1
function bold($result, $search) {
    return preg_replace("/$search/", "<strong>$search</strong>", $result);
}

while ($runrows = mysql_fetch_assoc($run)) {
    $cliCommandId = $runrows ['cliCommandId'];
    $code = $runrows ['code'];
    $os = $runrows ['os'];
    $title = $runrows ['title'];
    $tags = $runrows ['tags'];
    $script = $runrows ['script'];
    echo "
        <div class='title'><a href=''>".bold($title, $search)."</a></div>
        <div class='tags'>".bold($tags, $search)."</div>
        <div class='script'>".bold($script, $search)."</div><br>
        <p>";
}
Thielicious
  • 4,122
  • 2
  • 25
  • 35
  • Thielicious, thanks to take the time to answer. I am referring to every single keywords in every single search results. Like when you type a search terms, and the result page will give you all the results but the term that you have typed is bold in every single result. – Fabio Nov 27 '16 at 00:49
  • @Fabio Viola , sorry I misunderstood, try this Closure.. it finds the searched key in the result and makes it bold – Thielicious Nov 27 '16 at 01:22
  • Thielicious, thank you again to come back to the post. I did paste the code and when I execute the query a blank page comes out. Any idea? – Fabio Nov 27 '16 at 01:32
  • If you want I can repost again the code with your Closure? But I really copy and paste your code. – Fabio Nov 27 '16 at 01:34
  • Well, it should work because I tested it myself. What is your PHP version on server? Please copy again, maybe you copied already before I edited a typo – Thielicious Nov 27 '16 at 01:47
  • Also, place the Closure above the while loop, not after – Thielicious Nov 27 '16 at 02:00
  • Now the page with the results came back. But still no bold. The server use PHP 5.4. I just publish above the page with your code. – Fabio Nov 27 '16 at 02:27
  • Now that's a very old PHP version, I think it doesn't accept Closures. I made a workaround, try it now – Thielicious Nov 27 '16 at 02:35
  • I know it is a old PHP version. I need to update. I just changed with the above code you just posted and still the results comes ok but still no bold. I do not want to take a lot of your time Thielicious – Fabio Nov 27 '16 at 02:39
  • Hi Thielicious, how are you? I updated the code. But still issue. Above I posted my new code. – Fabio Nov 30 '16 at 00:03
  • Hi, sorry to say but it's tiring to see a wall of code. I gave you the feedback which actually works. I'd recommend you to follow this advice http://stackoverflow.com/help/mcve since I cannot reproduce your problem in my own case as things like your include php file I cannot see and your database I cannot look at. Best is to simplify your issue in a verfiable and minimal code (MCVE) so maybe that more people invest time to take a look into it. I'm sorry about it, I did what I can and it actually works under default circumstances. – Thielicious Nov 30 '16 at 19:32
  • 1
    Thank you so much Thielicious. have a nice day – Fabio Nov 30 '16 at 19:37