Forum Moderators: coopster

Message Too Old, No Replies

Need help with for each.

         

Simone100

5:56 am on Aug 23, 2007 (gmt 0)

10+ Year Member



Hello, can someone please tell me why for each works on the type of array below, but not an array like this:


$allfiles = Array();
$allfiles[1] = "afile.txt";
$allfiles[2] = "thatfile.html";
$allfiles[3] = "thatfile.gif";

The reason I'm trying to change it is because I need to be able to name the files anything, without numbers on them so putting the number in the array instead.
When I look up for each in the manual, [us.php.net...] it shows it working on arrays like the just above. But when I insert it below in the script, the script won't work anymore. Maybe it needs to call the numbers in the array somehow. Can someone tell me how to get my array above working in the following code? Thanks very much.

<?php
$currentFile = date('s');
$allfiles = array('this01.txt','test02.txt','test03.html','imagefile04.gif','image05.gif');
foreach($allfiles as $get):
if(preg_match("/".$currentFile."\.txt/","/".$get."\.txt/")):
$get_content = file_get_contents($get);
break;
elseif(preg_match("/".$currentFile."\.html/","/".$get."\.html/")):
$get_content = file_get_contents($get);
break;
elseif(preg_match("/".$currentFile."\.gif/","/".$get."\.gif/")):
echo "<img src=\"$get\">";
break;
endif;
endforeach;
echo $get_content; exit();?>

[1][edited by: Simone100 at 6:19 am (utc) on Aug. 23, 2007]

vincevincevince

6:46 am on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change:
if(preg_match("/".$currentFile."\.txt/","/".$get."\.txt/")):
To:
if(preg_match("/".$currentFile."\.txt/","".$get.".txt")):

Do the same for other similar lines.

Simone100

9:18 am on Aug 23, 2007 (gmt 0)

10+ Year Member



Thanks, now it says the echo $get_content; on the last line is undefined and not sure why.

<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$currentFile = date('s');
$allfiles = Array();
$allfiles[1] = "afile.txt";
$allfiles[2] = "thatfile.html";
$allfiles[3] = "thatfile.gif";
foreach($allfiles as $get):
if(preg_match("/".$currentFile."\.txt/","".$get.".txt")):
$get_content = file_get_contents($get);
break;
elseif(preg_match("/".$currentFile."\.html/","".$get.".html")):
$get_content = file_get_contents($get);
break;
elseif(preg_match("/".$currentFile."\.gif/","".$get.".gif")):
echo "<img src=\"$get\">";
break;
endif;
endforeach;
echo $get_content; exit();?>

If anyone thinks the way I'm doing this is too hard please let me know. For instance if there is a way to see if $allfiles is a gif any easier, then to continue if it is, something like?
if($allfiles && $currentfile == ".jpg"¦".gif")
echo "<img src=/'$allfiles[$currentFile]'>";
else etc.

Or if it can still be fixed as is... Thank you very much.

[1][edited by: Simone100 at 9:22 am (utc) on Aug. 23, 2007]

vincevincevince

9:22 am on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are looking for $get.txt (in the first case) but then you file_get_contents() on just $get...

Your file_get_contents($get) should be file_get_contents($get.".txt") etc. for each case

Although, it does occur to me that the error might be in the preg_match, where the second .".txt" should not be there at all.

Simone100

9:25 am on Aug 23, 2007 (gmt 0)

10+ Year Member



Thank you, not working yet.

[edited by: Simone100 at 9:34 am (utc) on Aug. 23, 2007]