[PHP]How to make your script easier
Posted: Sat Sep 17, 2011 8:38 am
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..
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
so you get the username from the id from your account
Ok, so here's an example how to set player data
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
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
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 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');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."'");
}
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