Guest User

Untitled

a guest
Dec 10th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 43.21 KB | None | 0 0
  1. <?php
  2. /* DbMapper.class.php - BetSter project (22.05.06)
  3.  * Copyright (C) 2006  Harald Kröll
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License as published by the Free
  7.  * Software Foundation; either version 2 of the License, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful, but WITHOUT
  11.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12.  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13.  * more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License along with
  16.  * this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
  18.  */
  19.  
  20. class DbMapper {
  21.  
  22.     function DbMapper(){
  23.         $connect  = mysql_connect (HOST, USER, PASSWORD) or
  24.             die("Database error, contact your admin");
  25.         if (!mysql_select_db (DBNAME, $connect))
  26.             error_catcher();
  27.     }
  28.  
  29.     // returns a Bet from a certain id
  30.     function getBet($id){
  31.  
  32.         $query = "SELECT * FROM bets WHERE id = '$id'";
  33.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  34.         $rowarray = mysql_fetch_array($result);
  35.  
  36.         $query = "SELECT id,name FROM possibilities WHERE bets_id = '$id'";
  37.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  38.         $possibilities_ids_array = array();
  39.         $possibilities_names_array = array();  
  40.         $i = 0;
  41.  
  42.         for ($possibilities_counter = 0; $possibilities_counter < (mysql_num_rows($result)) ; $possibilities_counter++) {
  43.             $possibilities_ids_array[$i] = mysql_result($result, $possibilities_counter, "id");
  44.             $possibilities_names_array[$i] = mysql_result($result, $possibilities_counter, "name");
  45.             $i++;
  46.         }
  47.  
  48.         $return_bet = new Bet($id = $rowarray["id"],
  49.                 $title = $rowarray["title"],
  50.                 $subtitle = $rowarray["subtitle"],
  51.                 $categories_id = $rowarray["categories_id"],
  52.                 $categories_name = $rowarray["categories_id"],
  53.                 $image = $rowarray["image"],
  54.                 $start_time = $rowarray["start"],
  55.                 $end_time = $rowarray["end"],
  56.                 $created =  $rowarray["created"],
  57.                 $autor_id =  $rowarray["autor_id"],
  58.                 $possibilities_names = $possibilities_names_array,
  59.                 $possibilities_ids = $possibilities_ids_array,
  60.                 $possibilities_quotes = 0);
  61.         return $return_bet;
  62.     }
  63.  
  64.     function getBetIdFromPossibility($possibility_id) {
  65.         $query = "SELECT DISTINCT possibilities.bets_id FROM possibilities WHERE possibilities.id = '$possibility_id'";
  66.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  67.         return mysql_result($result,0);
  68.     }
  69.  
  70.  
  71.     // returns the bets that are active now
  72.     function getActiveBets() {
  73.  
  74.         $query = "SELECT * FROM bets WHERE start < NOW() AND end > NOW() ORDER BY end ASC";
  75.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  76.         $return_bet_array = array();
  77.  
  78.  
  79.         for ($bet_counter = 0; $bet_counter < (mysql_num_rows($result)) ; $bet_counter++) {
  80.  
  81.             $bet_id = mysql_result($result, $bet_counter, "id");
  82.  
  83.             // Read out Arrays with Possibilities names and ids
  84.             $query = "SELECT id,name FROM possibilities WHERE bets_id = '$bet_id'";
  85.             $result2 = mysql_db_query (DBNAME,$query) or error_catcher();
  86.  
  87.             $possibilities_ids_array = array();
  88.             $possibilities_names_array = array();
  89.             $possibilities_quotes_array = array();
  90.  
  91.             for ($possibilities_counter = 0; $possibilities_counter < (mysql_num_rows($result2)) ; $possibilities_counter++) {
  92.                 $possibilities_ids_array[$possibilities_counter] = mysql_result($result2, $possibilities_counter, "id");
  93.                 $possibilities_names_array[$possibilities_counter] = mysql_result($result2, $possibilities_counter, "name");
  94.             }
  95.  
  96.             // Read out and calculate the array with the quotes    
  97.             $i = 0;
  98.             foreach ($possibilities_ids_array as $possibility_id) {
  99.                 $query1 = "SELECT SUM(credits) FROM transactions WHERE possibilities_id = '$possibility_id'";
  100.                 $query2 = "SELECT SUM(credits)
  101.                     FROM transactions, possibilities
  102.                     WHERE possibilities.bets_id = '$bet_id'
  103.                     AND possibilities.id = transactions.possibilities_id";
  104.                 $result1 = mysql_db_query (DBNAME,$query1)  or error_catcher();
  105.                 $result2 = mysql_db_query (DBNAME,$query2)  or error_catcher();
  106.                 $credits_of_possibility = mysql_result($result1, 0);
  107.                 $credits_of_bet = mysql_result($result2, 0);
  108.                 if ($credits_of_possibility == 0){
  109.                     $dirty_quote = "-";
  110.                 }
  111.                 else {
  112.                     $quote = $credits_of_bet / $credits_of_possibility;
  113.                     $dirty_quote = round($quote, 2);
  114.                 }
  115.                 $possibilities_quotes_array[$i] = $dirty_quote;
  116.                 $i++;
  117.             }
  118.  
  119.             // get the category_name from the category id
  120.             $query = "SELECT name FROM categories, bets WHERE bets.categories_id = categories.id AND bets.id = '$bet_id'";
  121.             $result1 = mysql_db_query (DBNAME,$query)  or error_catcher();
  122.             $category_name = mysql_result($result1, 0);
  123.  
  124.  
  125.             // Write the Bet objects in the return array
  126.             $return_bet_array[$bet_counter] = new Bet($bet_id,
  127.                     mysql_result($result, $bet_counter, "title"),
  128.                     mysql_result($result, $bet_counter, "subtitle"),
  129.                     mysql_result($result, $bet_counter, "categories_id"),
  130.                     $category_name,
  131.                     mysql_result($result, $bet_counter, "image"),
  132.                     mysql_result($result, $bet_counter, "start"),
  133.                     mysql_result($result, $bet_counter, "end"),
  134.                     mysql_result($result, $bet_counter, "created"),
  135.                     mysql_result($result, $bet_counter, "autor_id"),
  136.                     $possibilities_names_array,
  137.                     $possibilities_ids_array,
  138.                     $possibilities_quotes_array);                    
  139.         }
  140.         return $return_bet_array;
  141.     }
  142.  
  143.     // returns the bets from a certain categoy
  144.     function getActualCategoryBets($cat_id){
  145.  
  146.         $query = "SELECT * FROM bets WHERE start < NOW() AND end > NOW() AND categories_id = '$cat_id' ORDER BY end ASC";
  147.         $result = mysql_db_query (DBNAME,$query)
  148.              or error_catcher();
  149.         $return_bet_array = array();
  150.  
  151.  
  152.         for ($bet_counter = 0; $bet_counter < (mysql_num_rows($result)) ; $bet_counter++) {
  153.  
  154.             $bet_id = mysql_result($result, $bet_counter, "id");
  155.  
  156.             // Read out Arrays with Possibilities names and ids
  157.             $query = "SELECT id,name FROM possibilities WHERE bets_id = '$bet_id'";
  158.             $result2 = mysql_db_query (DBNAME,$query)
  159.                  or error_catcher();
  160.  
  161.             $possibilities_ids_array = array();
  162.             $possibilities_names_array = array();
  163.             $possibilities_quotes_array = array();
  164.  
  165.             for ($possibilities_counter = 0; $possibilities_counter < (mysql_num_rows($result2)) ; $possibilities_counter++) {
  166.                 $possibilities_ids_array[$possibilities_counter] = mysql_result($result2, $possibilities_counter, "id");
  167.                 $possibilities_names_array[$possibilities_counter] = mysql_result($result2, $possibilities_counter, "name");
  168.             }
  169.  
  170.             // Read out and calculate the array with the quotes    
  171.             $i = 0;
  172.             foreach ($possibilities_ids_array as $possibility_id) {
  173.                 $query1 = "SELECT SUM(credits) FROM transactions WHERE possibilities_id = '$possibility_id'";
  174.                 $query2 = "SELECT SUM(credits)
  175.                     FROM transactions, possibilities
  176.                     WHERE possibilities.bets_id = '$bet_id'
  177.                     AND possibilities.id = transactions.possibilities_id";
  178.                 $result1 = mysql_db_query (DBNAME,$query1)  or error_catcher();
  179.                 $result2 = mysql_db_query (DBNAME,$query2)  or error_catcher();
  180.                 $credits_of_possibility = mysql_result($result1, 0);
  181.                 $credits_of_bet = mysql_result($result2, 0);
  182.                 if ($credits_of_possibility == 0){
  183.                     $dirty_quote = "-";
  184.                 }
  185.                 else{
  186.                     $quote = $credits_of_bet / $credits_of_possibility;
  187.                     $dirty_quote = round($quote, 2);
  188.                 }
  189.                 $possibilities_quotes_array[$i] = $dirty_quote;
  190.                 $i++;
  191.             }
  192.  
  193.             // get the category_name from the category id
  194.             $query = "SELECT name FROM categories, bets WHERE bets.categories_id = categories.id AND bets.id = '$bet_id'";
  195.             $result1 = mysql_db_query (DBNAME,$query)  or error_catcher();
  196.             $category_name = mysql_result($result1, 0);
  197.  
  198.  
  199.             // Write the Bet objects in the return array
  200.             $return_bet_array[$bet_counter] = new Bet($bet_id,
  201.                     mysql_result($result, $bet_counter, "title"),
  202.                     mysql_result($result, $bet_counter, "subtitle"),
  203.                     mysql_result($result, $bet_counter, "categories_id"),
  204.                     $category_name,
  205.                     mysql_result($result, $bet_counter, "image"),
  206.                     mysql_result($result, $bet_counter, "start"),
  207.                     mysql_result($result, $bet_counter, "end"),
  208.                     mysql_result($result, $bet_counter, "created"),
  209.                     mysql_result($result, $bet_counter, "autor_id"),
  210.                     $possibilities_names_array,
  211.                     $possibilities_ids_array,
  212.                     $possibilities_quotes_array);                    
  213.         }
  214.         return $return_bet_array;
  215.     }
  216.  
  217.  
  218.     // returns the bets from a certain categoy
  219.     function getCategoryBets($cat_id){
  220.  
  221.         $query = "SELECT * FROM bets WHERE  categories_id = '$cat_id' ORDER BY end ASC";
  222.         $result = mysql_db_query (DBNAME,$query)
  223.              or error_catcher();
  224.         $return_bet_array = array();
  225.  
  226.  
  227.         for ($bet_counter = 0; $bet_counter < (mysql_num_rows($result)) ; $bet_counter++) {
  228.  
  229.             $bet_id = mysql_result($result, $bet_counter, "id");
  230.  
  231.             // Read out Arrays with Possibilities names and ids
  232.             $query = "SELECT id,name FROM possibilities WHERE bets_id = '$bet_id'";
  233.             $result2 = mysql_db_query (DBNAME,$query)
  234.                  or error_catcher();
  235.  
  236.             $possibilities_ids_array = array();
  237.             $possibilities_names_array = array();
  238.             $possibilities_quotes_array = array();
  239.  
  240.             for ($possibilities_counter = 0; $possibilities_counter < (mysql_num_rows($result2)) ; $possibilities_counter++) {
  241.                 $possibilities_ids_array[$possibilities_counter] = mysql_result($result2, $possibilities_counter, "id");
  242.                 $possibilities_names_array[$possibilities_counter] = mysql_result($result2, $possibilities_counter, "name");
  243.             }
  244.  
  245.             // Read out and calculate the array with the quotes    
  246.             $i = 0;
  247.             foreach ($possibilities_ids_array as $possibility_id) {
  248.                 $query1 = "SELECT SUM(credits) FROM transactions WHERE possibilities_id = '$possibility_id'";
  249.                 $query2 = "SELECT SUM(credits)
  250.                     FROM transactions, possibilities
  251.                     WHERE possibilities.bets_id = '$bet_id'
  252.                     AND possibilities.id = transactions.possibilities_id";
  253.                 $result1 = mysql_db_query (DBNAME,$query1)  or error_catcher();
  254.                 $result2 = mysql_db_query (DBNAME,$query2)  or error_catcher();
  255.                 $credits_of_possibility = mysql_result($result1, 0);
  256.                 $credits_of_bet = mysql_result($result2, 0);
  257.                 if ($credits_of_possibility == 0){
  258.                     $dirty_quote = "-";
  259.                 }
  260.                 else{
  261.                     $quote = $credits_of_bet / $credits_of_possibility;
  262.                     $dirty_quote = round($quote, 2);
  263.                 }
  264.                 $possibilities_quotes_array[$i] = $dirty_quote;
  265.                 $i++;
  266.             }
  267.  
  268.             // get the category_name from the category id
  269.             $query = "SELECT name FROM categories, bets WHERE bets.categories_id = categories.id AND bets.id = '$bet_id'";
  270.             $result1 = mysql_db_query (DBNAME,$query)  or error_catcher();
  271.             $category_name = mysql_result($result1, 0);
  272.  
  273.  
  274.             // Write the Bet objects in the return array
  275.             $return_bet_array[$bet_counter] = new Bet($bet_id,
  276.                     mysql_result($result, $bet_counter, "title"),
  277.                     mysql_result($result, $bet_counter, "subtitle"),
  278.                     mysql_result($result, $bet_counter, "categories_id"),
  279.                     $category_name,
  280.                     mysql_result($result, $bet_counter, "image"),
  281.                     mysql_result($result, $bet_counter, "start"),
  282.                     mysql_result($result, $bet_counter, "end"),
  283.                     mysql_result($result, $bet_counter, "created"),
  284.                     mysql_result($result, $bet_counter, "autor_id"),
  285.                     $possibilities_names_array,
  286.                     $possibilities_ids_array,
  287.                     $possibilities_quotes_array);                    
  288.         }
  289.         return $return_bet_array;
  290.     }
  291.  
  292.  
  293.  
  294.     // returns an array with all bets
  295.     function getAllBets(){
  296.  
  297.         $query = "SELECT * FROM bets ORDER BY end ASC";
  298.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  299.  
  300.         $return_bet_array = array();
  301.  
  302.  
  303.         for ($bet_counter = 0; $bet_counter < (mysql_num_rows($result)) ; $bet_counter++) {
  304.  
  305.             $bet_id = mysql_result($result, $bet_counter, "id");
  306.  
  307.             // Read out Arrays with Possibilities names and ids
  308.             $query = "SELECT id,name FROM possibilities WHERE bets_id = '$bet_id'";
  309.             $result2 = mysql_db_query (DBNAME,$query)
  310.                  or error_catcher();
  311.  
  312.             $possibilities_ids_array = array();
  313.             $possibilities_names_array = array();
  314.             $possibilities_quotes_array = array();
  315.  
  316.             for ($possibilities_counter = 0; $possibilities_counter < (mysql_num_rows($result2)) ; $possibilities_counter++) {
  317.                 $possibilities_ids_array[$possibilities_counter] = mysql_result($result2, $possibilities_counter, "id");
  318.                 $possibilities_names_array[$possibilities_counter] = mysql_result($result2, $possibilities_counter, "name");
  319.             }
  320.  
  321.             // Read out and calculate the array with the quotes    
  322.             $i = 0;
  323.             foreach ($possibilities_ids_array as $possibility_id) {
  324.                 $query1 = "SELECT SUM(credits) FROM transactions WHERE possibilities_id = '$possibility_id'";
  325.                 $query2 = "SELECT SUM( credits )
  326.                     FROM transactions, possibilities
  327.                     WHERE possibilities.bets_id = '$bet_id'
  328.                     AND possibilities.id = transactions.possibilities_id";
  329.                 $result1 = mysql_db_query (DBNAME,$query1) or die ("Ungültige Abfrage: ". mysql_error());
  330.                 $result2 = mysql_db_query (DBNAME,$query2) or die ("Ungültige Abfrage: ". mysql_error());
  331.                 $credits_of_possibility = mysql_result($result1, 0);
  332.                 $credits_of_bet = mysql_result($result2, 0);
  333.                 if ($credits_of_possibility == 0){
  334.                     $dirty_quote = "-";
  335.                 }
  336.                 else{
  337.                     $quote = $credits_of_bet / $credits_of_possibility;
  338.                     $dirty_quote = round($quote, 2);
  339.                 }
  340.                 $possibilities_quotes_array[$i] = $dirty_quote;
  341.                 $i++;
  342.             }
  343.  
  344.             // get the category_name from the category id
  345.             $query = "SELECT name FROM categories, bets WHERE bets.categories_id = categories.id AND bets.id = '$bet_id'";
  346.             $result1 = mysql_db_query (DBNAME,$query)  or error_catcher();
  347.             $category_name = mysql_result($result1, 0);
  348.  
  349.  
  350.             // Write the Bet objects in the return array
  351.             $return_bet_array[$bet_counter] = new Bet($bet_id,
  352.                     mysql_result($result, $bet_counter, "title"),
  353.                     mysql_result($result, $bet_counter, "subtitle"),
  354.                     mysql_result($result, $bet_counter, "categories_id"),
  355.                     $category_name,
  356.                     mysql_result($result, $bet_counter, "image"),
  357.                     mysql_result($result, $bet_counter, "start"),
  358.                     mysql_result($result, $bet_counter, "end"),
  359.                     mysql_result($result, $bet_counter, "created"),
  360.                     mysql_result($result, $bet_counter, "autor_id"),
  361.                     $possibilities_names_array,
  362.                     $possibilities_ids_array,
  363.                     $possibilities_quotes_array);                    
  364.         }
  365.         return $return_bet_array;
  366.     }
  367.  
  368.     // returns an array with all bets before now
  369.     function getActiveAndPastBets(){
  370.  
  371.         $query = "SELECT * FROM bets WHERE start < NOW() ORDER BY created DESC";
  372.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  373.  
  374.         $return_bet_array = array();
  375.  
  376.  
  377.         for ($bet_counter = 0; $bet_counter < (mysql_num_rows($result)) ; $bet_counter++) {
  378.  
  379.             $bet_id = mysql_result($result, $bet_counter, "id");
  380.  
  381.             // Read out Arrays with Possibilities names and ids
  382.             $query = "SELECT id,name FROM possibilities WHERE bets_id = '$bet_id'";
  383.             $result2 = mysql_db_query (DBNAME,$query) or error_catcher();
  384.  
  385.             $possibilities_ids_array = array();
  386.             $possibilities_names_array = array();
  387.             $possibilities_quotes_array = array();
  388.  
  389.             for ($possibilities_counter = 0; $possibilities_counter < (mysql_num_rows($result2)) ; $possibilities_counter++) {
  390.                 $possibilities_ids_array[$possibilities_counter] = mysql_result($result2, $possibilities_counter, "id");
  391.                 $possibilities_names_array[$possibilities_counter] = mysql_result($result2, $possibilities_counter, "name");
  392.             }
  393.  
  394.             // Read out and calculate the array with the quotes    
  395.             $i = 0;
  396.             foreach ($possibilities_ids_array as $possibility_id) {
  397.                 $query1 = "SELECT SUM(credits) FROM transactions WHERE possibilities_id = '$possibility_id'";
  398.                 $query2 = "SELECT SUM( credits )
  399.                     FROM transactions, possibilities
  400.                     WHERE possibilities.bets_id = '$bet_id'
  401.                     AND possibilities.id = transactions.possibilities_id";
  402.                 $result1 = mysql_db_query (DBNAME,$query1)  or error_catcher();
  403.                 $result2 = mysql_db_query (DBNAME,$query2)  or error_catcher();
  404.                 $credits_of_possibility = mysql_result($result1, 0);
  405.                 $credits_of_bet = mysql_result($result2, 0);
  406.                 if ($credits_of_possibility == 0){
  407.                     $dirty_quote = "-";
  408.                 }
  409.                 else{
  410.                     $quote = $credits_of_bet / $credits_of_possibility;
  411.                     $dirty_quote = round($quote, 2);
  412.                 }
  413.                 $possibilities_quotes_array[$i] = $dirty_quote;
  414.                 $i++;
  415.             }
  416.  
  417.             // get the category_name from the category id
  418.             $query = "SELECT name FROM categories, bets WHERE bets.categories_id = categories.id AND bets.id = '$bet_id'";
  419.             $result1 = mysql_db_query (DBNAME,$query)  or error_catcher();
  420.             $category_name = mysql_result($result1, 0);
  421.  
  422.  
  423.             // Write the Bet objects in the return array
  424.             $return_bet_array[$bet_counter] = new Bet($bet_id,
  425.                     mysql_result($result, $bet_counter, "title"),
  426.                     mysql_result($result, $bet_counter, "subtitle"),
  427.                     mysql_result($result, $bet_counter, "categories_id"),
  428.                     $category_name,
  429.                     mysql_result($result, $bet_counter, "image"),
  430.                     mysql_result($result, $bet_counter, "start"),
  431.                     mysql_result($result, $bet_counter, "end"),
  432.                     mysql_result($result, $bet_counter, "created"),
  433.                     mysql_result($result, $bet_counter, "autor_id"),
  434.                     $possibilities_names_array,
  435.                     $possibilities_ids_array,
  436.                     $possibilities_quotes_array);                    
  437.         }
  438.         return $return_bet_array;
  439.     }    
  440.  
  441.  
  442.  
  443.     function checkIfUserInDB($username, $password){
  444.         $query = "SELECT username FROM user WHERE username like '$username'
  445.             AND password like '".md5($password)."' AND (status LIKE 'active' OR status LIKE 'betmaster' OR status LIKE 'administrator' OR status LIKE 'dummy' OR status LIKE 'locked')";
  446.         $result = mysql_query ($query)  or error_catcher();
  447.         if (mysql_num_rows ($result) >= 1) return true;
  448.         else return false;
  449.     }
  450.  
  451.  
  452.     function checkIfEmailInDb($email){
  453.         $query = "SELECT email FROM user WHERE email like '$email'";
  454.         $result = mysql_query ($query)  or error_catcher();
  455.         if (mysql_num_rows ($result) >= 1) return true;
  456.         else return false;
  457.     }
  458.  
  459.     function getUserCount(){
  460.         $query = "SELECT count(id) FROM user";
  461.         $result = mysql_query ($query)  or error_catcher();
  462.         if (mysql_num_rows($result) >= 1){
  463.             return mysql_result($result, 0);
  464.         }
  465.     }
  466.  
  467.     function getUserConfNum($username){
  468.         $query = "SELECT conf_num FROM user WHERE username like '$username'";
  469.         $result = mysql_query ($query)  or error_catcher();
  470.         if (mysql_num_rows($result) >= 1){
  471.             return mysql_result($result, 0);
  472.         }
  473.     }
  474.  
  475.  
  476.     function getUsersOfPossibilities($pos) {
  477.         $query = "SELECT DISTINCT user.id, user.username, user.email, user.firstname, user.lastname, user.balance, user.status
  478.             FROM user, transactions WHERE transactions.possibilities_id = '$pos'
  479.             AND user.id = transactions.user_id;";
  480.         $result = mysql_query ($query)  or error_catcher();
  481.  
  482.         $user_array = array();
  483.         for ($user_counter = 0; $user_counter < (mysql_num_rows($result)) ; $user_counter++) {
  484.             $user_array[$user_counter] = new User(mysql_result($result, $user_counter, "id"),
  485.                     mysql_result($result, $user_counter, "username"),
  486.                     mysql_result($result, $user_counter, "email"),
  487.                     mysql_result($result, $user_counter, "firstname"),
  488.                     mysql_result($result, $user_counter, "lastname"),
  489.                     mysql_result($result, $user_counter, "balance"),
  490.                     mysql_result($result, $user_counter, "status"));
  491.         }
  492.         return $user_array;
  493.     }
  494.  
  495.  
  496.  
  497.     function getUser($username){
  498.         $query = "SELECT id,username,email,firstname,lastname,balance,status
  499.             FROM user WHERE username = '".$username."'";
  500.         $result = mysql_query ($query)  or error_catcher();
  501.         $rowarray = mysql_fetch_array($result);
  502.  
  503.         $user = new User($rowarray['id'],
  504.                 $rowarray['username'],
  505.                 $rowarray['email'],
  506.                 $rowarray['firstname'],
  507.                 $rowarray['lastname'],
  508.                 $rowarray['balance'],
  509.                 $rowarray['status']);
  510.         return $user;
  511.     }
  512.  
  513.  
  514.  
  515.     function getSite($id){
  516.         $query = "SELECT *  FROM sites WHERE id = '".$id."'";
  517.         $result = mysql_query ($query) or error_catcher();
  518.         $rowarray = mysql_fetch_array($result);
  519.  
  520.         $site = new Site($rowarray['id'],
  521.                 $rowarray['title'],
  522.                 $rowarray['text'],
  523.                 $rowarray['author'],
  524.                 $rowarray['date']);
  525.         return $site;
  526.     }
  527.  
  528.  
  529.     function getAllSites(){
  530.         $query = "SELECT * FROM sites ORDER BY title ASC";
  531.         $result = mysql_query ($query)  or error_catcher();
  532.         $sites = array();
  533.         for ($site_counter = 0; $site_counter < (mysql_num_rows($result)) ; $site_counter++) {
  534.             $sites[$site_counter] = new Site(mysql_result($result, $site_counter, "id"),
  535.                     mysql_result($result, $site_counter, "title"),
  536.                     mysql_result($result, $site_counter, "text"),
  537.                     mysql_result($result, $site_counter, "author"),
  538.                     mysql_result($result, $site_counter, "date"));
  539.         }
  540.         return $sites;
  541.     }
  542.  
  543.     function deleteUser($id){
  544.         $query = "DELETE FROM user WHERE id = '".$id."'";
  545.         $result = mysql_query ($query) or error_catcher();
  546.     }
  547.  
  548.     function deletePossibility($id){
  549.         $query = "DELETE FROM possibilities WHERE id = '".$id."'";
  550.         $result = mysql_query ($query)  or error_catcher();
  551.     }
  552.  
  553.     function deleteBet($id){
  554.         $query = "DELETE FROM bets WHERE id = '".$id."'";
  555.         $result = mysql_query ($query)  or error_catcher();
  556.     }
  557.  
  558.     function deleteCategory($id){
  559.         $bets = $this->getCategoryBets($id);
  560.         if (count($bets) == 0){
  561.             $query = "DELETE FROM categories WHERE id = '".$id."'";
  562.             $result = mysql_query ($query)  or error_catcher();
  563.             return true;
  564.         }
  565.         else {
  566.             return false;
  567.         }
  568.     }
  569.  
  570.     function deleteSite($id){
  571.         $query = "DELETE FROM sites WHERE id = '".$id."'";
  572.         $result = mysql_query ($query)  or error_catcher();
  573.     }
  574.  
  575.  
  576.     function getUserById($id){
  577.         $query = "SELECT id,username,email,firstname,lastname,balance,status
  578.             FROM user WHERE id = '".$id."'";
  579.         $result = mysql_query ($query)  or error_catcher();
  580.         $rowarray = mysql_fetch_array($result);
  581.  
  582.         $user = new User($rowarray['id'],
  583.                 $rowarray['username'],
  584.                 $rowarray['email'],
  585.                 $rowarray['firstname'],
  586.                 $rowarray['lastname'],
  587.                 $rowarray['balance'],
  588.                 $rowarray['status']);
  589.         return $user;
  590.     }
  591.  
  592.     function getLastUserId(){
  593.         $query = "SELECT MAX(id) FROM user";
  594.         $result = mysql_query ($query)  or error_catcher();
  595.         if (mysql_num_rows($result) >= 1){
  596.             return mysql_result($result, 0);
  597.         }
  598.     }
  599.  
  600.  
  601.     function getFirstUserId(){
  602.         $query = "SELECT MIN(id) FROM user";
  603.         $result = mysql_query ($query)  or error_catcher();
  604.         if (mysql_num_rows($result) >= 1){
  605.             return mysql_result($result, 0);
  606.         }
  607.     }
  608.  
  609.  
  610.     function getAllUsers(){
  611.         $query = "SELECT * FROM user ORDER BY Id ASC";
  612.         $result = mysql_query ($query) or error_catcher();
  613.         $user_array = array();
  614.         for ($user_counter = 0; $user_counter < (mysql_num_rows($result)) ; $user_counter++) {
  615.             $user_array[$user_counter] = new User(mysql_result($result, $user_counter, "id"),
  616.                     mysql_result($result, $user_counter, "username"),
  617.                     mysql_result($result, $user_counter, "email"),
  618.                     mysql_result($result, $user_counter, "firstname"),
  619.                     mysql_result($result, $user_counter, "lastname"),
  620.                     mysql_result($result, $user_counter, "balance"),
  621.                     mysql_result($result, $user_counter, "status"));
  622.         }
  623.         return $user_array;
  624.     }
  625.  
  626.     function getTopUsers($nr){
  627.         $query = "SELECT DISTINCT user.* FROM user, transactions WHERE
  628.             (STATUS NOT LIKE 'inactive' AND STATUS NOT LIKE 'dummy' AND STATUS NOT LIKE 'locked')
  629.             AND transactions.user_id = user.id
  630.             ORDER BY balance DESC
  631.             LIMIT ".$nr."";
  632.         $result = mysql_query ($query) or error_catcher();
  633.         $user_array = array();
  634.         for ($user_counter = 0; $user_counter < (mysql_num_rows($result)) ; $user_counter++) {
  635.             $user_array[$user_counter] = new User(mysql_result($result, $user_counter, "id"),
  636.                     mysql_result($result, $user_counter, "username"),
  637.                     mysql_result($result, $user_counter, "email"),
  638.                     mysql_result($result, $user_counter, "firstname"),
  639.                     mysql_result($result, $user_counter, "lastname"),
  640.                     mysql_result($result, $user_counter, "balance"),
  641.                     mysql_result($result, $user_counter, "status"));
  642.         }
  643.         return $user_array;
  644.     }
  645.  
  646.     // query doesn't work
  647.     function getWinningUsers($possibility_id){
  648.         $query = "SELECT DISTINCT user.*
  649.             FROM user, transactions, possibilities, userwins
  650.             WHERE user.id = transactions.user_id
  651.             AND transactions.possibilities_id = possibilities.id
  652.             AND possibilities.id = '".$possibility_id."'
  653.             AND userwins.possibilities_id <> possibilities.id
  654.             AND possibilities.win = 'yes'";
  655.         $result = mysql_query ($query) or error_catcher();
  656.         $user_array = array();
  657.         for ($user_counter = 0; $user_counter < (mysql_num_rows($result)) ; $user_counter++) {
  658.             $user_array[$user_counter] = new User(mysql_result($result, $user_counter, "id"),
  659.                     mysql_result($result, $user_counter, "username"),
  660.                     mysql_result($result, $user_counter, "email"),
  661.                     mysql_result($result, $user_counter, "firstname"),
  662.                     mysql_result($result, $user_counter, "lastname"),
  663.                     mysql_result($result, $user_counter, "balance"),
  664.                     mysql_result($result, $user_counter, "status"));
  665.         }
  666.         return $user_array;
  667.     }
  668.  
  669.  
  670.     function getLoosingUsers($bet_id){
  671.         $query = "SELECT DISTINCT user.*
  672.             FROM user, transactions, possibilities, userwins
  673.             WHERE user.id = transactions.user_id
  674.             AND transactions.possibilities_id = possibilities.id
  675.             AND possibilities.bets_id = '$bet_id'";
  676.         $result = mysql_query ($query) or error_catcher();
  677.         $user_array = array();
  678.         for ($user_counter = 0; $user_counter < (mysql_num_rows($result)) ; $user_counter++) {
  679.             $user_array[$user_counter] = new User(mysql_result($result, $user_counter, "id"),
  680.                     mysql_result($result, $user_counter, "username"),
  681.                     mysql_result($result, $user_counter, "email"),
  682.                     mysql_result($result, $user_counter, "firstname"),
  683.                     mysql_result($result, $user_counter, "lastname"),
  684.                     mysql_result($result, $user_counter, "balance"),
  685.                     mysql_result($result, $user_counter, "status"));
  686.         }
  687.         return $user_array;
  688.     }
  689.  
  690.     function insertUser($username, $email, $firstname, $lastname, $password, $status, $conf_num){
  691.         $query = "INSERT INTO user (username, password, email, firstname, lastname, status, conf_num)
  692.             VALUES('".$username."', '".md5($password)."', '".$email."', '".$firstname."', '".$lastname."', '".$status."', '".$conf_num."')";
  693.         $result = mysql_query ($query)  or error_catcher();
  694.         return $result;
  695.     }
  696.  
  697.  
  698.     function insertSite($site){
  699.         $query = "INSERT INTO sites (title, text, author, date)
  700.             VALUES('".$site->getTitle()."', '".$site->getText()."', '".$site->getAuthor()."', NOW())";
  701.         $result = mysql_query ($query)  or error_catcher();
  702.         return $result;
  703.     }
  704.  
  705.     function updateSite($site){
  706.         $query = "UPDATE sites SET title = '".$site->getTitle()."',
  707.             text = '".$site->getText()."',
  708.             author = '".$site->getAuthor()."',
  709.             date = NOW()
  710.                 WHERE id = '".$site->getId()."'";
  711.         $result = mysql_query ($query)  or error_catcher();
  712.         return $result;
  713.     }
  714.  
  715.     function updateUser($username, $firstname, $lastname, $password){
  716.         if ($password == ""){
  717.             $query = "UPDATE user SET firstname = '$firstname',
  718.                 lastname = '$lastname'
  719.                     WHERE username = '$username'"; 
  720.  
  721.         }
  722.         else {
  723.             $query = "UPDATE user SET password = '".md5($password)."',
  724.                 firstname = '$firstname',
  725.                 lastname = '$lastname'
  726.                     WHERE username = '$username'";
  727.         }
  728.         $result = mysql_query ($query)  or error_catcher();
  729.         return $result;
  730.     }
  731.  
  732.     function updateUserPwd($password, $username){
  733.         $query = "UPDATE user SET password = '".md5($password)."' WHERE username = '$username'";
  734.         $result = mysql_query ($query)  or error_catcher();
  735.         return $result;
  736.     }
  737.  
  738.     function insertCategory($category){
  739.         $query = "SELECT MAX(id) FROM categories";
  740.         $result = mysql_db_query (DBNAME,$query)  or error_catcher();
  741.         $max_id = mysql_result($result, 0);
  742.         $category->setCategoryPosition($max_id++);
  743.  
  744.         $query = "INSERT INTO categories (position, name, description, image)
  745.             VALUES ('".$category->getCategoryPosition()."',
  746.                     '".$category->getCategoryName()."',
  747.                     '".$category->getCategoryDescription()."',
  748.                     '".$category->getCategoryImage()."')";
  749.         $result = mysql_query ($query)  or error_catcher();
  750.         return $result;
  751.     }
  752.  
  753.     function updateCategory($category){
  754.         $query = "UPDATE categories SET position = '".$category->getCategoryPosition()."',
  755.                                         name = '".$category->getCategoryName()."',
  756.                                         description = '".$category->getCategoryDescription()."',
  757.                                         image = '".$category->getCategoryImage()."'
  758.                                     WHERE id = '".$category->getCategoryId()."'";
  759.         $result = mysql_query ($query)  or error_catcher();
  760.         return $result;
  761.     }
  762.  
  763.     function insertBetTmp($title, $subtitle, $cat_id, $image, $start, $end, $pos_array){
  764.         $query = "INSERT INTO bets_tmp (title, subtitle, categories_id, image, start, end)
  765.             VALUES ('".$title."', '".$subtitle."', '".$cat_id."', '".$image."', '".$start."', '".$end."')";
  766.         $result = mysql_query ($query)  or error_catcher();
  767.         $bet_id = mysql_insert_id();
  768.         // insert the possibilities
  769.         $id = mysql_insert_id();
  770.         if ($pos_array != ""){
  771.             foreach($pos_array as $pos){
  772.                 $query = "INSERT INTO possibilities_tmp (bets_id, name)
  773.                     VALUES ('".$id."', '".$pos."')";
  774.                 $result = mysql_query ($query)  or error_catcher();
  775.             }
  776.         }
  777.         return $bet_id;
  778.     }
  779.  
  780.     function insertBet($bet){
  781.         $query = "INSERT INTO bets (title, subtitle, categories_id, image, start, end, created, autor_id)
  782.             VALUES ('".$bet->getBetTitle()."',
  783.                     '".$bet->getSubtitle()."',
  784.                     '".$bet->getCategoryId()."',
  785.                     '".$bet->getBetImage()."',
  786.                     '".$bet->getBetStartTime()."',
  787.                     '".$bet->getBetEndTime()."',
  788.                     NOW(),
  789.                     '".$bet->getBetAutorId()."')";
  790.         $result = mysql_query ($query)  or error_catcher();
  791.         $bet_id = mysql_insert_id();
  792.         // insert the possibilities
  793.         $id = mysql_insert_id();
  794.         foreach($bet->getPossibilitiesNames() as $pos){
  795.             $query = "INSERT INTO possibilities (bets_id, name)
  796.                 VALUES ('".$id."', '".$pos."')";
  797.             $result = mysql_query ($query)  or error_catcher();
  798.         }
  799.         return $id;
  800.     }
  801.  
  802.  
  803.     function insertLog($host, $addr, $username, $event,
  804.             $agent){
  805.         $query = "INSERT INTO log (rem_host, rem_addr, user, event, rem_agt, time)
  806.             VALUES ('".$host."',
  807.                     '".$addr."',
  808.                     '".$username."',
  809.                     '".$event."',
  810.                     '".$agent."',
  811.                     NOW())";
  812.         $result = mysql_query ($query)  or error_catcher();
  813.         return $result;
  814.     }
  815.  
  816.  
  817.     function  getBettedCredits($user_id, $possibility_id){
  818.         $query = "SELECT SUM(credits) FROM transactions WHERE
  819.             possibilities_id = '$possibility_id' AND
  820.             user_id = '$user_id'";
  821.         $result = mysql_db_query (DBNAME,$query)  or error_catcher();
  822.         $credits = mysql_result($result, 0);
  823.         return $credits;
  824.     }
  825.  
  826.     /* testquerys
  827.  
  828.        $query = "SELECT SUM(credits) FROM transactions WHERE
  829.        possibilities_id = '$possibility_id' AND
  830.        user_id = '$user_id'";
  831.        UPDATE user SET balance = (SELECT SUM(credits) FROM transactions WHERE possibilities_id = bet_id)
  832.      */
  833.  
  834.     function  getWonCredits($user_id, $possibility_id){
  835.         // get quote of the possibiliity
  836.         $query1 = "SELECT SUM(credits) FROM transactions WHERE possibilities_id = '$possibility_id'";
  837.  
  838.         $query3 = "SELECT possibilities.bets_id FROM possibilities WHERE possibilities.id = '$possibility_id'";
  839.         $result3 = mysql_db_query (DBNAME,$query3)  or error_catcher();
  840.         $bet_id = mysql_result($result3, 0);
  841.  
  842.         $query2 = "SELECT SUM(credits)
  843.             FROM transactions, possibilities
  844.             WHERE possibilities.bets_id = '$bet_id'
  845.             AND possibilities.id = transactions.possibilities_id";
  846.         $result1 = mysql_db_query (DBNAME,$query1) or die ("Ungültige Abfrage: ". mysql_error());
  847.         $result2 = mysql_db_query (DBNAME,$query2) or die ("Ungültige Abfrage: ". mysql_error());
  848.         $credits_of_possibility = mysql_result($result1, 0);
  849.         $credits_of_bet = mysql_result($result2, 0);   
  850.         $quote = $credits_of_bet / $credits_of_possibility;
  851.         $quote = round($quote, 2);
  852.  
  853.         // get the the credits for the possibility
  854.         $query = "SELECT SUM(transactions.credits)
  855.             FROM transactions
  856.             WHERE transactions.possibilities_id = '$possibility_id'
  857.             AND transactions.user_id = '$user_id'";
  858.         $result = mysql_db_query (DBNAME,$query)  or error_catcher();
  859.         $pos_sum = mysql_result($result, 0);
  860.  
  861.         $won_credits = $pos_sum * $quote;
  862.         return $won_credits;
  863.     }
  864.  
  865.     function getQuoteFromPosId($possibility_id, $credits){
  866.         // get quote of the possibiliity
  867.         $query1 = "SELECT SUM(credits) FROM transactions WHERE possibilities_id = '".$possibility_id."'";
  868.         $result1 = mysql_db_query (DBNAME,$query1)  or error_catcher();
  869.         // does not work in mysql prior 4.1
  870.         /*  $query2 = "SELECT SUM(credits)
  871.             FROM transactions, possibilities
  872.             WHERE possibilities.bets_id = (SELECT possibilities.bets_id FROM possibilities
  873.             WHERE possibilities.id = '".$possibility_id."')
  874.             AND possibilities.id = transactions.possibilities_id";             */    
  875.         $query3 = "SELECT possibilities.bets_id FROM possibilities WHERE possibilities.id = '$possibility_id'";
  876.         $result3 = mysql_db_query (DBNAME,$query3)  or error_catcher();
  877.         $bet_id = mysql_result($result3, 0);
  878.  
  879.         $query2_new = "SELECT SUM(credits)
  880.             FROM transactions, possibilities
  881.             WHERE possibilities.bets_id = '".$bet_id."'
  882.             AND possibilities.id = transactions.possibilities_id";  
  883.  
  884.             $result2 = mysql_db_query (DBNAME,$query2_new)  or error_catcher();
  885.  
  886.         $credits_of_possibility = mysql_result($result1, 0) + $credits;
  887.         $credits_of_bet = mysql_result($result2, 0) + $credits;    
  888.         if ($credits_of_possibility != 0){
  889.             $quote = $credits_of_bet / $credits_of_possibility;
  890.         }
  891.         $quote = round($quote, 2);
  892.         return $quote;
  893.     }
  894.  
  895.  
  896.  
  897.     function getWonPossibility($bet_id){
  898.         $query = "SELECT id FROM possibilities WHERE win = 'yes' AND bets_id = '$bet_id'";
  899.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  900.         if (mysql_num_rows($result) >= 1){
  901.             return mysql_result($result, 0);
  902.         }
  903.     }
  904.  
  905.     function getPossibilityNameFromId($pos_id){
  906.         $query = "SELECT name FROM possibilities WHERE id = '$pos_id'";
  907.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  908.         if (mysql_num_rows($result) >= 1){
  909.             return mysql_result($result, 0);
  910.         }
  911.     }
  912.  
  913.  
  914.     function getPossibilitiesIdsOfBet($bet_id){
  915.         $query = "SELECT id FROM possibilities WHERE bets_id = '$bet_id'";
  916.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  917.  
  918.         for ($pos_counter = 0;
  919.                 $pos_counter < (mysql_num_rows($result));
  920.                 $pos_counter++) {
  921.             $pos_array[$pos_counter] = mysql_result($result, $pos_counter, "id");
  922.         }
  923.         return $pos_array;
  924.     }
  925.  
  926.     /**
  927.      * sets a transaction when a user ecexutes a bet
  928.      *
  929.      *
  930.     // *  has nothing to do with the transaction class!
  931.      */
  932.     function setTransaction($user_id, $pos_id, $credits){
  933.  
  934.         $query = "INSERT INTO transactions (possibilities_id, user_id, credits, time)".
  935.             "VALUES ('".$pos_id."', '".$user_id."', '".$credits."', NOW());";
  936.         $result = mysql_db_query (DBNAME,$query)  or error_catcher();
  937.  
  938.         return $result;
  939.     }
  940.  
  941.     function setWonPossibility($id){
  942.  
  943.         $query = "UPDATE possibilities SET win = 'yes' WHERE id = '$id'";
  944.         $result = mysql_db_query(DBNAME, $query)  or error_catcher();
  945.  
  946.         return $result;
  947.     }
  948.  
  949.  
  950.     function getAllTransactions(){
  951.  
  952.         $query = "SELECT transactions.id, bets.id, bets.title,
  953.             possibilities.name, transactions.possibilities_id,
  954.             transactions.user_id, transactions.credits,
  955.             transactions.time
  956.                 FROM transactions, possibilities, bets
  957.                 WHERE transactions.possibilities_id = possibilities.id
  958.                 AND possibilities.bets_id = bets.id
  959.                 ORDER BY transactions.time DESC";
  960.         $result = mysql_db_query (DBNAME,$query)  or error_catcher();
  961.  
  962.  
  963.         $return_trans_array = array();
  964.  
  965.  
  966.         for ($trans_counter = 0;
  967.                 $trans_counter < (mysql_num_rows($result));
  968.                 $trans_counter++) {
  969.             $return_trans_array[$trans_counter] =
  970.                 new Transaction(mysql_result($result, $trans_counter, "transactions.id"),
  971.                         mysql_result($result, $trans_counter, "bets.id"),
  972.                         mysql_result($result, $trans_counter, "title"),
  973.                         mysql_result($result, $trans_counter, "name"),
  974.                         mysql_result($result, $trans_counter, "transactions.possibilities_id"),
  975.                         mysql_result($result, $trans_counter, "user_id"),
  976.                         mysql_result($result, $trans_counter, "credits"),
  977.                         mysql_result($result, $trans_counter, "time"));
  978.         }
  979.         return $return_trans_array;
  980.     }
  981.  
  982.  
  983.     function getTransactions($user_id){
  984.  
  985.         $query = "SELECT transactions.id, bets.id, bets.title, possibilities.name, transactions.possibilities_id, transactions.credits, transactions.time
  986.             FROM transactions, possibilities, bets
  987.             WHERE user_id = '$user_id'
  988.             AND transactions.possibilities_id = possibilities.id
  989.             AND possibilities.bets_id = bets.id
  990.             ORDER BY transactions.time DESC";
  991.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  992.  
  993.  
  994.         $return_trans_array = array();
  995.  
  996.         for ($trans_counter = 0; $trans_counter < (mysql_num_rows($result)) ; $trans_counter++) {
  997.             $return_trans_array[$trans_counter] = new Transaction(mysql_result($result, $trans_counter, "transactions.id"),
  998.                     mysql_result($result, $trans_counter, "bets.id"),
  999.                     mysql_result($result, $trans_counter, "title"),
  1000.                     mysql_result($result, $trans_counter, "name"),
  1001.                     mysql_result($result, $trans_counter, "transactions.possibilities_id"),
  1002.                     $user_id,
  1003.                     mysql_result($result, $trans_counter, "credits"),
  1004.                     mysql_result($result, $trans_counter, "time"));
  1005.         }
  1006.         return $return_trans_array;
  1007.     }
  1008.  
  1009.  
  1010.     function getWins($user_id){
  1011.  
  1012.         $query = "SELECT bets.title,  possibilities.name,  userwins.won_credits,  userwins.quote
  1013.             FROM userwins, bets,  possibilities
  1014.             WHERE userwins.user_id = '$user_id'
  1015.             AND userwins.possibilities_id = possibilities.id
  1016.             AND  possibilities.bets_id = bets.id";
  1017.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1018.         $wins_array = array();
  1019.  
  1020.         for ($wins_counter = 0; $wins_counter < (mysql_num_rows($result)) ; $wins_counter++) {
  1021.             $wins_array[$wins_counter] = new Win("",
  1022.                     mysql_result($result, $wins_counter, "title"),
  1023.                     mysql_result($result, $wins_counter, "name"),
  1024.                     mysql_result($result, $wins_counter, "quote"),
  1025.                     mysql_result($result, $wins_counter, "won_credits"));
  1026.         }
  1027.         return $wins_array;
  1028.     }
  1029.  
  1030.  
  1031.     // used during ececute a bet
  1032.     function decBalance($user_id, $credits){
  1033.         $query = "UPDATE user SET balance = (balance - '$credits') WHERE id = '$user_id'";
  1034.         $result = mysql_db_query (DBNAME,$query)  or error_catcher();
  1035.         return $result;
  1036.     }
  1037.  
  1038.     // used when updating a balance manual
  1039.     function setBalance($user_id, $credits){
  1040.         $query = "UPDATE user SET balance = '$credits' WHERE id = '$user_id'";
  1041.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1042.         return $result;
  1043.     }
  1044.  
  1045.     function addBalance($user_id, $credits){
  1046.         $query = "UPDATE user SET balance = balance + '$credits' WHERE id = '$user_id'";
  1047.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1048.         return $result;
  1049.     }
  1050.  
  1051.     function setStatus($user_id, $status){
  1052.         $query = "UPDATE user SET status = '$status' WHERE id = '$user_id'";
  1053.         $result = mysql_db_query (DBNAME,$query)  or error_catcher();
  1054.         return $result;
  1055.     }
  1056.  
  1057.     function setEmail($username, $email){
  1058.         $query = "UPDATE user SET email = '$email' WHERE username = '$username'";
  1059.         $result = mysql_db_query (DBNAME,$query)  or error_catcher();
  1060.         return $result;
  1061.     }
  1062.  
  1063.     function getUserBalance($user_id){
  1064.         $query = "SELECT balance  FROM user WHERE id = '$user_id'";
  1065.         $result = mysql_query ($query) or error_catcher();
  1066.         $rowarray = mysql_fetch_array($result);
  1067.         $balance = $rowarray['balance'];
  1068.         return $balance;
  1069.     }
  1070.  
  1071.  
  1072.     /**
  1073.      * retuns true if the user has not betted on another
  1074.      * possibility before
  1075.      *
  1076.      */
  1077.  
  1078.     function checkBettedPossibility($user_id, $pos){
  1079.  
  1080.         // extract bet_id and possibility_id from $pos 
  1081.         $id_array = explode("#",$pos);
  1082.         $bet_id = $id_array[0];
  1083.         $possibility_id = $id_array[1];
  1084.  
  1085.         $query = "SELECT possibilities.id
  1086.             FROM possibilities, transactions
  1087.             WHERE possibilities.bets_id = '$bet_id'
  1088.             AND transactions.user_id = '$user_id'
  1089.             AND transactions.possibilities_id = possibilities.id";
  1090.  
  1091.         $result = mysql_db_query (DBNAME,$query)  or error_catcher();
  1092.  
  1093.         $rowarray = mysql_fetch_array($result);
  1094.  
  1095.         $allowed_possibility_id = $rowarray['id'];
  1096.  
  1097.         if (($allowed_possibility_id == $possibility_id) || empty($allowed_possibility_id))
  1098.             return true;
  1099.         else
  1100.             return false;
  1101.     }
  1102.  
  1103.  
  1104.     function getAllCategories(){
  1105.         $query = "SELECT * FROM categories ORDER BY position, id asc";
  1106.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1107.         $rowarray = mysql_fetch_array($result);
  1108.  
  1109.         $cat_array = array();
  1110.         for ($cat_counter = 0; $cat_counter < (mysql_num_rows($result)) ; $cat_counter++) {
  1111.             $cat_array[$cat_counter] = new Category(mysql_result($result, $cat_counter, "id"),
  1112.                     mysql_result($result, $cat_counter, "position"),
  1113.                     mysql_result($result, $cat_counter, "name"),
  1114.                     mysql_result($result, $cat_counter, "description"),
  1115.                     mysql_result($result, $cat_counter, "image"));
  1116.         }
  1117.         return $cat_array;
  1118.     }
  1119.  
  1120.     function getCategory($id){
  1121.         $query = "SELECT * FROM categories WHERE id = $id";
  1122.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1123.         $rowarray = mysql_fetch_array($result);
  1124.  
  1125.         $category = new Category($rowarray['id'],
  1126.                 $rowarray['position'],
  1127.                 $rowarray['name'],
  1128.                 $rowarray['description'],
  1129.                 $rowarray['image']);
  1130.         return $category;
  1131.     }
  1132.  
  1133.  
  1134.     function shiftCategoryUp($id){
  1135.  
  1136.         // get pos from category with $id
  1137.         $query = "SELECT position FROM categories WHERE id = $id";
  1138.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1139.         $position = mysql_result($result,0);
  1140.        
  1141.         // get previous position:
  1142.         $query = "SELECT max(position) FROM categories WHERE position < $position";
  1143.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1144.         $prev_position = mysql_result($result,0);
  1145.  
  1146.         // get max position:
  1147.         $query = "SELECT min(position) FROM categories";
  1148.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1149.         $max_position = mysql_result($result,0);
  1150.  
  1151.         if ($position != $max_position){
  1152.  
  1153.         // set previous to actual
  1154.         $query = "UPDATE categories SET position = $position WHERE position = $prev_position";
  1155.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1156.    
  1157.         // set actual to previous
  1158.         $query = "UPDATE categories SET position = $prev_position WHERE id = $id";
  1159.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1160.         }
  1161.     }
  1162.    
  1163.     function shiftCategoryDown($id){
  1164.  
  1165.         // get pos from category with $id
  1166.         $query = "SELECT position FROM categories WHERE id = $id";
  1167.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1168.         $position = mysql_result($result,0);
  1169.        
  1170.         // get next position:
  1171.         $query = "SELECT min(position) FROM categories WHERE position > $position";
  1172.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1173.         $next_position = mysql_result($result,0);
  1174.  
  1175.         // get min position:
  1176.         $query = "SELECT max(position) FROM categories";
  1177.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1178.         $min_position = mysql_result($result,0);
  1179.  
  1180.         if (($position != $min_position)){
  1181.         // set next to actual
  1182.         $query = "UPDATE categories SET position = $position WHERE position = $next_position";
  1183.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1184.    
  1185.         // set actual to next
  1186.         $query = "UPDATE categories SET position = $next_position WHERE id = $id";
  1187.         $result = mysql_db_query (DBNAME,$query) or error_catcher();
  1188.         }
  1189.     }
  1190. }
  1191. ?>
Add Comment
Please, Sign In to add comment