Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Multiple Actions In One File, Beginner - Intermediate
Adrian
post May 7 2005, 02:04 AM
Post #1


Addicted Dot
Group Icon
Group: Moderator
Posts: 7,779
Joined: 26-September 04
Member No.: 763





Multiple Actions in one file - By Adrian Trimble
I'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 thumbsup.gif


IPB
 
+Quote Post  Go to the top of the page
Eddie_K
post May 7 2005, 02:17 AM
Post #2


Hush now, I'm thinking...
Group Icon
Group: *Premier*
Posts: 3,977
Joined: 25-May 04
From: Heaven
Member No.: 47





Nice stuff smile.gif I should start already learning PHP... doesen't look hard... Who has some time to spare and give me :whistle:

Thanks Adrian bigwink.gif

Eddie


IPB
Take the time to relax. Enjoy a game of Fallen Sword, great multiplayer RPG!

 
+Quote Post  Go to the top of the page
Zaigham
post May 7 2005, 03:40 AM
Post #3


13Dots' Friend!
Group Icon
Group: *Premier*
Posts: 501
Joined: 13-July 04
From: Pakistan
Member No.: 400





Very nice, easy tutorial Adrian !!

Thanks for sharing smile.gif

Regards,

Zaigham


IPB
 
+Quote Post  Go to the top of the page
*D*
post May 7 2005, 11:20 AM
Post #4


Merry KissMoose
Group Icon
Group: Main Team
Posts: 15,988
Joined: 18-May 04
From: North Pole
Member No.: 2





Nice stuff Adrian flowers.gif
 
+Quote Post  Go to the top of the page
fade
post May 11 2005, 06:09 PM
Post #5


New Member
Group Icon
Group: Member
Posts: 12
Joined: 11-May 05
Member No.: 3,934





Looks hard but isn't learning? I will try this. Thanks for the tut
 
+Quote Post  Go to the top of the page
gcds
post Jan 26 2006, 09:54 AM
Post #6


New Member
Group Icon
Group: Member
Posts: 22
Joined: 25-January 06
Member No.: 9,246





QUOTE
Nice stuff I should start already learning PHP... doesen't look hard...

cool and esy
 
+Quote Post  Go to the top of the page
mousy
post Mar 21 2006, 01:08 PM
Post #7


New Member
Group Icon
Group: *Premier*
Posts: 6
Joined: 21-March 06
Member No.: 10,415





Just extening on this slightly.

You could also use switch.

switch ($action){
case "a":
//actions for A
break;
case "b":
//actions for B
break;
case "c":
//actions for C
break;
default:
//actions when $action does not match any of the cases
}

smile.gif

This post has been edited by mousy: Mar 21 2006, 01:09 PM


IPB
Planning a PHP Project. If you have any ideas please let me know via MSN :)
 
+Quote Post  Go to the top of the page
Holic
post Mar 21 2006, 07:57 PM
Post #8


Regular Dot
Group Icon
Group: Member
Posts: 169
Joined: 21-August 04
Member No.: 567





Yes, the use of a switch statement would be better here, but a good tutorial nonetheless smile.gif
 
+Quote Post  Go to the top of the page

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 8th January 2009 - 09:26 AM
Mobile Phones | Mortgages | Credit Cards | Best Credit Card Deals | Debt Consolidation