Hello, I need help in PHP conditional statement. I am not PHP programmer. I have a site which is coded in PHP. Footer of the site is common for all the 200+ pages. To call footer in page below code has been used.... <? include('footer.php'); ?> Code (markup): Now, what i want is different link on different pages. For example.... if url is http://website.com/buy_short_jeans.php then link in footer should be a link like <a href="http://website.com/buy_short_jeans.php">Buy Short Jeans</a> What i have figured out is if-elseif can be used for that. See below code... <?php $url; if ($url == "http://website.com/buy_short_jeans.php")Â {Â php coding that makes hyperlink } elseif ($url == "http://website.com/buy_short_skirts.php")Â { php coding that makes hyperlink } else { hyperlink for main domain } ?> Code (markup): can you plz help me to finish doing that???
Well firstly, that variable $url is doing nothing. If this were me, I'd use something unique to the page from the database to perform this - that way, you type the code in once and it works for every page like a set tag or even the url itself. But if I'm understanding you right, Put this: $url = http://website.com/buy_short_jeans.php in the body of your page and then: echo $url in the footer include. The variable is defined in the page and the footer will read the variable from the page when it's browsed. I haven't tested this, but in theory it should work.
Tested this and it works. I've also developed it a little for you: Page 1 Page 2 Footer Page 1: $url = $_SERVER['PHP_SELF']; $title = 'page 1'; include('footer.php'); Code (markup): Page 2: $url = $_SERVER['PHP_SELF']; $title = 'page 2'; include('footer.php'); Code (markup): Footer: echo '<a href="'.$url.'">'.$title.'</a>'; Code (markup): If you browse Page 1, the link will read Page 1 and link to page 1, if on page 2 it will read page 2 and link to page 2. And mostly automated too. Enjoy.
I did that in this way.... <a href="<? $url = $_SERVER['SERVER_NAME']; $file = $_SERVER['REQUEST_URI']; echo "http://".$url.$file; ?> " title=" <? if ( $url.$file == "www.website.com") { echo "Buy short jeans skirts from website.com"; } elseif ( $url.$file == "www.website.com/buy_short_jeans.php") { echo "Buy short jeans online"; } elseif ( $url.$file == "www.website.com/buy_short_skirts.php") { echo "Buy short skirts online"; } else { echo "Buy jeans"; } ?> "> <? if ($url.$file == "www.website.com/buy_short_jeans.php"){ echo "Buy Short Jeans"; } elseif ( $url.$file == "www.website.com/buy_short_skirts.php") { echo "Buy short skirts"; } else { echo "Buy short"; } ?> Code (markup):
i included the code in footer.php kjb: Thank for the help. Is there a way to fetch title of the page?
You could fetch the contents of the current page using file_get_contents(), and then do a regular experession to get the text inside the <title> tags. Or you could do something inside your header.php which just assigns the title to a variable $title or something, and then use it in your footer.