foreach as - error in my PHP function, only 37 lines long, please look.

This php function searches for sequentially named images and echo's them out.  It used to work.. Maybe the command was depreciated or my syntax is wrong? I always get an error on line 26, that's the line that simply says "AS"

For some reason  "AS" keeps giving me a php error
<?php
$part='PART1';
$webserver='/home/webmaster/domains/website/public_html/media/import/';

function pictureTest($input)
        {
        $link = mysql_connect('localhost', 'dbuser', 'dbpassword');
        if (!$link) {
            die('Could not connect: ' . mysql_error());
        }
        if (!mysql_select_db('test')) {
            die('Could not select database: ' . mysql_error());
        }
		
        $result = mysql_query("SELECT image FROM test WHERE part = '$input'");
        if (!$result) {
			die('Could not query:' . mysql_error());
			$input=mysql_result($result, 0, 'image');
			mysql_close($link);
			return $input;
        }
			{
			global $webserver;
			$p = "";
			foreach(glob($webserver.$input.'*.jpg') 
			as 
			$filename) {
			$p .= "," . str_replace($webserver,"",$filename); }
			$p = substr($p,1,strlen($p) - 1);
			return $p;
			}
		}

		
echo pictureTest("PART1");

?>

Open in new window

www_puertoricoautoforo_comAsked:
Who is Participating?
 
theodorejsalvoConnect With a Mentor Commented:
Can you copy the error it says when you run the script?
0
 
theodorejsalvoCommented:
Are you missing an else somewhere around line 22?
0
 
www_puertoricoautoforo_comAuthor Commented:
I tried just now putting an "else" there but it didn't work.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
www_puertoricoautoforo_comAuthor Commented:
Warning: Invalid argument supplied for foreach() in /home/webmaster/domains/website.com/public_html/csv_fixer/test2.php on line 22
0
 
Chris StanyonConnect With a Mentor Commented:
Not really sure I understand your logic.

Can you let us know what error you're getting, and also try putting the foreach statement all on one line.

if (!$result) {
     die('Could not query:' . mysql_error());
     //THE FOLLOWING THREE LINES WILL NEVER RUN
     $input=mysql_result($result, 0, 'image');
     mysql_close($link);
     return $input;
}

{    //WHY IS THIS LINE HERE
     global $webserver;
     $p = "";
     foreach(glob($webserver.$input.'*.jpg') as $filename) {
          $p .= "," . str_replace($webserver,"",$filename);
     }
     $p = substr($p,1,strlen($p) - 1);
     return $p;
}    //WHY IS THIS LINE HERE

Open in new window

0
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
I think your error is probably on line 25 at glob($webserver.$input.'*.jpg').  PHP is saying line 26 because it didn't make sense at that point.  I would echo some of the values like $input to see if they have acceptable values.  Where are you getting $input?
0
 
Chris StanyonCommented:
Before your foreach statement add the following line

print_r(glob($webserver.$input.'*.jpg'));

That way you will see if the glob function is returning an array - which is what the foreach loop is expecting - and seemingly not getting



0
 
www_puertoricoautoforo_comAuthor Commented:
If I delete the
9: {    //WHY IS THIS LINE HERE

and the

17: {    //WHY IS THIS LINE HERE

it still gives me an error on line 26

Warning: Invalid argument supplied for foreach() in /home/webmaster/domains/raceonusa.com/public_html/csv_fixer/test2.php on line 26
0
 
Chris StanyonCommented:
Hi,

Didn't say that would fix the problem - just wondered why it was there.

Did you get a response from the print_r statement?
0
 
Dave BaldwinFixer of ProblemsCommented:
I would not be able to access a directory the way you are from a web page.  The first line below works for on my server on a web page.  I'm guessing $input is a subdirectory.  See if the second line might work for you.
foreach(glob('media/import/*.jpg') as $filename)
foreach(glob('media/import/$input/*.jpg') as $filename)

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
I'm sure that error message refers to glob($webserver.$input.'*.jpg').  Either $webserver or $input has a bad value or maybe your host has changed permissions on Apache so you can't access that directory that way.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.