Forum Moderators: coopster

Message Too Old, No Replies

Notification when no results does not work

php form function

         

kristo5747

9:21 pm on Dec 16, 2010 (gmt 0)

10+ Year Member



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:
<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?

Matthew1980

9:35 pm on Dec 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there kristo5747,

Well looking at the opening part of that function, your mixing your styles of echo:-

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

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

The difference is that double quoting requires escaping of the quotes via 'toothpicking', and the single quote method doesn't

Hopefully you can get the error_reporting(E_ALL); turned on so that you can get more detail on your error's or warnings that your likely getting.

Also:-

function doResult_PFN() {
global $dir; <-
global $mydir;<-
global $Result_PFN;<-

these globals should be passed into the function as parameters, and not done the way as you have, this puts necessary CPU power on the php parser whilst it searches for a reference to the variables your quoting within the scope of your application.

And really you should define a function before you call it too, in class/oop context this does work (method's available up and down the chain..), but not considered good architecture when you call before you build ;)

Cheers,
MRb

kristo5747

9:52 pm on Dec 16, 2010 (gmt 0)

10+ Year Member



Matthew, thanks so much for your reply. I will follow your recommendations.

Forgive me but I am not understanding (I am self-taught) what do you mean by "call before you build".

Can you give me an example?

Matthew1980

10:19 pm on Dec 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>Forgive me but I am not understanding (I am self-taught) what do you mean by "call before you build".

Example:-

echo foo();<--function called before it's defined

function foo(){
return "This is foo";
}

echo foo();<--function called after it's been defined

Analagy: Imagine trying to 'see' what colour top someone has on before you have opened the door to greet them..

Just the way logic works I guess ;-p and the way as I have always looked at function construction...

Cheers,
MRb