Forum Moderators: coopster

Message Too Old, No Replies

Trying to get in array working

         

Simone100

6:56 am on Aug 24, 2007 (gmt 0)

10+ Year Member



Hello, anyone know why this code isn't working? All it shows on the page is the current second but no files on page refresh. Please let me know whats wrong, thanks very much.


<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$currentfile = date('s');
$allfiles = array();
$allfiles[1] = "thisfile.txt";
$allfiles[2] = "thatfile.html";
$allfiles[3] = "thatfile.gif";
$allfiles[4] = "thisfile.txt";
$allfiles[5] = "thatfile.html";
$allfiles[6] = "thatfile.gif";
if (in_array(".gif",$allfiles))
{echo '<img src="'.$allfiles[$currentfile].'">';}
else {$get_content = file_get_contents($allfiles[$currentfile]);
echo $get_content;}
?>

dreamcatcher

7:04 am on Aug 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Simone100,

This is your problem:

if (in_array(".gif",$allfiles))

Its looking for a value of '.gif' and there isn`t one.

dc

Simone100

7:10 am on Aug 24, 2007 (gmt 0)

10+ Year Member



Thank you very much, I get the same result even if I put in the whole address "thatfile.gif" Is there an easy way to scan for a .gif without all the hassle of preg match?

[edited by: Simone100 at 7:12 am (utc) on Aug. 24, 2007]

vincevincevince

7:13 am on Aug 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd recommend using something like strpos() rather than in_array()

Simone100

8:11 am on Aug 24, 2007 (gmt 0)

10+ Year Member



Thanks, it says it will only look for the first position gif, I need it to look for it anywhere in the array.

vincevincevince

8:13 am on Aug 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then combine it with foreach and test each member of the array individually with strpos

Simone100

9:02 am on Aug 24, 2007 (gmt 0)

10+ Year Member



I don't know how.

Maybe someone can tell me why this one won't work then. Says undefined index on these two lines.
if (in_array(substr($allfiles[$currentfile], -3), array('gif','jpg','png')))
else { $get_content = file_get_contents($allfiles[$currentfile]);

and whole code.


<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$currentfile = date('s');
$allfiles = Array();
$allfiles[1] = "thisfile.txt";
$allfiles[2] = "thatfile.html";
$allfiles[3] = "thatfile.gif";
$allfiles[4] = "thisfile.txt";
$allfiles[5] = "thatfile.html";
$allfiles[6] = "thatfile.gif";
if (in_array(substr($allfiles[$currentfile], -3), array('gif','jpg','png')))
echo '<img src="'.$allfiles[$currentfile].'">';
else { $get_content = file_get_contents($allfiles[$currentfile]);
echo $get_content;}
?>

Can anyone see why this isn't working? Thanks.

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

dreamcatcher

12:24 pm on Aug 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Simone100,

Try something like this:

$images = array('.gif','.jpg','.png);

foreach ($allfiles AS $file)
{

if (in_array(strrchr(strtolower($file),'.'),$images))
{
echo '<img src="'.$file.'">';
}
else
{
echo file_get_contents($file);
}

}

Not tested, but should work how you want I think.

dc

Simone100

7:44 am on Aug 30, 2007 (gmt 0)

10+ Year Member



Thanks for helping, don't understand this, it says Parse error: parse error, unexpected T_STRING, expecting ')' in
echo '<img src="'.$file.'">';

I tried adjusting the quotations but fouled it up worse and I don't understand why it wants a paranthesis there, seems like the brackets like you have it should be the way.

[edited by: Simone100 at 7:46 am (utc) on Aug. 30, 2007]

Simone100

8:57 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



Nevermind, going to try something else. Thanks

[edited by: Simone100 at 8:57 pm (utc) on Aug. 30, 2007]

cmarshall

9:56 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would HIGHLY recommend using preg_match for this.

preg_match is your friend. He's a bit loopy, but very rich, and will let you drive his Maserati if you learn his lingo.