1

I'm having an issue with one of my conditional statements. The code below is a single text search where the user can enter a string and check a set of files within a directory. The code is working great, although I'm just having a small output glitch.

The 2nd conditional below (just before find_files function) is displaying one echo statement in the middle of my search results. In other words, my results are displaying perfectly, although that 2nd conditional statement appears once within the search results.

Even more weird is that the conditional does works when it's supposed to (i.e. when I enter a string and the string "is not found" within the files), so I'm confused. And I know the conditional is not included in a loop, so why would it display at all during the search?

This is the one last glitch I need to work out and this will work great. Any help would be appreciated.

 <?php

 $query = $_POST['query'];



 if ((isset($query)) && (empty($query))) {
 echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
 }


 elseif ((isset($query)) && (!find_files('.')))  { 
 echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>"; 
 } 



 find_files('.');
 function find_files($seed) {
 if(! is_dir($seed)) return false;
 $files = array();
 $dirs = array($seed);

 while(NULL !== ($dir = array_pop($dirs)))
    {
      if($dh = opendir($dir))
        {
          while( false !== ($file = readdir($dh)))
            {
              if($file == '.' || $file == '..') continue;
              $path = $dir . '/' . $file;
              if(is_dir($path)) {    $dirs[] = $path; }
              else { if(preg_match('/^.*\.(php[\d]?|js|txt)$/i', $path)) { check_files($path); }}
            }
          closedir($dh);
        }
    }
}

function check_files($this_file) 
{

$query = $_POST['query'];

$str_to_find = $query;


if(!($content = file_get_contents($this_file))) { echo("<p style=\"color:darkgray; font-
family:arial\">Could not check $this_file</p>\n"); }
else { if(stristr($content, $str_to_find)) { echo("<p style=\"color:darkgray; font-
family:arial\">$this_file -> contains $str_to_find</p>\n"); }}
unset($content);
}

?>
5
  • 2
    Missing return true; at the end of find_files() ? Commented Feb 26, 2012 at 17:56
  • 2
    Just as a minor point, it's often better to return your values from functions rather than to echo them. This gives you fewer points (depending on circumstances) to check for issues. Commented Feb 26, 2012 at 18:03
  • HI Eugen, I placed return true; directly under 'unset($content); and the output still displays the same Commented Feb 26, 2012 at 18:04
  • 1
    I see you chose not to use my formatting improvements from last question. Commented Feb 26, 2012 at 18:12
  • Mike, I have taken your comments seriously, although I have not yet implemented due to my obsession of wrapping up the isssues with this code. Please don't assume I didn't choose to listen. Commented Feb 26, 2012 at 18:19

2 Answers 2

1

Simply adding return, won't help. this modified code works and displays no error me

    if (!isset($_REQUEST['query']))
    {
        //Ask for query here :)     
        //echo "<p style=\"color:darkgray; font-family:arial\">No query specified.</p>";
        exit;
    }

    $query = isset($_REQUEST['query']) ? $_REQUEST['query'] : '';


    if (empty($query))
    {
        echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
        exit;
    }


    $filesFound = find_files('.');

    if (!$filesFound)
    {
        echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
    }


    function find_files($seed)
    {
        if (!is_dir($seed)) return false;
        $found = false;
        $dirs = array($seed);

        while (NULL !== ($dir = array_pop($dirs)))
        {
            if ($dh = opendir($dir))
            {
                while (false !== ($file = readdir($dh)))
                {
                    if ($file == '.' || $file == '..') continue;
                    $path = $dir . '/' . $file;
                    if (is_dir($path))
                    {
                        $dirs[] = $path;
                    }
                    else
                    {
                        if (preg_match('/^.*\.(php[\d]?|js|txt)$/i', $path))
                        {
                            if (!$found)
                            {
                                $found = check_files($path);
                            }
                        }
                    }
                }
                closedir($dh);
            }
        }
        return $found;
    }

    function check_files($this_file)
    {
        $query = $_REQUEST['query'];

        $str_to_find = $query;


        if (($content = file_get_contents($this_file)) === false)
        {
            echo("<p style=\"color:darkgray; font-family:arial\">Could not check $this_file</p>\n");
            return false;
        }
        else
        {
            if (stristr($content, $str_to_find))
            {
                echo("<p style=\"color:darkgray; font-family:arial\">$this_file -> contains $str_to_find</p>\n");
                return true;
            }
        }
    }
Sign up to request clarification or add additional context in comments.

6 Comments

As nasty as this code is (not your fault), it does answer the question.
BlindAndFurious, this is working good so far, great job, although before I input any search string, it's outputting "Your search produced no results". I want that to wait until user inputs a search. We are getting close.
Just change the text in first if statement (code modified) or comment out the echo statement.
Your suggestion does work, but I still would like it to say, "Your search produced no results" if the user submits a query without any text. By commenting out your code, it took that possibility away at all. I'm already voting your answer for sure, but that would make my day to get that figured out.
BlindAnd Furious, I just noticed that your code it producing only one result from the search. Would you be able to take that time and look at your code to see why it would do this?
|
-1

In your second condition, you are checking to see if the condition: !find_files('.') matches. In order to check that condition, PHP is actually running the function at that time to get its return value, which it then checks for the condition.

On top of that, the find_files() function returns false when it is provided with incorrect input, but does not send a return value when it is successful. That means it does not provide the conditional statement with a value that evaluates to positive, so !find_files('.') evaluates to true, and the echo statement runs.

To fix this, you should just add a return true; as the very last line of your find_files() function.

But I'd also recommend fixing the fact that you're running the function twice. Use something like:

if ((isset($query)) && (empty($query))) {
    echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
}

else {
    $success = find_files('.');

    if ((isset($query)) && (!$success))  { 
        echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
    }
}

Instead of:

if ((isset($query)) && (empty($query))) {
    echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
}

elseif ((isset($query)) && (!find_files('.')))  { 
    echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>"; 
}

find_files('.');

7 Comments

If you take a closer look, find_files actually generates output through check_files. So returning true won't help.
Rick with your answer do I still need 'return true;' at bottom? I tried your answer without the return true and it's still outputting that result. Any thoughts?
@h0tw1r3 - That output isn't the problem though. The problem is that find_files can never evaluate to true, so !find_files('.') can never return false.
@Rob Myrick - Yes, the return true; is the most important part. It needs to go at the end of the find_files() function though, not at the end of the other function or the document.
And from my newbie perspective, I'm seeing that the condition !find_files('.') is being evaluated before the find_files function is ever being run. Is that the case? Or is find_files being evaluated at the same time it runs the condition?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.