PlagueInfected Posted August 31, 2009 Share Posted August 31, 2009 im trying to code this little script to where if you're on this page, you echo this and if you're on that page you echo that text. here is the current code i tried and no luck with it, any ideas? <title> <?php $page = array(); $page = explode("/", $_SERVER['PHP_SELF']); $directory_no = count($page) - 2; $directory = $page[$directory_no]; define (DIRECTORY, $directory); if (DIRECTORY == '/') { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } elseif (DIRECTORY == '/?pg=index') { echo 'Plague Infected | Index'; } elseif (DIRECTORY == 'contact.php') { echo 'Plague Infected | Contact'; } elseif (DIRECTORY == 'portfolio.php') { echo 'Plague Infected | Portfolio'; } else { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } ?> </title> Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/ Share on other sites More sharing options...
mattal999 Posted August 31, 2009 Share Posted August 31, 2009 Well firstly try echoing DIRECTORY to see what you get. Tell us what the output is. Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909630 Share on other sites More sharing options...
PlagueInfected Posted August 31, 2009 Author Share Posted August 31, 2009 it just echo 'inc' idk how to get it to do what it's supposed to do Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909645 Share on other sites More sharing options...
Mardoxx Posted August 31, 2009 Share Posted August 31, 2009 http://www.mysite.com/index.php $_SERVER['PHP_SELF'] => /index.php $page = explode("/", $_SERVER['PHP_SELF']); $page[1] = index.php http://www.mysite.com/ $_SERVER['PHP_SELF'] => / $page = explode("/", $_SERVER['PHP_SELF']); $page = try removing your slashes Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909647 Share on other sites More sharing options...
Mardoxx Posted August 31, 2009 Share Posted August 31, 2009 can you give the URL to where this is being called/run from? because $directory_no = count($page) - 2; comes out negative if it's run at server root Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909648 Share on other sites More sharing options...
PlagueInfected Posted August 31, 2009 Author Share Posted August 31, 2009 done it, still doesn't echo the right text on the page i want it to echo on Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909650 Share on other sites More sharing options...
mattal999 Posted August 31, 2009 Share Posted August 31, 2009 I think you want to replace this: $directory_no = count($page) - 2; With: $directory_no = count($page); Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909772 Share on other sites More sharing options...
PlagueInfected Posted August 31, 2009 Author Share Posted August 31, 2009 just tried it and no luck =/ Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909774 Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 $page = array(); $page = explode("/", $_SERVER['PHP_SELF']); $directory_no = count($page) -1; $directory = $page[$directory_no]; this line: $directory_no = count($page) - 2; should be -1, because you want to get the last entry in your exploded array (which would be the page itself) -2 will give you the second to last, and in many cases, that is also the first, and sometimes it will become -1. Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909776 Share on other sites More sharing options...
PlagueInfected Posted August 31, 2009 Author Share Posted August 31, 2009 still doesn't change the tags, tried adding -1 to it and no joy Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909778 Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 echo the directory variable (both your constant and the variable itself) and see what happens. Perhaps the variable isn't the value you expect it to be Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909780 Share on other sites More sharing options...
PlagueInfected Posted August 31, 2009 Author Share Posted August 31, 2009 just adds -1 to the text on the first page, and the tags still don't change when i jump to another page Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909785 Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 wait so when you echo the variable it just shows "-1" at the end of the word? what does your code look like now Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909787 Share on other sites More sharing options...
PlagueInfected Posted August 31, 2009 Author Share Posted August 31, 2009 like this... <title> <?php $page = array(); $page = explode("/", $_SERVER['PHP_SELF']); $directory_no = count($page); $directory = $page[$directory_no] -1; if ($directory == '/') { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } elseif ($directory == '/?pg=index') { echo 'Plague Infected | Index'; } elseif ($directory == 'contact.php') { echo 'Plague Infected | Contact'; } elseif ($directory == 'portfolio.php') { echo 'Plague Infected | Portfolio'; } else { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } ?> </title> Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909788 Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 And it doesn't work for any of the pages? When you echoed them what did they say? oh and the URL won't look like this : /?pg=index index.php in that case. But the others should be fine. try also doing a print_r on $page and see what it holds Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909791 Share on other sites More sharing options...
PlagueInfected Posted August 31, 2009 Author Share Posted August 31, 2009 it will echo Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico and i added my site's URL and it still gave me the first tag print_r doesn't seem to do it, here is how i coded it <?php $page = array(); $page = print_r("/", $_SERVER['SCRIPT_NAME']); $directory_no = count($page); $directory = $page[$directory_no] -1; if ($directory == 'http://plagueinfected.com/') { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } elseif ($directory == 'http://plagueinfected.com/?pg=index') { echo 'Plague Infected | Index'; } elseif ($directory == 'http://plagueinfected.com/contact.php') { echo 'Plague Infected | Contact'; } elseif ($directory == 'http://plagueinfected.com/portfolio.php') { echo 'Plague Infected | Portfolio'; } else { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909792 Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 this is not going to work. $_PHPSELF returns the page your are currently on, sans any paths to the document itself. So if i'm on www.facebook.com/books/awesome.php and I echo $_SERVER['PHP_SELF'] it will echo "/awesome.php" You are trying to test the variable against a full URL and it will never ever match those. none of those will work. And Echo the variable itself. As in change your code to $page = array(); $page = print_r("/", $_SERVER['SCRIPT_NAME']); $directory_no = count($page); $directory = $page[$directory_no] -1; print_r($page); echo $directory; if ($directory == 'http://plagueinfected.com/') { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } elseif ($directory == 'http://plagueinfected.com/?pg=index') { echo 'Plague Infected | Index'; } elseif ($directory == 'http://plagueinfected.com/contact.php') { echo 'Plague Infected | Contact'; } elseif ($directory == 'http://plagueinfected.com/portfolio.php') { echo 'Plague Infected | Portfolio'; } else { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } that will tell you what the actual variable itself looks like, and what you should change your test too Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909795 Share on other sites More sharing options...
PlagueInfected Posted August 31, 2009 Author Share Posted August 31, 2009 tried it again, it only pulls -1 than the tag 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico' so it looks like this '/-1lague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico' $page = array(); $page = print_r("/", $PHP_SELF); $directory_no = count($page); $directory = $page[$directory_no] -1; print_r($page); echo $directory; if ($directory == 'http://plagueinfected.com/') { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } elseif ($directory == 'http://plagueinfected.com/?pg=index') { echo 'Plague Infected | Index'; } elseif ($directory == 'http://plagueinfected.com/contact.php') { echo 'Plague Infected | Contact'; } elseif ($directory == 'http://plagueinfected.com/portfolio.php') { echo 'Plague Infected | Portfolio'; } else { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909798 Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 oh dude my bad. I'm an idiot $page = array(); $page = print_r("/", $_SERVER['SCRIPT_NAME']); $directory_no = count($page) - 1; $directory = $page[$directory_no]; subtracted from the wrong variable. try that, with the echos and see what happens Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909801 Share on other sites More sharing options...
PlagueInfected Posted August 31, 2009 Author Share Posted August 31, 2009 just gives me // and Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico //Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909803 Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 oh didn't notice something in your code above when I said print_r I meant print_r($page); not to do $page = print_r("/", $PHP_SELF); that is messing up your variables. change it back to $page = explode("/", $PHP_SELF); or whatever it was before Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909805 Share on other sites More sharing options...
PlagueInfected Posted August 31, 2009 Author Share Posted August 31, 2009 ok did what u did and i got this <title> Array ( [0] => [1] => inc [2] => head.php ) head.phpPlague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico</title> Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909806 Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 ok thats good. you can get rid of the echos and stuff. Just change your if statements accordingly, like for that page it should be if ($directory == "head.php"){ echo "whatever"; } Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909807 Share on other sites More sharing options...
PlagueInfected Posted August 31, 2009 Author Share Posted August 31, 2009 ok done it still echos the first line dont know if i did it right with the calling <?php $page = array(); $page = explode("/", $PHP_SELF); $directory_no = count($page) - 1; $directory = $page[$directory_no]; if ($directory == '/') { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } elseif ($directory == 'index') { echo 'Plague Infected | Index'; } elseif ($directory == 'contact.php') { echo 'Plague Infected | Contact'; } elseif ($directory == 'portfolio.php') { echo 'Plague Infected | Portfolio'; } else { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909808 Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 elseif ($directory == 'index') { needs to be index.php if ($directory == '/') { doesn't mean anything and will never actually run true. You are only seeing this line because it is also echoed in the else clause. I would set it up like this: <?php $page = array(); $page = explode("/", $PHP_SELF); $directory_no = count($page) - 1; $directory = $page[$directory_no]; if ($directory == 'index.php') { echo 'Plague Infected | Index'; } elseif ($directory == 'contact.php') { echo 'Plague Infected | Contact'; } elseif ($directory == 'portfolio.php') { echo 'Plague Infected | Portfolio'; } else { echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/172556-echo-text-based-on-the-url/#findComment-909811 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.