Page 1 of 1

[SOLVED] For Loop - Error Message

Posted: Wed Jul 11, 2007 9:04 am
by dome90uk
Hi,

I have inherited a piece of code from a client which I cannot get to work properly. The validation in the script doesn’t work – ( i.e. when a user doesn’t put anything into a search box they receive this error. ( users can search via town or postcode )

Code: Select all

“Warning: stristr() [function.stristr]: Empty delimiter. in /home/ public_html/newsite/export.php on line 19”
Here is the code:

Code: Select all

<?
$searchterm=$_GET[search];
$fname="export.csv";
$numfields=7;
$fd = fopen ($fname, "r");
$contents = fread ($fd, filesize ($fname));
$contents=trim($contents);
echo "<br><br>";
$contents=strtr($contents,chr(10),",");
fclose ($fd);
$fvalues=explode(",",$contents);
$numbers=count($fvalues);
$found=0;

		for ($i=0;$i<=$numbers;$i=$i+$numfields)
		{
			$a1=$fvalues[$i+2];
			if (stristr($a1,$searchterm))
			{
			$company=trim($fvalues[$i]);
			echo "<p><b>$company</b><br>";
			$a1=$fvalues[$i+1];
			echo "Contact: $a1<br>";
			$a1=$fvalues[$i+2];
			echo "Address: $a1<br>";
			$a1=$fvalues[$i+3];
			echo "Telephone: $a1<br>";
			$a1=$fvalues[$i+4];
			echo "Fax: $a1<br>";
			$a1=$fvalues[$i+5];
			echo "Email: <A HREF='mailto:$a1'>$a1</A><br>";
			$a1=$fvalues[$i+6];
//			echo "Website: $a1</A><br>";
			if ($a1=="None")
			{
				$a1="";
			}
			echo "Website: <A HREF='http://$a1' TARGET='_blank'>$a1</A><br>";
			$found=$found+1;
			echo"<br></p><hr>";
			}

		}
		if ($found>0)
		{
			echo "<p><br>$found member(s) found.<br></p>";
		}
		else
		{
			echo "<p><br>Sorry, no members found in that area.<br></p>";
		}
			?>
The code searches a CSV file for members contact details and displays them on the webpage.

I have a very little knowledge of PHP and would really appreciate any guidance or help that you could share. Thanks.

Dome

Posted: Wed Jul 11, 2007 9:34 am
by arturm
before you do

Code: Select all

for ($i=0;$i<=$numbers;$i=$i+$numfields)
add

Code: Select all

if (!empty($searchterm)) {

    // your "for" loop

}
this should solve your problem

[SOLVED]

Posted: Wed Jul 11, 2007 10:05 am
by dome90uk
Thank you - that solved it :)