ferdri3 Posted October 5, 2011 Share Posted October 5, 2011 Hello person who reads this now I work a lot with <?php include("..."); ?> and I never thought beyond the html I put in there. In 1 of the include files I have the set-up of a simple vertical html/css menu and on the right next to it I have an image. However, not every page on my website needs the same image and I probably will change a lot of them frequently. Now I was wondering if (and how) I could include a piece of PHP code which does the following: If the web-url is "page1.php" show "image1.png" If the web-url is "page2.php" show "image2.png" If the web-url is "page3.php" show "image3.png" etc. This way I only have to change the "imageX.png" in 1 file. I hope you can help me. Best regards and thanks in advance! Ferdri3 Quote Link to comment https://forums.phpfreaks.com/topic/248474--/ Share on other sites More sharing options...
AyKay47 Posted October 5, 2011 Share Posted October 5, 2011 seems to me like you will want to store both the page name and the image name in variables, then use an if conditional to compare the values.. $x = 1; $url = "page".$x.".php"; $image = "image".$x.".php"; something simple like that. however there are some questions that need to be answered. 1. how are these included files being generated? Dynamically? if not, you can simply add the right image manually. Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1275988 Share on other sites More sharing options...
Adam Posted October 5, 2011 Share Posted October 5, 2011 I would use a switch on the basename of the PHP_SELF server var: switch (basename($_SERVER['PHP_SELF'])) { case 'page1.php': $image = 'image1.png'; break; case 'page2.php': $image = 'image2.php'; break; // etc. } Ensure you include a "default:", or check the $image variable isset before trying to display the image. Edit You could use a method like AyKay47's suggestion - depends on your situation as to what would work best. Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1275990 Share on other sites More sharing options...
floridaflatlander Posted October 5, 2011 Share Posted October 5, 2011 I use a switch too for stuff like that, don't forget a default $url = basename($_SERVER['PHP_SELF']); switch ($url) { case "/index.php": $page_title = 'y'; $h1 = 'yy'; $h2 = 'yyy'; $discription = 'yyyy'; $keywords = 'yyyyyy'; break; default: $page_title = 'x'; $h1 = 'xxxx'; $h2 = 'x'; $discription = 'x'; $keywords = 'x'; break; } You can add an image name for a page Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1275991 Share on other sites More sharing options...
AyKay47 Posted October 5, 2011 Share Posted October 5, 2011 I would use a switch on the basename of the PHP_SELF server var: switch (basename($_SERVER['PHP_SELF'])) { case 'page1.php': $image = 'image1.png'; break; case 'page2.php': $image = 'image2.php'; break; // etc. } Ensure you include a "default:", or check the $image variable isset before trying to display the image. Edit You could use a method like AyKay47's suggestion - depends on your situation as to what would work best. yeah, really depends on the situation here and how many pages are to be created.. Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1275992 Share on other sites More sharing options...
ferdri3 Posted October 5, 2011 Author Share Posted October 5, 2011 Thanks for the fast replies! I've set up a test server: www.meesterferdi.net/test1.php www.meesterferdi.net/test2.php This is what I have now: test1.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <!-- START title & keywords & description --> <title>Test Homepage</title> <meta content="keywords" name="keywords" /> <meta content="testing page" name="description" /> <!-- !END! title & keywords & description --> <!-- START CSS stylesheetlinks --> <link href="css/test.css" rel="stylesheet" type="text/css" /> <!-- !END! CSS stylesheetlinks --> </head> <body style="margin: 0"> <?php include("menu.php"); ?> </body> </html> test2.php looks the same. menu.php <div id="menu"> <ul> <li> <a href="menu1.php" title="menu1">menu1</a> </li> <li> <a href="menu2.php" title="menu2">menu2</a> </li> <li> <a href="menu3.php" title="menu3">menu3</a> </li> <li> <a href="menu4.php" title="menu4">menu4</a> </li> <li> <a href="test1.php" title="test1">test1</a> </li> <li> <a href="test2.php" title="test2">test2</a> </li> </ul> </div> <div id="title_img"> <?php switch (basename($_SERVER['PHP_SELF'])) { case 'test1.php': $image = 'images/image1.png'; break; case 'test2.php': $image = 'images/image2.png'; break; } ?> </div> test.css /* START menu style */ ul { list-style: none; margin: 0; padding: 0; } img { border: none; } #menu { position: relative; float: left; width: 200px; margin: 10px; } #menu li a { height: 32px; voice-family: "\"}\""; voice-family: inherit; height: 24px; text-decoration: none; } #menu li a:link, #menu li a:visited { font-family:Arial, Helvetica, sans-serif; color: #000; display: block; background: url(../images/include/menu.gif); padding: 8px 0 0 30px; } #menu li a:hover { color: #2f5bb7; background: url(../images/include/menu.gif) 0 -32px; padding: 8px 0 0 30px; } /* !END! menu style */ #title_img { position: relative; width: 670px; height: 192px; float: left; } The menu displays just fine, but the images dont appear. Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1276022 Share on other sites More sharing options...
AyKay47 Posted October 5, 2011 Share Posted October 5, 2011 what does a view source show your image paths as? Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1276023 Share on other sites More sharing options...
ferdri3 Posted October 5, 2011 Author Share Posted October 5, 2011 images/image1.png images/image2.png tried: ../images/image1.png ../images/image2.png no difference When I view source in chrome, all php code is gone (even the "<?php include" part).. I don't know if that's normal. Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1276027 Share on other sites More sharing options...
AyKay47 Posted October 5, 2011 Share Posted October 5, 2011 php script does not show in a browser view source.. what directory is the page that you are calling these include from in, in relativity to the image directory.. Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1276030 Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 The PHP will be gone because it is processed before it hits your browser. Anyway, in your code you are assigning the image path to a variable but arent doing anything with it switch (basename($_SERVER['PHP_SELF'])) { case 'test1.php': $image = 'images/image1.png'; break; case 'test2.php': $image = 'images/image2.png'; break; } echo '<img src="', $image , '">'; Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1276032 Share on other sites More sharing options...
ferdri3 Posted October 5, 2011 Author Share Posted October 5, 2011 The PHP will be gone because it is processed before it hits your browser. Anyway, in your code you are assigning the image path to a variable but arent doing anything with it switch (basename($_SERVER['PHP_SELF'])) { case 'test1.php': $image = 'images/image1.png'; break; case 'test2.php': $image = 'images/image2.png'; break; } echo '<img src="', $image , '">'; The echo made it work! HUGE thanks to all of you! Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1276038 Share on other sites More sharing options...
AyKay47 Posted October 5, 2011 Share Posted October 5, 2011 The PHP will be gone because it is processed before it hits your browser. Anyway, in your code you are assigning the image path to a variable but arent doing anything with it switch (basename($_SERVER['PHP_SELF'])) { case 'test1.php': $image = 'images/image1.png'; break; case 'test2.php': $image = 'images/image2.png'; break; } echo '<img src="', $image , '">'; lol... i assumed the img tag was somewhere below the code he gave us.. Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1276039 Share on other sites More sharing options...
ferdri3 Posted October 5, 2011 Author Share Posted October 5, 2011 for if anyone wants to know, here is the text version: <?php switch (basename($_SERVER['PHP_SELF'])) { case 'index.php': $txt = "Line of Text 1"; break; case 'anotherpage.php': $txt = "Line of Text 2"; break; } echo '<h2>', $txt ,'</h2>'; ?> If you dont want the text to be a header, replace: echo '<h2>', $txt ,'</h2>'; with: echo $txt; Quote Link to comment https://forums.phpfreaks.com/topic/248474--/#findComment-1276072 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.