What am I doing wrong in my loop (with search)?

There’s a .txt file with 18 lines in it. Each line has a bunch of data about a particular book (title, author, isbn, etc…) that separates these attributes with an asterisk “*”. The php page is reading from that file, extracting the data on the line (via explode), seeing if the keyword (from another page) matches the title of any of the books and printing the results. Or it should be anyway.

I can get it to display all the books if there is a blank keyword (this is set to ‘ALL’). But if there IS a keyword entered, it only reads the first line in the .txt file…this leads me to believe that I did something wrong in my loop.

I’ve spent several hours trying to figure out what it is I’m doing wrong here. I just can’t get it. Any help with an explanation as to what I did wrong or what I needed to do, would be greatly appreciated. I don’t want a copy/pasted solution, I want to learn from the mistake and understand. Here’s the code:

[php] if ($keyword === ‘ALL’)
{
for ($ii = 1; $ii <= $lines_in_file; $ii++)
{
print “

” . $ii . ". " . $title;
print “
” . "Category: " . $category;
print “
” . "Publication Date: " . $pub_date;
print “
” . "ISBN: " . $isbn . “

”;
}
}
else
{
for ($ii = 1; $ii <= $lines_in_file; $ii++)
{
		$pos = stripos($title, $keyword);
		if ($pos === false)
		{
			print "<br />There are no matches found for " . "'" . $keyword . "'";
			break;
		} elseif ($post !== false)
		{
			print "<p>" . $ii . ". " . $title;
			print "<br />" . "Category: " . $category;
			print "<br />" . "Publication Date: " . $pub_date;
			print "<br />" . "ISBN: " . $isbn . "</p>";		


		} [/php]

Where did I go wrong?

I would start with the
[php]} elseif ($post !== false)[/php]
is it supposted to be
[php]} elseif ($pos !== false)[/php]

That was a typo, it is indeed supposed to be $pos.

However, changing that made no differences.

what does $pos = ?
echo it out see what it is , maybe not what you expect it to be.

I made a replica of what you are doing works for me

[php]<?php
$lines_in_file = 3;

$title = ‘football went in the net’;
$category = ‘Footbal’;
$pub_date = ‘12-12-2012’;
$isbn = ‘3424-234234-2342345’;

$keyword = ‘net’;

if ($keyword === ‘ALL’) {
for ($ii = 1; $ii <= $lines_in_file; $ii++) {
print “

” . $ii . ". " . $title;
print “
” . "Category: " . $category;
print “
” . "Publication Date: " . $pub_date;
print “
” . "ISBN: " . $isbn . “

”;
}
} else {
for ($ii = 1; $ii <= $lines_in_file; $ii++) {
$pos = stripos($title, $keyword);
if ($pos === false) {
print "
There are no matches found for " . “’” . $keyword . “’”;
break;
} elseif ($pos !== false) {
print “

” . $ii . ". " . $title;
print “
” . "Category: " . $category;
print “
” . "Publication Date: " . $pub_date;
print “
” . "ISBN: " . $isbn . “

”;
}
}
}
?>[/php]

result

  1. football went in the net
    Category: Footbal
    Publication Date: 12-12-2012
    ISBN: 3424-234234-2342345

  2. football went in the net
    Category: Footbal
    Publication Date: 12-12-2012
    ISBN: 3424-234234-2342345

  3. football went in the net
    Category: Footbal
    Publication Date: 12-12-2012
    ISBN: 3424-234234-2342345

Here is the rest of the code before that loop:

[php] $keyword = $_POST[‘keyword’];

if (empty($keyword))
	{
		$keyword = 'ALL';
		print "<h3>Current Titles</h3>";
	} else 	{
		if (!empty($keyword))
    	{
    	print "<h3>Current Titles that match: " . $keyword . "</h3>";
     	}
    }


	// open booklist.txt file

$filename = 'data/' . 'booklist.txt'; // indicates directory and filename

$lines_in_file = count(file($filename)); // counts lines in file

$fp = fopen($filename, 'r'); // opens file for reading

$line = fgets($fp);  //Reads one line from the file
$linet = trim($line); // gets rid of any whitespace to left or right of the line

list($title, $category, $pub_date, $isbn) = explode ('*', $line); // looks at line for delimiter, breaks it up into parts[/php]

As far as what $pos does, I’m using it as it is used on php.net re: stripos function: http://php.net/manual/en/function.stripos.php

As far as your replica, that isn’t it. Each entry is supposed to be different from one another and the keyword is used to search from them all, then you are to print only the entries that contain the keyword in the title.

It does what you wanted it to do, it does not look at others as I have not set it to an array but a static output so yes it does relicate what you wanted.

Seeing as i do not have a list of categories and titles to make in to a page list add other code to put the list in to memory then go down it line by line, my version does it the way you gave it to me.

I did not ask what it did i asked what it echoed out to you as is it what you expected ?
$pos could be false all the time for all I know.

I appreciate the help. I was able to mess around with it a bit and I did get it to behave the way I wanted eventually (which was to look at each line and see if the keyword was in the title and if so, print that line out).

Here’s the updated code for that: [php] if ($keyword !== ‘ALL’)
{

for ($ii = 1; $ii <= $lines_in_file; $ii++)
	{
		$line = fgets($fp);  //Reads one line from the file
		$linet = trim($line); // gets rid of any whitespace to left or right of the line
		list($title, $category, $pub_date, $isbn) = explode ('*', $linet); // looks at line for delimiter, breaks it up into parts

		$pos = stripos($title, $keyword);

		//print "<br />[[ DEBUG #1: \$pos is: " . $pos . "<br />"; // debugging

		if ($pos !== false)
		{
			print "<p>" . $ii . ". " . $title;
			print "<br />" . "Category: " . $category;
			print "<br />" . "Publication Date: " . $pub_date;
			print "<br />" . "ISBN: " . $isbn . "</p>";		
		
		} [/php]

However, what if the $pos IS false…that is, what if the keyword does NOT match something in the $title?

How would I get it to print out an error message if there weren’t any titles with the keyword?

nvm. I think I got it.

I put the entire thing into 1 loop, then used the following outside that loop to generate an error message:

[php] }
if (empty($prnt_ctr) && ($keyword !== ‘ALL’))
{
print “
There are no titles with the keyword: '” . $keyword . “’
” ;
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service