DragonPrime - LoGD Resource Community
Welcome Guest
  • Good morning, Guest.
    Please log in, or register.
  • August 25, 2016, 09:36:52 AM
Home Forums News Links Downloads Login Register Advanced Search
* * *
DragonPrime Menu
Login
 
 
Resource Pages
IRC Channels
Search

Pages: [1]   Go Down
  Print  
Author Topic: SQL Global Variables  (Read 3326 times)
0 Members and 1 Guest are viewing this topic.
Morgul
Guest
« on: May 24, 2004, 12:54:53 AM »

I don't know if this was used before, but this is my idea and i'll post it anyway:
Global Sql Vars are somekind of global variables. It can be used even if your php settings forbid you to use global vars. It can be used for anything, I use them for quest variables in LOTGD.
For example: You assign a value of a variable in one php file, and you can retrieve it in another php file, without using $_GET or $_POST, but using the database.
Here is what you need to do:

STEP1: CREATING TABLES:
-[create] New Table, 4 fields (ex: 'u_global_int' for integer variables)
-[field]   index  tinyint(4)  auto_increment             
-[field]   name  text                   
-[field]   value  tinyint(4)  default: 0             
-[field]   user  text (or category, etc)

STEP2: PROGRAMMING THE FUNCTIONS:     
Create a php file (for ex: 'userfunc.php'):
Code:
<?
   require_once "common.php";
function ListInt($sort){
   $sql="SELECT * FROM u_global_int ORDER BY ".$sort;
   $result = db_query($sql) or die(db_error(LINK));
   while ($row=mysql_fetch_array($result)) {
      output("USER: ".$row[user].", NAME: ".$row[name].", VALUE: ".$row[value]."`n");
   }
   db_free_result($result);
}

function IntClear(){
   $sql="TRUNCATE TABLE u_global_int";
   db_query($sql) or die(db_error(LINK));
     
}
   

function Int($user,$name,$op,$value){
   if($value==""){$value="0";}
  if($op=="add"){             // ADD
        $sql="INSERT INTO u_global_int(user,name,value) VALUES ('".$user."','".$name."','".$value."')";
      db_query($sql) or die(db_error(LINK));
  }elseif($op=="get"){         // GET
      $ret=0;
      $sql="SELECT * FROM u_global_int WHERE name ='".$name."' AND user = '".$user."' LIMIT 1";
      $result = db_query($sql) or die(db_error(LINK));
         $row=mysql_fetch_array($result);
         $ret=$row['value'];
         return $ret;
         db_free_result($result);
   }elseif($op=="set"){      // SET
      $sql="SELECT * FROM u_global_int WHERE name ='".$name."' AND user = '".$user."' LIMIT 1";
      $result = db_query($sql) or die(db_error(LINK));
      if(db_num_rows($result)<>0){
         $sql="UPDATE u_global_int SET name = '".$name."',value = '".$value."' WHERE name = '".$name."' AND user = '".$user."' LIMIT 1";
         db_query($sql) or die(db_error(LINK));
      }else{
         $sql="INSERT INTO u_global_int(user,name,value) VALUES ('".$user."','".$name."','".$value."')";
         db_query($sql) or die(db_error(LINK));
      }
      db_free_result($result);
   }elseif($op=="del"){
      $sql="DELETE FROM u_global_int WHERE name = '".$name."' AND user = '".$user."' LIMIT 1";
      $result=db_query($sql) or die(db_error(LINK));
   }elseif($op=="delall"){
      $sql="DELETE FROM u_global_int WHERE user = '".$user."'";
      $result=db_query($sql) or die(db_error(LINK));
   
   }elseif($op=="+"){
      $sql="SELECT * FROM u_global_int WHERE name ='".$name."' AND user = '".$user."' LIMIT 1";
      $result = db_query($sql) or die(db_error(LINK));
      if(db_num_rows($result)<>0){
         $sql="SELECT * FROM u_global_int WHERE name ='".$name."' AND user = '".$user."' LIMIT 1";
         $result = db_query($sql) or die(db_error(LINK));
         $row=mysql_fetch_array($result);
         $temp=$row['value'];
         $temp+=$value;
         $sql="UPDATE u_global_int SET name = '".$name."',value = '".$temp."' WHERE name = '".$name."' AND user = '".$user."' LIMIT 1";
         db_query($sql) or die(db_error(LINK));
      }else{
         $sql="INSERT INTO u_global_int(user,name,value) VALUES ('".$user."','".$name."','".$value."')";
         db_query($sql) or die(db_error(LINK));
      }
   }elseif($op=="-"){
      $sql="SELECT * FROM u_global_int WHERE name ='".$name."' AND user = '".$user."' LIMIT 1";
      $result = db_query($sql) or die(db_error(LINK));
      if(db_num_rows($result)<>0){
         $sql="SELECT * FROM u_global_int WHERE name ='".$name."' AND user = '".$user."' LIMIT 1";
         $result = db_query($sql) or die(db_error(LINK));
         $row=mysql_fetch_array($result);
         $temp=$row['value'];
         $temp-=$value;
         $sql="UPDATE u_global_int SET name = '".$name."',value = '".$temp."' WHERE name = '".$name."' AND user = '".$user."' LIMIT 1";
         db_query($sql) or die(db_error(LINK));
      }else{
         $sql="INSERT INTO u_global_int(user,name,value) VALUES ('".$user."','".$name."','".$value."')";
         db_query($sql) or die(db_error(LINK));
      }
   }elseif(($op=="*") || ($op=="product")){
      $sql="SELECT * FROM u_global_int WHERE name ='".$name."' AND user = '".$user."' LIMIT 1";
      $result = db_query($sql) or die(db_error(LINK));
      if(db_num_rows($result)<>0){
         $sql="SELECT * FROM u_global_int WHERE name ='".$name."' AND user = '".$user."' LIMIT 1";
         $result = db_query($sql) or die(db_error(LINK));
         $row=mysql_fetch_array($result);
         $temp=$row['value'];
         $temp = ($temp * $value);
         $sql="UPDATE u_global_int SET name = '".$name."',value = '".$temp."' WHERE name = '".$name."' AND user = '".$user."' LIMIT 1";
         db_query($sql) or die(db_error(LINK));
      }else{
         $sql="INSERT INTO u_global_int(user,name,value) VALUES ('".$user."','".$name."','".$value."')";
         db_query($sql) or die(db_error(LINK));
      }
   }elseif(($op=="/") || ($op==":") || ($op=="div")){
      $sql="SELECT * FROM u_global_int WHERE name ='".$name."' AND user = '".$user."' LIMIT 1";
      $result = db_query($sql) or die(db_error(LINK));
      if(db_num_rows($result)<>0){
         $sql="SELECT * FROM u_global_int WHERE name ='".$name."' AND user = '".$user."' LIMIT 1";
         $result = db_query($sql) or die(db_error(LINK));
         $row=mysql_fetch_array($result);
         $temp=$row['value'];
         $temp = ($temp / $value);
         $sql="UPDATE u_global_int SET name = '".$name."',value = '".$temp."' WHERE name = '".$name."' AND user = '".$user."' LIMIT 1";
         db_query($sql) or die(db_error(LINK));
      }else{
         $sql="INSERT INTO u_global_int(user,name,value) VALUES ('".$user."','".$name."','".$value."')";
         db_query($sql) or die(db_error(LINK));
      }
   }
}

