[PHP]How to make your script easier

Post all your tuts or request for tuts here.
Post Reply
stefans
Posts: 48
Joined: Wed Sep 14, 2011 7:14 pm

[PHP]How to make your script easier

Post by stefans »

Hello all,

So this is a tutorial about how you can make your script easier..
You can do this by using functions, so you don't have to type the whole code again and again and again =p
You can have functions that return something, or just functions that do something..

So here's an example for a function to get player data..

Code: Select all

function getPlayerData($userid, $row) {
$query = mysql_query("SELECT * FROM users WHERE id = '".$userid."'");
$row2 = mysql_fetch_assoc($query);
return $row2[$row];
}
So this will give you the the value of the row of that userid
So what i do is make a page functions.php and include it in my config.php
then you can use your function like that

Code: Select all

echo "Your name is ".getPlayerData($_SESSION['id'], 'username');
so you get the username from the id from your account

Ok, so here's an example how to set player data

Code: Select all

 function setPlayerData($userid, $row, $value) {
mysql_query("UPDATE users SET ".$row." = '".$value."' WHERE id = '".$userid."'");
}
So this will set the entered value of the entered row of the entered userid
I also include this in my functions.php page
Now i'm going to give you an example of using the 2 functions together

Code: Select all

$currentmoney = getPlayerData($_SESSION['id'], 'money');
$newmoney = $currentmoney - 10;
setPlayerData($_SESSION['id'], 'money', $newmoney);

I just typed these here, so there can be some faults, but it's just to explain it all,
I did my best to explain it so =p
Current project: http://www.mmtycoon.eu
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: [PHP]How to make your script easier

Post by Ark »

Awesome functions! Definately gonna use them! Thanks Stefans :)
Orgullo Catracho
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: [PHP]How to make your script easier

Post by hallsofvallhalla »

Not sure I can recommend this. Player data should only be run once on a page. I would not rerun the function everytime i needed the data. Maybe I am not reading this correctly but it seems overkill.

If you pull the player data in the beginning of the page as i do in my tutorials $playerinfo3 stores all teh data for the rest of the script.

However if you are using javascript and ajax a script like this would be useful to pull updated data.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [PHP]How to make your script easier

Post by Jackolantern »

I think he just means using the function to get the player data at the start of the script; not using it every time you need any info about the player. Because that definitely would be overkill and would tax the database for no reason.

Functions definitely are the backbone of procedural programming, and are very important. You could work in data-scrubbing algorithms into those functions so you can clean everything heading towards MySQL. Although the majority of the calls you make to the function will not have player-generated data that could be dangerous, sometimes it is best to assume you screwed up somewhere when it comes to stuff like this and just scrub everything provided your security algorithms are not too CPU-costly (and in PHP, they usually aren't).
The indelible lord of tl;dr
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: [PHP]How to make your script easier

Post by Ark »

Well yeah maybe not to get data but instead to delete it.

Code: Select all

function deleteData($table, $id)
{
    mysql_query("DELETE from ".$table." where id=".$id."");
}
 
It's more neat and simple.
Orgullo Catracho
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [PHP]How to make your script easier

Post by Jackolantern »

There is also the benefit of making your scripts cleaner and more maintainable by using functions. What their use does is separates out the implementation of functionality from the actual game logic. When the two are muddled together, it makes pages much harder to read, understand and update later.
The indelible lord of tl;dr
Post Reply

Return to “Tutorials”