PHP help please

Soldato
Joined
19 Jul 2006
Posts
2,960
Location
Leicester
Hi,

I have a script that searches a .txt file based on a surname the user inputs.

My problem is, I can't seem to add an error message if no users are found.
I did manage to get an error message to display, but it was displaying for the amount of people found in the txt file, but that didn't match the search, along with the one user found.

I've not done PHP for a few years now and can't seem to figure this out.
I did originally find this script on the Internet and have changed it a little bit.

Thanks for any help.

PHP:
if ( isset( $_POST['search'] ) )

{

$q = $_POST["surname"];

if (empty($q))

{
echo "You must enter a Surname in the box below.";
}

else

{

$f = fopen("list.txt", "r");

while (($line = fgets($f)) !== FALSE)

{
if (strstr($line, $q)) {
print "<li>$line<br/>";
}

}

}

}
 
Associate
Joined
24 May 2011
Posts
262
PHP:
if (isset($_POST['search']))
{

    $q = $_POST["surname"];

    if (empty($q)){
        echo "You must enter a Surname in the box below.";
    } else {

        $f = fopen("list.txt", "r");

        while (($line = fgets($f)) !== FALSE)
        {
            if (strstr($line, $q)) {
                print "<li>$line<br/>";
            }
        }

    }

}

I've formatted it so it's easier to read.

The following code is used to print the line when it's found:
PHP:
if (strstr($line, $q)) { 
  print "<li>$line<br/>"; 
}

You could use a boolean to store if the line has been found. After the 'while' loop you can then check that boolean and print out an error if it has not been found.
 
Soldato
OP
Joined
19 Jul 2006
Posts
2,960
Location
Leicester
Thanks for the reply.

There might be more than one user returned though if they have the same surname as another, so I'm still not sure how to do it.

Thanks in advance for any help.
 
Associate
Joined
7 May 2004
Posts
353
Couple of things, firstly strstr() is case sensitive. So if the text file contains 'Smith' and the user inputs 'smith', it won't find any matches. stristr() is case insensitive.

Secondly, the way I'd do it is with a simple count. Then you can return number of results for that surname and also error if the result is 0.

PHP:
if (isset($_POST['search'])) {

    $q = $_POST["surname"];

    if (empty($q)) {
        echo "You must enter a Surname in the box below.";
    } else {

        $f = fopen("list.txt", "r");
        $i = 0;

        while (($line = fgets($f)) !== FALSE)
        {
            if (stristr($line, $q)) {
                print "<li>$line<br/>";
                $i++;
            }
        }

        if($i == 0) {
             echo "No results found.";
        } else {
            echo $i . " results found."
        }

    }

}
 
Last edited:
Soldato
OP
Joined
19 Jul 2006
Posts
2,960
Location
Leicester
Thanks for the help.

Markeh, it works great, thanks...although I had to spend a few minutes thinking 'why doesn't it work?' then I saw no ; at the end!
 
Back
Top Bottom