?>

To use the new function use 'require_once "userfunc.php"' in your file instead of 'require_once "common.php"'.

EXAMPLES:
WARNING: "Morgul" is not a real user. It's just a category for storing variables. Instead of "Morgul" you can use whatever you want.
Code:
//Create a variable called "health" for user "Morgul", initial value: 100:
Int("Morgul","health","set", 100);             
//or:
Int("Morgul","health","add", 100);

//Modify the variable called "health" for user "Morgul", new value: 75:
Int("Morgul","health","set", 75);

//Increasing "health" for "Morgul" by 5;
Int("Morgul","health","+",5);

//Decreasing "health" for "Morgul" by 5;
Int("Morgul","health","-",5);

//Multiplying "health" for "Morgul" by 2;
Int("Morgul","health","*",2);

//Dividing "health" for "Morgul" by 2;
Int("Morgul","health","/",2); //or Int("Morgul","health","div",2);

//Displaying the value of "health" from "Morgul":
output(Int("Morgul","health","get","");

//Deleting "health" from "Morgul":
Int("Morgul","health","del","");

//Displaying all global variables, sorted out by 'user' (available: index, name, value, user)
ListInt(user);

//Deleting All variables in "Morgul":
Int("Morgul","","delall","");

//Clearing ALL GLOBAL VARIABLES:
IntClear();

//Creating a variable in "potions" category:
Int("potions","healing",0);

//Creating a var for the current user using his login name:
int($session[user][login],"has-been-here","set",1);

//Retrieving and displaying a var for the current user using his login name:
output(int($session[user][login],"has-been-here","get",""));

If you find any bugs, errors, please tell me...
P.S. Take a look at your table after you've created some vars
« Last Edit: April 30, 2007, 08:34:30 PM by Talisman » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  


*
DragonPrime Notices
Welcome to DragonPrime - The LoGD Resource Community!

Support Us
No funds raised yet this year
Your help is greatly appreciated!
Who's Online
25 Guests, 0 Users
DragonPrime LoGD
Recent Topics
Home Forums News Links Downloads Login Register Advanced Search