Jump to content

include variable file name


frazto
 Share

Recommended Posts

Hi there!Now bear with me as i am a total amateur when it comes to PHP...I currently use a bit of script to randomly select an image from an array on refresh and this works fine;<?php $imagearray=file('backgrounds.inc.php'); $imagenum=rand(0,count($imagearray)-1);?><style type="text/css">#image {position: relative;top: 150px;left: 0px;margin: 0px;width: 800px;height: 400px;background : url(<?php echo $imagearray[$imagenum];?>) #FFFFFF repeat;}</style>What I would like to do however is match the image to some text which describes it eg;if ( $imagearray[$imagenum] == "images/bg.jpg" ) include ("info.html");BUT I would like not to have to type out a list of every possible eventuality ie bg1, bg2, bg3, bg4 etc... Instead it would match the number of the info.html file to the number of the bg.jpg file.Also, currently the echo $imagearray[$imagenum] command echos: images/bg.jpg, but I can't get $imagearray[$imagenum] to equal images/bg.jpg.Any help on this would be greatly appreciated.I hope it makes sense.

Link to comment
Share on other sites

hi frazto....the file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] ) function as far as i know might suit you better using the FILE_USE_INCLUDE_PATH flag (and according to the documentation on php.net for php 5) - it however only returns the files contents as a string. you would thus need to explode ( string $delimiter , string $string [, int $limit ] ) that string by delimiter - eg "\r\n" in order to have it as an array such as you expect :) i used this function to read and write the database connection include's contents in an admin site i am working on... :)

$imagearray = explode('\r\n',file_get_contents('backgrounds.inc.php', FILE_USE_INCLUDE_PATH));

remember this include path would be relative thus the backgrounds.inc.php would need to be in the same directory as the file containing the above code. if in an ssi subdirectory from that, simply prefix in the string ssi/ if in an upper directory ../ lots of info in traversing paths from the current document - just <_< for "php path" :)hope that helps :(

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

or you could just go ahead and use the file ( string $filename [, int $flags = 0 [, resource $context ]] ) function just remember to use the same FILE_USE_INCLUDE_PATH flag as with file_get_contents :)as such:

$imagearray = file('backgrounds.inc.php', FILE_USE_INCLUDE_PATH | FILE_IGNORE_NEW_LINES);

remember to reference the path to the file to retrieve contents from correctly! :)AND add the FILE_IGNORE_NEW_LINES flag too, unless you want to add an rtrim ( string $str [, string $charlist ] ) function to the array elements printed :) they WOULD have a new line ending the individual elements of the array otherwise...use

print_r($imagearray);

after setting the variable, if you need to inspect the contents of an array too.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

What I would like to do however is match the image to some text which describes it eg;if ( $imagearray[$imagenum] == "images/bg.jpg" ) include ("info.html");BUT I would like not to have to type out a list of every possible eventuality ie bg1, bg2, bg3, bg4 etc... Instead it would match the number of the info.html file to the number of the bg.jpg file.
You can just use the variable name in the string you use for the path. ie,include("info_bg".$imagenum.".html");assuming your info files are named info_bg1.html, info_bg2.html, and so on.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...