Prodigal Son Posted October 6, 2009 Share Posted October 6, 2009 I'm coding a small database driven site and I have my index.php include the view page and call the database to make queries based on what is in the $_GET variables. So to grab a review with id 10, my url would look like: index.php?view=review&rid=10 I see a lot of sites that would have the url look like this instead: review.php?id=10 Is that a different coding method altogether? To keep it simple with the first method in my index.php I could have: -php stuff -header --include review.php file -footer Since the included files will only carry its own individual content if I need to edit the layout I could just change the index page. However the second method I would need to include a header and footer into the content on all pages? If I make some drastic changes and let's say I wouldn't even have a footer anymore or maybe I would need another sub section for the layout, wouldn't I have to go through and edit all the pages? The first method also seems like it would be easier to separate logic and layout across specific pages by grabbing the view get request if needed. Maybe I'm missing some advantages of the second method? Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 I see a lot of sites that would have the url look like this instead: review.php?id=10 Thats the method that I tend to employ, I prefer the neater looking urls. Though personally I tend to have the file in a review folder so that the url would be http://www.mysite.com/review/?id=10 (and thats when I'm not using 'pretty urls' with mod_rewrite). The code structure is basically the opposite of what you have... -php stuff include 'header.php'; <!-- content of review .php --> include 'footer'; Quote Link to comment Share on other sites More sharing options...
Prodigal Son Posted October 6, 2009 Author Share Posted October 6, 2009 Thats the method that I tend to employ, I prefer the neater looking urls. Though personally I tend to have the file in a review folder so that the url would be http://www.mysite.com/review/?id=10 (and thats when I'm not using 'pretty urls' with mod_rewrite). The code structure is basically the opposite of what you have... -php stuff include 'header.php'; <!-- content of review .php --> include 'footer'; Hmm, using the other way I could easily create a switch statement and have cases for each view and keep stuff like function calls separate from the page. Using this method, how would create a controller type page? Would something like this work: switch (basename($_SERVER['PHP_SELF'], '.php')) { case 'review': run_function($_GET['id']); break; } Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 I don't understand the question. The whole point of the approach is you eliminate the need for the switch statement because each page is it's own independent page. So you have no need of varying the include based on the requested page. Quote Link to comment Share on other sites More sharing options...
Prodigal Son Posted October 6, 2009 Author Share Posted October 6, 2009 I don't understand the question. The whole point of the approach is you eliminate the need for the switch statement because each page is it's own independent page. So you have no need of varying the include based on the requested page. Hmm, the way I learned to code (maybe it was a bad way lol) was that it's better to keep functions separate from the actual page. So let's say I have 5 functions and some simple isset logic for review.php, do you suggest putting those directly into review.php? Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 Generally speaking functions should be kept out of the HTML certainly, infact as much PHP as possible should be kept seperate, but I don't see how the approach changes. You could place it in review.php at the top of the page. If you really wanted to keep it seperate but it in a seperate functions script and simply include the functions script on any pages that require. If the page is specific to a single page though, theres obviously no point it being in a seperate script. Quote Link to comment Share on other sites More sharing options...
Prodigal Son Posted October 6, 2009 Author Share Posted October 6, 2009 Yea, makes sense. I should just put it at the top of the page, I guess I overcomplicated things. Since both ways seem to be able to do the same thing (I originally thought the first way was better) I guess I'll use the second way. Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 There is no real right way, it's all down to personal preference. I must admit I found getting to grips with the best structure for a site far more difficult than I found getting used to PHP syntax. Generally speaking my structure is like so... <?php //- FILE START $title = "Title for page"; $keywords = "keywords for page"; $description = "descrip for page"; ... // etc include 'header.php'; // main markup of page include 'footer.php'; //- FILE END ?> header.php will have includes for settings, mysql_ connections etc, it will also contain the HTML declaration, <head> section and begining of the <body> section. It will also generally include a header image and a site menu. footer.php will contain the actual site footer, close any mysql_ connections, do any other tidying up and close the </body> tag. Quote Link to comment Share on other sites More sharing options...
Prodigal Son Posted October 6, 2009 Author Share Posted October 6, 2009 Yea, I will probably set things up like that except add a config.php file as well. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 Yer, thats essential what my settings include is for Quote Link to comment 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.