|
PHP Include
|
|
Topic Started: Sep 16 2005, 10:06 PM (3,875 Views)
|
|
Veris
|
Sep 16 2005, 10:06 PM
Post #1
|
- Posts:
- 405
- Group:
- Members
- Member
- #21,718
- Joined:
- June 17, 2004
|
Alright, I need some help here, I have coded my website in PHP before, but now several months later i am having some trouble. I know the basic php include script, but I need some other help.
If this is the PHP Include:
- Quote:
-
<?php include('ssi2.php'); ?>
I need to link my pages like this:
- Quote:
-
<a href="roster.php?x=roster">
But I cant find the code that defines where "X" goes on the index.php. Because if I remember correctly a code is first placed where you want different pages to go, something that says...x=page, or something ,lol, and then in the area where you placed x=page (yea i made that up) is where your roster.html will go, with the link <a href="roster.php?page=roster">. Can someone help me out here. Thanks,
Edit: is it anything like this:
if($page) { include "$page"; } else { $page = "blank.html"; include "$page"; }
Edit: or something like this??
<? $page = "$id"; include("$page"); ?>
-illuminatie
|
|
|
| |
|
Das
|
Sep 17 2005, 12:02 AM
Post #2
|
- Posts:
- 7,114
- Group:
- Members
- Member
- #34,712
- Joined:
- November 10, 2004
|
<?php if ($_GET['page'] == "") { $page = "index.php"; } else { $page = $_GET['page'] . ".php"; } include($page); ?>
If you went to index.php?page=blah it would load blah.php ... or it should ...
|
|
|
| |
|
FearKiller
|
Sep 17 2005, 02:25 AM
Post #3
|
- Posts:
- 1,257
- Group:
- Members
- Member
- #1,769
- Joined:
- May 21, 2003
- I'm Browsing With
- Firefox
|
- Code:
-
$valid = array(); $valid[] = 'page1'; $valid[] = 'page2'; $valid[] = 'page3';
if(in_array($_GET['page'], $valid)) { include($_GET['page'] . '.php'); } else { include('index.php'); }
This will prevent users from getting errors because PHP will be trying to include files that don't exist. Just input all your valid files into the "$valid" array to display them. If the page is not in the array or if no page is set, then "index.php" will be displayed by default.
|
|
|
| |
|
Das
|
Sep 17 2005, 02:55 AM
Post #4
|
- Posts:
- 7,114
- Group:
- Members
- Member
- #34,712
- Joined:
- November 10, 2004
|
- FearKiller
- September 16, 2005 11:25 PM
- Code:
-
$valid = array(); $valid[] = 'page1'; $valid[] = 'page2'; $valid[] = 'page3';
if(in_array($_GET['page'], $valid)) { include($_GET['page'] . '.php'); } else { include('index.php'); }
This will prevent users from getting errors because PHP will be trying to include files that don't exist. Just input all your valid files into the "$valid" array to display them. If the page is not in the array or if no page is set, then "index.php" will be displayed by default.
of to keep it automagic:
- Quote:
-
$page = $_GET['page']; $full_page = $_GET['page'] . ".php"; if ($page == "") { include("index.php"); } else { if(file_exists($page)) { include($full_page); } else { include("index.php"); } }
You can also change what's in red to something like error.php
|
|
|
| |
|
FearKiller
|
Sep 17 2005, 09:58 AM
Post #5
|
- Posts:
- 1,257
- Group:
- Members
- Member
- #1,769
- Joined:
- May 21, 2003
- I'm Browsing With
- Firefox
|
Hmm, I was unaware of the file_exists() function.
*FearKiller learns something new 
There is a slight error in your code though. Your using the variable $page in file_exists() when you should be using $full_page I believe.
- Code:
-
if(!file_exists($_GET['page'] . '.php')) { include('index.php'); } else { include($_GET['page'] . '.php'); }
There's no need to check if the variable is set or empty because if it is, then the file doesn't exist. There's going to be 500 different ways to do this by the time me and Das are done.
|
|
|
| |
|
Das
|
Sep 17 2005, 01:26 PM
Post #6
|
- Posts:
- 7,114
- Group:
- Members
- Member
- #34,712
- Joined:
- November 10, 2004
|
- FearKiller
- September 17, 2005 06:58 AM
There is a slight error in your code though. Your using the variable $page in file_exists() when you should be using $full_page I believe.
There's no need to check if the variable is set or empty because if it is, then the file doesn't exist.
:$ I don't know what I was thinking last night.
|
|
|
| |
|
The Legendary Sam
|
Sep 18 2005, 09:11 PM
Post #7
|
- Posts:
- 205
- Group:
- Members
- Member
- #31,847
- Joined:
- October 7, 2004
|
include() Copies any text/html/js/php/etc... into the file that the include() command lies on.
require() If an include fails, it will give you a warning and continue executing script. If require() fails, it will stop execution and give a fatal error.
require_once() Works exactly like require(), except the file used to include via require_once() can only be included once in that script. Attempting to include it again will result in a fatal error.
Another method to do this is to use switch statements.
- Quote:
-
Originally posted by Das Ein:
<?php if ($_GET['page'] == "") { $page = "index.php"; } else { $page = $_GET['page'] . ".php"; } include($page); ?>
A better way to do that is:
- Code:
-
<?php $page = $_GET['page']; if(!isset($_GET['page']) && $_GET['page'] != '') { $_GET['page'] = 'index.php'; } else { $page = $_GET['page'].'.php'; } include($page); ?>
Switch is the best command to use in this case.
- Code:
-
<?php $id = $_GET['id']; switch($_GET['id']) { case 1: include('page1.php'); break; case 2: include('page2.php'); break; case 3: include('page3.php'); break; default: include('index.php'); } ?>
You can also go: case('blah'): include('blah.php'); break; default executes when $id is empty or if $id is not specified by a case.
|
|
|
| |
|
FearKiller
|
Sep 18 2005, 10:09 PM
Post #8
|
- Posts:
- 1,257
- Group:
- Members
- Member
- #1,769
- Joined:
- May 21, 2003
- I'm Browsing With
- Firefox
|
- Quote:
-
Switch is the best command to use in this case.
Not if you have many options to choose from.
- Code:
-
<?php $id = $_GET['id']; switch($_GET['id']) { case 1: include('page1.php'); break; case 2: include('page2.php'); break; case 3: include('page3.php'); break; case 4: include('page4.php'); break; case 5: include('page5.php'); break; case 6: include('page6.php'); break; case 7: include('page7.php'); break; case 8: include('page8.php'); break; case 9: include('page9.php'); break; case 10: include('page10.php'); break; case 11: include('page11.php'); break; case 12: include('page12.php'); break; case 13: include('page13.php'); break; case 14: include('page14.php'); break; case 15: include('page15.php'); break; case 16: include('page16.php'); break; case 17: include('page17.php'); break; case 18: include('page18.php'); break; case 19: include('page19.php'); break; case 20: include('page20.php'); break; default: include('index.php'); } ?>
|
|
|
| |
|
The Legendary Sam
|
Sep 18 2005, 10:15 PM
Post #9
|
- Posts:
- 205
- Group:
- Members
- Member
- #31,847
- Joined:
- October 7, 2004
|
Oh please. That's child's play.
- Code:
-
<?php require('admincp/content.php'); switch($_GET['type']) { case('page'): switch($_GET['id']) { case 1: print($Page_ID_1_Content); break; case 2: print($Page_ID_2_Content); break; case 3: print($Page_ID_3_Content); break; case 4: print($Page_ID_4_Content); break; case 5: print($Page_ID_5_Content); break; case 6: print($Page_ID_6_Content); break; case 7: print($Page_ID_7_Content); break; case 8: print($Page_ID_8_Content); break; case 9: print($Page_ID_9_Content); break; case 10: print($Page_ID_10_Content); break; case 11: print($Page_ID_11_Content); break; case 12: print($Page_ID_12_Content); break; case 13: print($Page_ID_13_Content); break; case 14: print($Page_ID_14_Content); break; case 15: print($Page_ID_15_Content); break; case 16: print($Page_ID_16_Content); break; case 17: print($Page_ID_17_Content); break; case 18: print($Page_ID_18_Content); break; case 19: print($Page_ID_19_Content); break; case 20: print($Page_ID_20_Content); break; case 21: print($Page_ID_21_Content); break; case 22: print($Page_ID_22_Content); break; case 23: print($Page_ID_23_Content); break; case 24: print($Page_ID_24_Content); break; case 25: print($Page_ID_25_Content); break; default: print($Page_ID_1_Content); break; } break; case('content'): switch($_GET['id']) { case 1: print($Content_ID_1_Content); break; case 2: print($Content_ID_2_Content); break; case 3: print($Content_ID_3_Content); break; case 4: print($Content_ID_4_Content); break; case 5: print($Content_ID_5_Content); break; case 6: print($Content_ID_6_Content); break; case 7: print($Content_ID_7_Content); break; case 8: print($Content_ID_8_Content); break; case 9: print($Content_ID_9_Content); break; case 10: print($Content_ID_10_Content); break; case 11: print($Content_ID_11_Content); break; case 12: print($Content_ID_12_Content); break; case 13: print($Content_ID_13_Content); break; case 14: print($Content_ID_14_Content); break; case 15: print($Content_ID_15_Content); break; case 16: print($Content_ID_16_Content); break; case 17: print($Content_ID_17_Content); break; case 18: print($Content_ID_18_Content); break; case 19: print($Content_ID_19_Content); break; case 20: print($Content_ID_20_Content); break; case 21: print($Content_ID_21_Content); break; case 22: print($Content_ID_22_Content); break; case 23: print($Content_ID_23_Content); break; case 24: print($Content_ID_24_Content); break; case 25: print($Content_ID_25_Content); break; default: print($Content_ID_1_Content); break; } break; case('redirect'): switch($_GET['id']) { case 1: print('<script>location.href="'.$Redirect_ID_1.'";</script>'); break; case 2: print('<script>location.href="'.$Redirect_ID_2.'";</script>'); break; case 3: print('<script>location.href="'.$Redirect_ID_3.'";</script>'); break; case 4: print('<script>location.href="'.$Redirect_ID_4.'";</script>'); break; case 5: print('<script>location.href="'.$Redirect_ID_5.'";</script>'); break; case 6: print('<script>location.href="'.$Redirect_ID_6.'";</script>'); break; case 7: print('<script>location.href="'.$Redirect_ID_7.'";</script>'); break; case 8: print('<script>location.href="'.$Redirect_ID_8.'";</script>'); break; case 9: print('<script>location.href="'.$Redirect_ID_9.'";</script>'); break; case 10: print('<script>location.href="'.$Redirect_ID_10.'";</script>'); break; case 11: print('<script>location.href="'.$Redirect_ID_11.'";</script>'); break; case 12: print('<script>location.href="'.$Redirect_ID_12.'";</script>'); break; case 13: print('<script>location.href="'.$Redirect_ID_13.'";</script>'); break; case 14: print('<script>location.href="'.$Redirect_ID_14.'";</script>'); break; case 15: print('<script>location.href="'.$Redirect_ID_15.'";</script>'); break; case 16: print('<script>location.href="'.$Redirect_ID_16.'";</script>'); break; case 17: print('<script>location.href="'.$Redirect_ID_17.'";</script>'); break; case 18: print('<script>location.href="'.$Redirect_ID_18.'";</script>'); break; case 19: print('<script>location.href="'.$Redirect_ID_19.'";</script>'); break; case 20: print('<script>location.href="'.$Redirect_ID_20.'";</script>'); break; case 21: print('<script>location.href="'.$Redirect_ID_21.'";</script>'); break; case 22: print('<script>location.href="'.$Redirect_ID_22.'";</script>'); break; case 23: print('<script>location.href="'.$Redirect_ID_23.'";</script>'); break; case 24: print('<script>location.href="'.$Redirect_ID_24.'";</script>'); break; case 25: print('<script>location.href="'.$Redirect_ID_25.'";</script>'); break; default: print('<script>location.href="'.$Redirect_ID_1.'";</script>'); break; } break; default: print($News); if($Site_Status=='offline') { print($Offline_Reason); } } ?>
A lot more where that came from.
|
|
|
| |
|
FearKiller
|
Sep 18 2005, 10:42 PM
Post #10
|
- Posts:
- 1,257
- Group:
- Members
- Member
- #1,769
- Joined:
- May 21, 2003
- I'm Browsing With
- Firefox
|
Which would you think is better?
- Code:
-
<?php if(!file_exists($_GET['page'] . '.php')) { include('index.php'); } else { include('page'. $_GET['page'] . '.php'); } ?>
OR
- Code:
-
<?php $id = $_GET['id']; switch($_GET['id']) { case 1: include('page1.php'); break; case 2: include('page2.php'); break; case 3: include('page3.php'); break; case 4: include('page4.php'); break; case 5: include('page5.php'); break; case 6: include('page6.php'); break; case 7: include('page7.php'); break; case 8: include('page8.php'); break; case 9: include('page9.php'); break; case 10: include('page10.php'); break; ... case 91: include('page91.php'); break; case 92: include('page92.php'); break; case 93: include('page93.php'); break; case 94: include('page94.php'); break; case 95: include('page95.php'); break; case 96: include('page96.php'); break; case 97: include('page97.php'); break; case 98: include('page98.php'); break; case 99: include('page99.php'); break; default: include('index.php'); } ?>
The first method uses 7 lines of code for an infinite number of pages. The second method uses 105 lines of code for only 100 different pages.
The first method doesn't require you to edit the code in order for you to add new pages. Just name the file "pagex.php" where "x" represents the page number and you're all set.
You're gonna tell me that case switching is the best method for this? Please explain.
|
|
|
| |
|
Slay
|
Sep 18 2005, 10:43 PM
Post #11
|
- Posts:
- 695
- Group:
- Members
- Member
- #56,266
- Joined:
- June 1, 2005
|
:blink: So many numbers and letters.... :wacko:
|
|
|
| |
|
The Legendary Sam
|
Sep 18 2005, 10:46 PM
Post #12
|
- Posts:
- 205
- Group:
- Members
- Member
- #31,847
- Joined:
- October 7, 2004
|
You still would have to set the variables for the content. So it really doesn't matter.
|
|
|
| |
|
FearKiller
|
Sep 18 2005, 11:54 PM
Post #13
|
- Posts:
- 1,257
- Group:
- Members
- Member
- #1,769
- Joined:
- May 21, 2003
- I'm Browsing With
- Firefox
|
- The Legendary Sam
- September 18, 2005 09:46 PM
You still would have to set the variables for the content. So it really doesn't matter. 
:huh: It doesn't matter that there's an extra 98 lines of coding?
Wouldn't the content be set in the included files and then be echoed on the page?
Example
page1.php
- Code:
-
<?php $content = array(); $content['This is My Page'] = 'Hello all welcome to my page.'; $content['What is this site about?'] = 'This site is about me and my life|I have lived in such and such for so long.'; $content['Conclusion'] = 'Thanks for visiting my site come back soon'; ?>
main.php
- Code:
-
<?php if(!file_exists($_GET['page'] . '.php')) { include('index.php'); } else { include('page'. $_GET['page'] . '.php'); }
foreach($content as $idx=>$val) { echo "<h1>$idx</h1>\n"; $val = explode('|', $val); foreach($val as $val2) { echo "<p>$val2</p>\n"; } echo "\n"; } ?>
Output from combined files
- Code:
-
<h1>This is My Page</h1> <p>Hello all welcome to my page.</p>
<h1>What is this site about?</h1> <p>This site is about me and my life</p> <p>I have lived in such and such for so long.</p>
<h1>Conclusion</h1> <p>Thanks for visiting my site come back soon</p>
|
|
|
| |
|
Veris
|
Nov 11 2005, 08:35 PM
Post #14
|
- Posts:
- 405
- Group:
- Members
- Member
- #21,718
- Joined:
- June 17, 2004
|
Okay, sorry its been awhile, but thanks for the help , for awhile I was using this code:
- Code:
-
<?php switch($id) { default: include('home.php'); break; case "news": include('news.php'); break; case "forums": include('forums.php'); break; case "potw": include('potw.php'); break; case "downloads": include('downloads.php'); break; case "interviews": include('interviews.php'); break; case "previews": include('previews.php'); break; case "hardware": include('hardware.php'); break; case "software": include('software.php'); break; case "articles": include('articles.php'); break; case "staff": include('staff.php'); break; case "contact": include('contact.php'); break; case "resume": include('resume.php'); break; case "membersarea": include('membersarea.php'); break; case "webhosting": include('webhosting.php'); break; case "gamehosting": include('gamehosting.php'); break; case "webdesign": include('webdesign.php'); break; case "email": include('email.php'); break; case "blink182gh": include('blink182gh.php'); break; case "yourad": include('yourad.php'); }
?>
But then I found that it was becoming annoying to keep putting the different pages into a seperate file, so I'm now using this:
- Code:
-
<?php $x = $_GET['page']; if(!isset($_GET['x']) && $_GET['x'] != '') { $_GET['x'] = 'index.php'; } else { $x = $_GET['x'].'.php'; } include($x); ?>
|
|
|
| |
| 1 user reading this topic (1 Guest and 0 Anonymous)
|