Link to home
Start Free TrialLog in
Avatar of www_puertoricoautoforo_com
www_puertoricoautoforo_comFlag for United States of America

asked on

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

Avatar of theodorejsalvo
theodorejsalvo
Flag of United States of America image

Are you missing an else somewhere around line 22?
Avatar of www_puertoricoautoforo_com

ASKER

I tried just now putting an "else" there but it didn't work.
ASKER CERTIFIED SOLUTION
Avatar of theodorejsalvo
theodorejsalvo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Warning: Invalid argument supplied for foreach() in /home/webmaster/domains/website.com/public_html/csv_fixer/test2.php on line 22
SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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



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
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?
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

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.