.evo. Posted December 29, 2008 Share Posted December 29, 2008 Ok so I've seen a bunch of tutorials and stuff on using if/else statements and $_GET variables. And I've developed my own dynamic content code, so that if the URL is "index.php?act=test" it would show "content/test.php", but if it didn't exist it would show a 404 error. I had that working fine and everything, but then I tried to make it so if the act= part wasn't set, it would automatically show "content/home.php", and this is where I got stuck. I cannot get it to work no matter what I do. If I get the main function (content + 404 error) of the code to work then the home part will not work, and when I edited the code to try and make it work, it gave the 404 error no matter what. EDIT: Also, it's not a syntax error or anything, I have validated the code using phpDesigner, it's just not functioning properly. // this was a solution to a problem on another forum so I thought it wouldn't hurt to add it setlocale(LC_ALL, 'en_US.UTF8'); $idx = "/index.php"; $cur = $_SERVER['REQUEST_URI']; $act = $_GET['act']; $filename = "content/$act.php"; if (is_null($act)){ $act = "home"; } function show_content(){ if (file_exists($filename)){ include($filename); } else { include("content/404.php"); } } This was in my "functions.php" which I include on my "index.php", then where I want my content I just put "show_content()". This is my first serious PHP code so I thought I might share with more experienced coders. Any help or suggestions will be great. Link to comment https://forums.phpfreaks.com/topic/138724-solved-ripping-my-hair-out-over-this-mis-functional-code/ Share on other sites More sharing options...
redarrow Posted December 29, 2008 Share Posted December 29, 2008 404 error been discussed a lot, and it come to the opinion off many programmers that a error message produced by the programmer is the way forward. Link to comment https://forums.phpfreaks.com/topic/138724-solved-ripping-my-hair-out-over-this-mis-functional-code/#findComment-725254 Share on other sites More sharing options...
kenrbnsn Posted December 29, 2008 Share Posted December 29, 2008 You need to pass the value of "$filename" to your function. Variables inside a function are local in scope unless they are set to GLOBAL. Also, you're setting the value of $filename before checking for null. Try this: <?php function show_content($act){ $f = (file_exists("content/$act.php"))?"content/$act.php":"content/404.php" include($filename); } $idx = "/index.php"; $cur = $_SERVER['REQUEST_URI']; $act = (isset($_GET['act']))?$_GET['act']:'home'; show_content($act); ?> Ken Link to comment https://forums.phpfreaks.com/topic/138724-solved-ripping-my-hair-out-over-this-mis-functional-code/#findComment-725295 Share on other sites More sharing options...
.evo. Posted December 29, 2008 Author Share Posted December 29, 2008 You had a syntax error in that code, which I fixed, lol. That code shows nothing at all for the content, not even a 404. ??? Link to comment https://forums.phpfreaks.com/topic/138724-solved-ripping-my-hair-out-over-this-mis-functional-code/#findComment-725307 Share on other sites More sharing options...
kenrbnsn Posted December 29, 2008 Share Posted December 29, 2008 That's because this line <?php include($filename); ?> should be <?php include($f); ?> Sorry... Ken Link to comment https://forums.phpfreaks.com/topic/138724-solved-ripping-my-hair-out-over-this-mis-functional-code/#findComment-725319 Share on other sites More sharing options...
.evo. Posted December 29, 2008 Author Share Posted December 29, 2008 Works a charm! Thank you very much, Ken. Link to comment https://forums.phpfreaks.com/topic/138724-solved-ripping-my-hair-out-over-this-mis-functional-code/#findComment-725323 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.