View Full Version : PHP, and all in one
joshg678 06-24-2003, 08:24 AM I was wondering if anyone know how to make a php webpage work in one page.
What im looking for is for me to have a layout, and if the url is http://www.hi.com/index.php it would display the main page, and if it was http://www.hi.com/index.php?me=dnlds it would show a different section. So basicalley i want a layout (wich i have) and can use if commands and else commands that will work togather with one uneek look. so this way i don't have to have 6 or 7 different pages, so if i have to change something like in the header or footer i change one page and not 6 or 7.
if you need an example of what im talking about http://www.digital-coaster.com has the same thing, but he won't give me the script.
in PHP, variables sent through the URL (within the query_string) can be accessed within the _GET array.
so
index.php?variable=value
would be passing a single variable=value pair within the query string. the value part can then be re-attributed to the variable by accessing the $_GET array --
echo $_GET['variable'];
would output 'value'
--------------------
so, let's for the sake of argument, decide we are going to call our content reference 'page' - we would then want URLs of the order index.php?page=XXXXX where XXXXX is the part that changes and thus dictates our content.
the function include() will then be used within our index.php script to pull in a subpage named relative to the XXXXX part.
so somewhere within your index.php (where the main content will go) you would want something like
<?php
$content_folder = 'my_pages/';
// always best to pull content from a subfolder - for security reasons
$inc_ref = (isset($_GET['page']) && $_GET['page'] !== "") ? $_GET['page'] : 'default' ;
// that just checks if the page=xxxx exists in the url and sets a default if it doesn't
if(file_exists($content_folder.$inc_ref. '.php'))
{
include($content_folder.$inc_ref. '.php');
// include my_pages/xxxxx.php if it exists
}
else
{
echo 'page not found, oops';
}
that will check the query string for page=whatever and then look in the folder my_pages/ to see if a file with the 'whatever' value .php exists. if it does it will include it, else output an error message.
subnote: remove the linebreak from the if(file_exists line.
joshg678 06-25-2003, 01:14 AM hmmm. Well you've kinda lost me there a bit.
joshg678 06-25-2003, 04:49 AM OHHHH. i got it now, thatnks i'll try it out.
now what do you mean about the line break?
sorry about the 2 posts, i could not edit the last one.
Dude128 06-25-2003, 05:11 AM where it says:
if(file_exists($content_f
older.$inc_ref. '.php'))
in the code, put that all on one line- the forum for some reason tends to break some things up.
joshg678 06-25-2003, 05:29 AM ahhh. thank you both.
joshg678 06-26-2003, 01:35 AM ok thank you soooo much, but i was wondering, how would i set it up to display something else, like if someone types index.php?page=hello and that is not there, have is display the page not found, but if they just type in index.php have it desplay something like welcome.
Dude128 06-26-2003, 02:58 AM just read the code ;)
note the comments (the lines that begin with //), as they tell you what the code is doing. you'll notice that "checks if the page=xxxx exists in the url and sets a default if it doesn't"- and if you look at the code you'll see that one line will "// include my_pages/xxxxx.php if it exists" else { ... do something ... } (in this case, the code prints "page not found, oops". you could instead include an error page, or do something else.
joshg678 06-26-2003, 04:07 AM umm, yah, my head hirts.
But i think i can get it, i hope, i'll come back if i need help, but also, can't i add both a error page and a main page if the url is just index.php?
joshg678 06-28-2003, 09:01 PM ok could i add the following before the error?
{
default: echo 'Here is the default of index.php?';
}
ok, this might make it a tad easier to understand as it always includes a page and just uses quite simple logic to deduce which page.
$content_folder = 'my_pages/';
if(!isset($_GET['page']) || $_GET['page'] == "")
{
// no page=xxxx in url, so set default
$inc_ref = 'default';
}
if(!file_exists($content_folder.$inc_ref. '.php'))
{
// no file found to include, set inc_ref to page_not_found
$inc_ref = 'page_not_found';
}
// include the page (note that I've used .php extensions for them)
include($content_folder.$inc_ref. '.php');
basically - it first checks the url for page=xxxxx - if it doesn't find anything it sets the include_reference ($inc_ref) value to 'default'
it then checks to see if a file exists in the named subfolder (with a .php extension (eg folder/default.php - folder/url_value.php etc)) - if it cannot find a file, it resets the reference to 'page_not_found'
it then includes the referenced file
folder/default.php = if no value found in url
folder/page_not_found.php = if no file found
folder/xxxxxxxxx.php = if url value is good and if file is found.
hope that helps anyway.
SchitzophreniiC 06-29-2003, 06:47 PM <?php
if(isset($_GET['news'])) {
include 'news.php';
}
if(isset($_GET['forums'])) {
include '/forums/index.php';
}
?>
thats the code if you were wondering how people have:
http://www.theirsite.com/?news
http://www.theirsite.com/?forums
joshg678 06-30-2003, 04:02 AM thanks you all, i'll try it tomarrow.
joshg678 07-01-2003, 03:54 AM hey okii,
your script was wrong, but i got it to work, here is what works.
<?
$content_folder = "my_pages/";
if(!isset($_GET['page']) || $_GET['page'] == ""){
$inc_ref = "default";
}
else{
$inc_ref = $_GET['page'];
}
if(!file_exists($content_folder.$inc_ref.".php")){
$inc_ref = "page_not_found";
}
include($content_folder.$inc_ref.".php");
?>
also if you need to do it with images use this one:
<?
print "<html>\n";
print "<body>\n";
$i = $_GET['img'];
if(!isset($i)){
print "No Image Specified";
}
else{
if(file_exists($i)){
print "<img src=\"$i\" border=\"0\">\n";
}
else{
print "Image does not exist";
}
}
print "</body>\n";
print "</html>\n";
?>
hey okii,
your script was wrong, but i got it to work, here is what works.
hehe - well spotted josh ;) forgetting to assign $inc_ref to the $_GET variable was a tad silly - guess I should have tested the code before posting.
glad it's working anyway.
joshg678 07-02-2003, 03:18 AM no problem, and thank you for helping me and i hope other people might want theis script, and i might hire you for some help.
|