View Full Version : Actions for includes in php
proxyMX
07-28-2002, 05:40 PM
hi there,
i was wondering if there was any way i could simply use one page for includes, instead of having to save the normal way.
Normally youd alter the includes code to the specific page eg
index.php's includes would be indexinc.php,navigation.php,extras.php
news includes would be
newsinc.php, navigation.php,extras.php
Basically what i want to do is for every inc page of my site create an action from the main template that would display the unique included page to each
eg
index.php?action=news
would include newsinc.php and the navigation and extras files
index.php?action=freebies
would include freeinc.php and the navigation and extras files
anyway i can do this? im a total newbie to php
Xenon
07-28-2002, 11:44 PM
not sure if i understand you the right way...
try this code:
if(isset($action) and trim($action)!="") {
include($action."inc.php");
}
include("navigation.php");
include("extras.php");
proxyMX
07-29-2002, 01:39 AM
Xenon, Lets have an affair, your an asset
So i would simply create a full compliment of links and type in their function? using
if
ifelse
if else
if else
if else?
it works tops, Im such a newb
By the way Xenon, could you tell me a good resource OTHER than php.net for tutorials
btw sorry for the triple post :eek:
proxyMX
07-29-2002, 01:49 AM
Hmm nope, having problems now, can you tell me whats up with this code?
<? if(isset($action) and trim($action)!="home") {
include($action."main.php");
}
if else(isset($action) and trim($action)!="legal") {
include($action."legal.php");
}
?>
It's elseif not if else and I would just do...
<?
if ($action == "home") {
include ("main.php");
} elseif ($action == "legal") {
include ("legal.php");
}
?>
or you could just try
<?
include ("$action.php")
?>
and just make sure the $action is the same name as the page your trying to include
proxyMX
07-29-2002, 04:28 AM
Doh! Shows my talent in the php area then :P
proxyMX
07-29-2002, 04:32 AM
by the way, i know the if and elseif statements and the include function, but can people explain actions to me please? and is it possible to set the include main action as the default
what do u mean actions? you could try www.php.net, but i dunno what you mean by actions.
proxyMX
07-29-2002, 04:52 AM
Thanks for the update, its running more efficently now,
proxyMX
07-29-2002, 04:58 AM
what i meab by actions is like
index.php?action=forums
or is that simply a user defined function?
$action is something that PHP automatically does for you, when you do URL.php?action=WHATEVER...it doesn't have to be action either it could be URL.php?url=WHATEVER then you would just replace $action with $url, anyways this is what I do...
print "<a href=\"index.php?action=forums\">Forums</a>"
print "<a href=\"index.php?action=news\">News</a>"
then do on index.php
if ($action == "forums") {
header ("Location: h ttp://www.URL.com/forum") //redirects you to the URL
} elseif ($action == "news") {
include ("news.php") //includes the page
}
take out the space between the h and the ttp: hopes this helps.
proxyMX
07-29-2002, 01:49 PM
i used this to set the homepage as a default. However its not as efficent as id like. You see the problem im having is that i want to set the action of main
if ($action == "home") {
include ("http://www.proxymx.com/main.php");
as the loaded include when you load index.php
so i set it out like so
if ($action == "home") {
include ("http://www.proxymx.com/main.php");
} elseif ($action == "legal") {
include ("http://www.proxymx.com/legal.php");
}
else include ('http://www.proxymx.com/main.php');
However, this presents a problem, if a page isnt found on request, it simply directs the user back to the mainpage.
What i want to do is when a user enters the site {index.php} i want them to see the main.php page included. And then use actions such as ?action=forums for other pages
the code i used is sufficent to do so, however if ever a page is not found, it simply returns to the mainpage, when i want it to display the standard error pages if a page is not found.
So in short what i want to do is set the main.php as the default include when a user loads index.php, However if they type the wrong action i want the standard error messages to show up
Well you could try this
if ($action == "home") {
include ("http://www.proxymx.com/main.php");
} elseif ($action == "legal") {
include ("http://www.proxymx.com/legal.php");
} else{
include ("404.html");
then you just upload the 404.html (or .htm i dunno) from you comp to the site and that's what will get displayed if the page isn't found. if you can't find the 404.html just type in a random URL and when the 404 error comes up just go File/Save As...
proxyMX
07-30-2002, 11:08 PM
defeats the purpose :P
no it doesn't because it's saying if it doesn't find the page display an error, it just takes a little bit more work and it does exactly what u wanted it to do.
proxyMX
07-31-2002, 07:14 PM
But then there would be no way to set the home as a deafult
Then try this
if ($action == "home") {
include ("http://www.proxymx.com/main.php");
} elseif ($action == "") {
include ("http://www.proxymx.com/main.php");
} elseif ($action == "legal") {
include ("http://www.proxymx.com/legal.php");
} else{
include ("404.html");
there, i believe that solves your problem.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.