PHP Question


Read With Formatting | Free Open Source Tutorials Account

PHP Development
Thread: PHP Question


tunnelvision
Alright Guys!

Ive got a question that i like answered. How do you get multiply pages on one page. For example you have the about us and contact us on one page and when you click the about us link the URL bar looks like this general.php?id=about. If any one can tell me how to do this or tell me some tutorials that will help me along my way i will be most grateful.

Cheers
<!--tv

md_doc
Well there are a few different ways of doing it. You can do a


switch($_GET['id']) {
case 'about':
my_function();
break;
default;
my_default_page();
break:
}


You could use the switch statement to include different files as well instead of just calling functions.

I, however, suggest that you don't use a single page to show multiple pages because it is not very search engine friendly and not very user friendly either. You really think a person wants to type in some_php_page.php?id=about to get to your about page when they can just type in about.php?

There are much better ways to do templating or reuse code than having everything in one file. It is, however, good to know how to do it so it was a very good question!

tunnelvision
There are much better ways to do templating or reuse code than having everything in one file.


How would u do this? :confused: Would the PHP code that you will reuse go into one PHP file on the server and the pages would refer to that PHP file with the code in?

Cheers
<!--tv

md_doc
At minimum you should have a file that stores functions you use often on your site. I personally use classes and a lot of Object Oriented Programming to reuse code but that confuses a lot of people.

If you are just getting started you can jump in by buying a book, however, if you do not want to go the buying a book route then you should probably just create a file called functions.php which you put all your generic functions in. Maybe you have a displayItem($id) (if you are doing a ecommerce type site).



function displayItem($id)
{
// do database connection

$sql = "SELECT id, name, price FROM products WHERE id=$id";

$results = query_array($sql);

return $results;

}



You would then include this file in all your files



include_once(functions.php);

// all your code for the page


This is a simple and old fasion way of reusing code so you do not have to have the function in all your files. You could make a header() and footer() function for display. As your site grows you might want to have display_functions.php and database_functions.php.

Again the ideal way is to use OOP but it is a little more complicated for some people to grasp. But if you are learning you might as well just learn it right off the bat as it is really not that difficult.

I hope this helps!

tunnelvision
Thanks it helps me alot. You answered some questions that where confusing me. I bought a PHP book 'SAMs Teach Yourself PHP' about 2 weeks ago and it seems to have teached me alot (but not enough :D) and i will definitely look into that OOP.


Thanks for your time
<!-- tunnelvision -->

md_doc
Well I am certain glad I could be of some help. If you have any more question please let us know. Books will help a lot but sometimes they are confusing and someone explaining it in a different way might help so you can always copy the code from the book and ask us what it really means or if there is a better way to do it.

Good luck with PHP!

tunnelvision
cheers m8 will do!