Jump to content

No results returned in search engine?


kristo5747

Recommended Posts

Greetings!

 

This n00b has a "search engine" dedicated to extracting relevant information from log files. It works by parsing preset fields in the log filename.

 

I'd like to have some notification when the search returns no rows as I am planning to add more search options. Here's my code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
        <title>LogFile Search</title>
        <style type="text/css">      <<irrelevant stuff removed>>        </style>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" language="javascript" src="js/UI.js"></script>
    </head>
    <body>
        <h3 id="myh3">Log File Search</h3><h5>Version 1.0.1</h5>
        <h4 id="myh4"><ul><li>         <<irrelevant stuff removed>>   </li></ul></h4>
        <form method="post" name="LogFile_Search" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <table id="mytable">
                <tbody>
                    <tr>
                        <td id="mytd">Result(P|F|N):  <input type="text" id="Result_PFN" name="Result_PFN" size="31"/></td>
                        <td id="mytd">Script Version:  <input type="text" id="ScriptVersion" name="ScriptVersion" size="31"/></td>
                    </tr>
                </tbody>
            </table>
            <br>
            <input type="submit" value="Search" onclick="doForm();">
            <input type="button" value="Reset" onclick="resetForm();">
        </form>
<div id="searchResult">
            <?php
            /* Where the logs files are dropped by external process. */
            $mydir = $_SERVER['DOCUMENT_ROOT'] . "/LogFile_Search/LogFiles";
            /* Handle to directory. */
            $dir = opendir($mydir);
            /* Form variables. */
            $Result_PFN = $_POST['Result_PFN'];
            $ScriptVersion = $_POST['ScriptVersion'];
            /*  Test each field -- triggers JS alert if all are empty.         */
            if (!empty($Result_PFN)) {
                doResult_PFN();
            } elseif (!empty($ScriptVersion)) {
                doScriptVersion();
            }
            /* Function for Search on Script version in logfile contents.             */
            function doScriptVersion() {
                global $dir;
                global $mydir;
                global $ScriptVersion;

                echo '<table id=\'mytable\'><tbody><tr><td id=\'mytd\'>Log File Name.</td></tr>';
                //grabs all log files matching the script version number that's input in the form.
                $cmd = "find " . $mydir . " -type f -print0 | xargs -0 grep  -e 'Test_Version=" . $ScriptVersion . "'";
                exec($cmd, $output);

                //Sort the array using a case insensitive "natural order" algorithm.
                natcasesort($output);

                if (count($output) > 0) {
                    foreach ($output as $v) {
                        //Display file name without full path as URL.
                        echo '<tr><td><a href="' . basename($mydir) . '/'
                        . array_pop(explode("/", array_shift(explode(":", $v)))) . '"  target="_blank">'
                        . array_pop(explode("/", array_shift(explode(":", $v)))) . '</a></td></tr>';
                    }
                } else {
                    echo "<html><body style=\"background-color:#0080c0\">
<script type=\"text/javascript\" language=\"javascript\">alert(
'Nothing found'
+ '.');</script> </body></html>";
                }
                closedir($dir);
                echo "</table></body>";
            }

            /* Function for Search on Result marker in file name.            */
            function doResult_PFN() {
                global $dir;
                global $mydir;
                global $Result_PFN;

                echo '<table id=\'mytable\'><tbody><tr><td id=\'mytd\'>Log File Name.</td></tr>';

                if ($dir) {
      //List files in directory
                    while (($file = readdir($dir)) !== false)
      //Ignores OS stuff.
                        if ($file != "." && $file != "..") {
      //Pattern match for result.
                            if (preg_match("/~(P|F|N)*" . $Result_PFN . "~/i", $file)) {
                      //Sort the array using a case insensitive "natural order" algorithm.
                      natcasesort($file);
                                echo '<tr><td><a href="' . basename($mydir) . '/' . $file . '"  target="_blank">' . $file . '</a></td></tr>';
                            }
                        } else {
                    echo "<html><body style=\"background-color:#0080c0\">
<script type=\"text/javascript\" language=\"javascript\">alert(
'Nothing found'
+ '.');</script> </body></html>";
                }
                    closedir($dir);
                }
                echo "</table></body>";
            }
            ?>
        </div>
    </body>
</html>

 

When doResult_PFN() returns no result, the ELSE portion is never evaluated and no notification is sent to the user. However, I have no problem with doScriptVersion(): it returns an alert.

 

What am I missing?

 

Thanks,

 

Al.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.