kade119 Posted April 20, 2010 Share Posted April 20, 2010 Hey all, Newb is back, someone help me work up a small script to dynamically call a specific banner image depending on which page is clicked on... contact.php = contact.jpg work.php = work.jpg etc.. thanks, just want to see how it would work - trying to learn below is a script that just determines if it 's the index and if not uses an alternate image... just thought this might be somewhere to start from... <?php function is_homepage() { return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/')); } $flash = 'http://www.testsite.com/banner.swf'; if (is_homepage()) { ?> <object width="550" height="400"> <param name="movie" value="<?php echo $flash ?>"> <embed src="<?php echo $flash ?>" width="550" height="400"> </embed> </object> <?php } else { echo '<img src="banner.jpg" alt="Image Banner"/>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/ Share on other sites More sharing options...
themistral Posted April 20, 2010 Share Posted April 20, 2010 You could try something like $banner = substr($_SERVER['REQUEST_URI'], 0, (strlen($_SERVER['REQUEST_URI']))-3).'jpg'; This will take the php off the end of the filename and replace it with jpg. That should give you a starting point Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045436 Share on other sites More sharing options...
kade119 Posted April 20, 2010 Author Share Posted April 20, 2010 is there anyway you can explain the meaning of the code, so i understand it thanks Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045442 Share on other sites More sharing options...
jcbones Posted April 20, 2010 Share Posted April 20, 2010 substr() strlen() $_SERVER['vars'] Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045449 Share on other sites More sharing options...
kade119 Posted April 20, 2010 Author Share Posted April 20, 2010 okay i think i understand it... here is the additional code to finish this minor script print "<img src=\"$banner\" />" so substr() this subtracts .php from the file name strlen() this counts how many characters $_SERVER['vars'] and this .. is the location of the file? Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045457 Share on other sites More sharing options...
themistral Posted April 20, 2010 Share Posted April 20, 2010 For an explanation of $_SERVER['REQUEST_URI'] take a look at http://www.php.net/manual/en/reserved.variables.server.php So, I am taking the portion of the URL after http://www.mysite.com and manipulating it. $url = $_SERVER['REQUEST_URI']; This will return /filename.php We will then get use the substr function to get the bit we want for the filename. $banner = substr($_SERVER['REQUEST_URI'], 1); This says that we want the whole string but to start at the second character (0 being the first). $banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3); We then want the php extension to be dropped so we find the length of the string and subtract 3 (as php is 3 characters) . $banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3).'jpg'; Then you add the jpg extension. I would suggest googling substr and strlen if you don't know how to use them. Php.net has examples of how to use them as well. Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045458 Share on other sites More sharing options...
kade119 Posted April 20, 2010 Author Share Posted April 20, 2010 wow... thank you for the breakdown, that helps a lot so if i want to combine my script that identifies if it's the homepage it uses my flash file otherwise it uses my banner function. Does this look correct? <?php function is_homepage() { return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/')); } $flash = 'http://www.testsite.com/banner.swf'; if (is_homepage()) { ?> <object width="550" height="400"> <param name="movie" value="<?php echo $flash ?>"> <embed src="<?php echo $flash ?>" width="550" height="400"> </embed> </object> <?php } else { $banner = substr($_SERVER['REQUEST_URI'], 0, (strlen($_SERVER['REQUEST_URI']))-3).'jpg'; print "<img src=\"$banner\" />" } ?> Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045463 Share on other sites More sharing options...
themistral Posted April 20, 2010 Share Posted April 20, 2010 Give it a go and see what happens - you learn more from your mistakes than you will even learn from getting everything right first time. $banner = substr($_SERVER['REQUEST_URI'], 0, (strlen($_SERVER['REQUEST_URI']))-3).'jpg'; Word of caution, this will return /filename.jpg. As mentioned in my previous post, use $banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3).'jpg'; to get rid of the first backslash. Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045468 Share on other sites More sharing options...
kade119 Posted April 20, 2010 Author Share Posted April 20, 2010 $banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3).'jpg'; what does the "1" define? does that mean pull one string? Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045474 Share on other sites More sharing options...
kade119 Posted April 20, 2010 Author Share Posted April 20, 2010 this look better? <?php function is_homepage() { return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/')); } $banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3).'jpg'; $flash= '<script type="text/javascript"> var params = { wmode: "transparent" }; swfobject.embedSWF("_flash/banner_lecdg.swf", "fpBanner", "950", "300", "9.0.0", "expressInstall.swf"); </script>'; if (is_homepage()) { echo $flash; } else { echo "<img src=\"$banner\" />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045478 Share on other sites More sharing options...
kade119 Posted April 21, 2010 Author Share Posted April 21, 2010 need assistance in complete this function, not sure if i'm implementing it correctly this echo, can it be written like that? echo "<img src='../graphics/$banner' />"; <?php function is_homepage() { return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/')); } $banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3).'jpg'; $flash= '<script type="text/javascript"> var params = { wmode: "transparent" }; swfobject.embedSWF("_flash/banner.swf", "fpBanner", "620", "250", "9.0.0", "expressInstall.swf"); </script>'; if (is_homepage()) { echo $flash; } else { echo "<img src='../graphics/$banner' />"; } ?> and this is on my page include... <?php print is_homepage($banner) ?> Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045788 Share on other sites More sharing options...
kade119 Posted April 21, 2010 Author Share Posted April 21, 2010 i've tested this line and do not think it's acquiring the image based on the file name $banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3).'jpg'; Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045814 Share on other sites More sharing options...
kade119 Posted April 21, 2010 Author Share Posted April 21, 2010 any ideas? really appreciate any assistance Quote Link to comment https://forums.phpfreaks.com/topic/199182-echo-banner-image-based-on-page-name/#findComment-1045979 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.