Jump to content
MakeWebGames

Simple Funtions


Joel
 Share

Recommended Posts

Simple Funtions

Hey people I will show you how to create simple functions for Mccodes instead of creating loads off files.

So create a file, Name it anything, I will call mine test.php

Now we would need this code in side the file:

 

<?php
include "globals.php";
/*------------------------
Our functions will go here.
------------------------*/
$h->endpage();
?>

 

Now we need to create the Cases for the functions, So let me update the code above:

 

<?php
include "globals.php";
switch($_GET['action'])
{
case 'test1': test1(); break;
case 'test2': test2(); break;
default: index(); break;
}
$h->endpage();
?>

 

You can have more Cases if you wish but you will need to create more functions, so now we have the Cases, we can start doing the functions, let me show you an example below off how we will do the functions:

 

<?php
include "globals.php";
switch($_GET['action'])
{
case 'test1': test1(); break;
case 'test2': test2(); break;
default: index(); break;
}
function index()
{
global $ir,$c,$userid,$h;
print "what ever you want here";
}
$h->endpage();
?>

 

The index will be the main page of the file.

Let me show you how it works:

 

<?php
include "globals.php";
switch($_GET['action'])
{
case 'test1': test1(); break;
case 'test2': test2(); break;
default: index(); break;
}
function index()
{
global $ir,$c,$userid,$h;
print "what ever you want here";
}
function test1()
{
global $ir,$c,$userid,$h;
print "test1 success";
}
function test2()
{
global $ir,$c,$userid,$h;
print "test2 success";
}
$h->endpage();
?>

 

Now if you have both test1 and test2 functions in there, then go to your address bar and test them, so the URL would be like this:

http://www.YOURGAME.com/FILENAME.php?action=test1

and

http://www.YOURGAME.com/FILENAME.php?action=test2

And then it should say Success!

Now that was simple wasn't it?

Any help just say, I am happy to help!

Link to comment
Share on other sites

Re: Simple Funtions

 

nice, we need people like you to help out the noobs that haven't got a clue lol....thanks for posting mate

Thanks for your comment, and functions are easy to use if you no what to do, so yeah it will help out the "noobs" lol.

Link to comment
Share on other sites

Re: Simple Funtions

 

* Topic Moved *

This thread doesn't supply an explanation of usage of any sort, and it is linked to DBS code - not useful for everyone, try writing about something that doesn't require any third-party framework/application/script/engine.

Oh alright sorry.

Link to comment
Share on other sites

Re: Simple Funtions

 

<html>
<head><title>Example</title>
<body bgcolor="#cccccc">
<h3>Example Page</h3>
<?php
if(isset($_GET['action'])  {

 switch($_GET['action'])
 {
 case 'a': include('inc/a.inc.php'); break;
 case 'b': include(inc/b.inc.php'); break;
 }

}
else {

index?

}
?>
</body>
</html>

 

instead of having loads of lines of code you could have them included...

just a different way of doing it :P

Link to comment
Share on other sites

Re: Simple Funtions

pog-one, that's a good point, however, it's not necessary most of the time.

Most scripts I make for my game or other people's games, have like four or five cases. Splitting one file, such as attack.php over five seperate files is just ridiculous IMHO.

At least, splitting it up in some sort of haphazzard way as doing one function in each file is overkill.

Files should be split according to the logic flow.

For instance, mccodes has a glob_func.php file with many functions in it common to many files.

globals.php is good since a header and footer is needed on every page.

But, take attack.php. Do you really need an attacklost.php? and an attackwon.php?

For god sakes!

pog-one, I'm definitely in agreement with you that splitting up files like you're talking about is very useful in the right situation.

 

A perfect place for this is the admin panel. You could have all user related functions in one file, log functions in another, and so on.

Any staff functions that only need one function could be in the main staff file or in a staff/misc.php file.

Link to comment
Share on other sites

Re: Simple Funtions

yeah good point.

I havent used mccodes in a while but for my game in the admin pannel i have done a switch include, just makes the coding cleaner.

functions.php

<?php

//   The switch functions could be put into a single "switchFunction.php" for all pages
// ...all the functions

function mailbox() {

 globals $blah;

 print 'works!';

}

// more functions

?>

 

and if you wanted to code a page all the coding needed would be so much less.

 

<html>
<head><title>Example</title>
<body bgcolor="#cccccc">
<h3>Example Page</h3>
<div style="width:60%; border:1px solid #666666"> 
<?php
 switch($_GET['action'])
 {
 case '1': index(); break;
 case '2': mailbox(); break;
 default: index(); break;
 }
}
?>
</div>
</body>
</html>
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...