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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
theodorejsalvoCommented:
Can you copy the error it says when you run the script?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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 StanyonWebDevCommented:
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 BaldwinFixer 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 StanyonWebDevCommented:
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 StanyonWebDevCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.