Multiple Actions in one file -
By Adrian TrimbleI'm sure you have seen those scripts (blog scripts, forum software etc) which have 70kb .php files. Why? Because they have Multiple functions in each file. I am going to show you one way of doing this.
See a working version of what you will be creating
here.
First create a new .php document and name it multiple.php. Then put the following code in it:
CODE
<?php
$action = $_GET['action'];
if ($action == a) {
echo "You have done action A. <br><a href=multiple.php>Click Here</a> to Select a different Action";
}
elseif ($action == b) {
echo "You have done action B. <br><a href=multiple.php>Click Here</a> to Select a different Action";
}
elseif ($action == c) {
echo "You have done action C. <br><a href=multiple.php>Click Here</a> to Select a different Action";
}
else {
echo "Please choose an action:
<br><a href=multiple.php?action=a>Action A</a>
<br><a href=multiple.php?action=b>Action B</a>
<br><a href=multiple.php?action=c>Action C</a>";
}
?>
This code will work on a server with register_globals off and on.
Now I shall explain the whole script.
CODE
<?php
This opens the php code. You can only write php code inside one of these tags. Before this tag you can put standard HTML.
CODE
$action = $_GET['action'];
This code defines the variable 'action' for servers with register_globals off.
CODE
if ($action == a) {
echo "You have done action A. <br><a href=multiple.php>Click Here</a> to Select a different Action";
}
If variable 'action' is equal to A then do X script. The code between the { and } is the code that will be executed if action is equal to A. In this case the code to execute is to write some text and make a link. You can put any php code inside the curly braces { } though.
CODE
elseif ($action == b) {
echo "You have done action B. <br><a href=multiple.php>Click Here</a> to Select a different Action";
}
For action B it is an elseif function. If A isn€™t executed then it will attempt B. These work in the same way as the if statement and are used after the first if statement.
CODE
elseif ($action == c) {
echo "You have done action C. <br><a href=multiple.php>Click Here</a> to Select a different Action";
}
For action C it is an elseif function. If B isn€™t executed then it will attempt C. These work in the same way as the if statement and are used after the first if statement.
CODE
else {
echo "Please choose an action:
<br><a href=multiple.php?action=a>Action A</a>
<br><a href=multiple.php?action=b>Action B</a>
<br><a href=multiple.php?action=c>Action C</a>";
}
If 'action' isn€™t 'a', 'b' or 'c' then do this. Which again can be any php script but in this case it writes some text and links to the different actions.
CODE
?>
You use this to close the php. After this tag you can put standard HTML.
Although this tutorial on its own isn€™t much use it would be very useful for database driven websites and admin pages (i.e. one admin file for the whole of the news system, you could have news.php and be able to submit, edit, delete etc all news posts from that one file using the tutorial above).
Anyway, hope someone finds this useful. I only just discovered how to do it a few days ago. Something that was racking my brain, then it suddenly came to me in an English exam!
If you notice any problems/spelling errors etc please tell me.
-Adrian