Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: HKV3

  1. #1
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default HKV3

    Just been bored lately, been playing around with PHP (haven't touched it since March ).

    Figured I'd throw something together..

    Doesn't look that great right now, but I'm just really doing it to play around.. not sure if I'll ever release it.

    Been playing around with method chaining, really pretty neat imo.

    Example:

    PHP Code:
    $data $this->database->select()->where("username"$username)->on("users")->fetch_object(); 
    And

    PHP Code:
    $this->database->update()->where("id"$user_id)->set($field$value)->on("users"); 

  2. #2
    Join Date
    May 2005
    Location
    San Francisco, CA
    Posts
    7,160
    Tokens
    2,331

    Latest Awards:

    Default

    Yeah, method chaning can be really useful when dealing with certain types of classes. It's especially useful with things such as DB drivers (like in your example).

  3. #3
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    Yeah, I store all the wheres, and sets in an array (as sort of a cache), until the on() method is called (which performs the query.. then clears the cache.. then sets the performed query into the class so the num_rows, fetch_array, and fetch_object classes can work with it).

    Not that great, but just something to play around with:

    PHP Code:
    <?php

    class Database
    {

        private 
    $database_details;
        private 
    $cache;
        private 
    $query;

        public function 
    init($database_details)
        {
            if (empty(
    $database_details["server"]) || empty($database_details["username"]) ||
                empty(
    $database_details["name"]))
            {
                
    $this->core->error("Database details incomplete.""database");
            }

            
    $this->database_details $database_details;

            
    $this->connect_database();
            
    $this->select_database();
        }

        private function 
    connect_database()
        {
            
    $connect mysql_connect($this->database_details["server"], $this->
                
    database_details["username"], $this->database_details["password"]);

            if (!
    $connect)
            {
                
    $this->core->error("Could not connect to MySQL server.""database");
            }
        }

        private function 
    select_database()
        {
            
    $select mysql_select_db($this->database_details["name"]);

            if (!
    $select)
            {
                
    $this->core->error("Could not select the database.""database");
            }
        }

        public function 
    select($field '')
        {
            
    $this->cache["type"] = "SELECT";

            
    $field = ($field == '') ? '*' "`{$field}`";

            
    $this->cache["field"] = $field;

            return 
    $this;
        }

        public function 
    update()
        {
            
    $this->cache["type"] = "UPDATE";

            return 
    $this;
        }

        public function 
    where($field$equals)
        {
            
    $this->cache["where"][] = "`{$field}`='{$equals}'";

            return 
    $this;
        }

        public function 
    set($field$equals)
        {
            
    $this->cache["set"][] = "`{$field}`='{$equals}'";

            return 
    $this;
        }

        public function 
    limit($number)
        {
            
    $this->cache["limit"] = "0,{$number}";

            return 
    $this;
        }

        public function 
    order($by$dir)
        {
            
    $dir strtoupper($dir);

            
    $this->cache["order"] = "`{$by} {$dir}`";

            return 
    $this;
        }

        public function 
    on($table)
        {
            switch (
    $this->cache["type"])
            {
                case 
    "SELECT":
                    
    $query "{$this->cache["type"]} {$this->cache["field"]} FROM `{$table}`";
                    break;
                case 
    "UPDATE":
                    
    $query "{$this->cache["type"]} `{$table}`";
                    break;
            }

            if (!empty(
    $this->cache["set"]))
            {
                
    $query .= ' SET';

                
    $number_set 0;
                
    $count_set count($this->cache["set"]);

                foreach (
    $this->cache["set"] as $set)
                {
                    
    $query .= "{$set} ";

                    if (
    $count_set != && $number_set != ($count_set 1))
                    {
                        
    $query .= ', ';
                    }

                    
    $number_set++;
                }
            }

            if (!empty(
    $this->cache["where"]))
            {
                
    $query .= ' WHERE';

                
    $number 0;
                
    $count count($this->cache["where"]);

                foreach (
    $this->cache["where"] as $where)
                {
                    
    $query .= "{$where} ";

                    if (
    $count != && $number != ($count 1))
                    {
                        
    $query .= "AND ";
                    }

                    
    $number++;
                }

            }

            if (
    $this->cache["order"] != "")
            {
                
    $query .= " ORDER BY {$this->cache["order"]}";
            }

            if (
    $this->cache["limit"] != "")
            {
                
    $query .= " LIMIT {$this->cache["limit"]}";
            }

            
    $this->query($query);

            
    $this->cache "";

            return 
    $this;
        }

        public function 
    query($query)
        {
            
    $query = @mysql_query($query);

            
    $this->query $query;

            return 
    $query;
        }

        public function 
    fetch_array($query '')
        {
            
    $query = ($query == '') ? $this->query $query;

            
    $array = @mysql_fetch_array($query);

            return 
    $array;
        }

        public function 
    fetch_object($query '')
        {
            
    $query = ($query == '') ? $this->query $query;

            
    $object = @mysql_fetch_object($query);

            return 
    $object;
        }

        public function 
    num_rows($query '')
        {
            
    $query = ($query == '') ? $this->query $query;

            
    $num_rows = @mysql_num_rows($query);

            return 
    $num_rows;
        }

        public function 
    count_table($table)
        {
            return 
    $this->select()->on($table)->num_rows();
        }

    }
    Last edited by Dentafrice; 08-07-2009 at 03:31 AM.

  4. #4
    Join Date
    May 2005
    Location
    San Francisco, CA
    Posts
    7,160
    Tokens
    2,331

    Latest Awards:

    Default

    Nice! Looks similar to one of my old MySQLi driver classes from a while back in terms of how the sql clauses, etc are stored.

    PHP Code:
    <?php

    class MySQL_Drivers_MySQLi_Select {

        protected static 
    $_instance;
        private 
    $_options;

        public function 
    __construct() {
        
        }
        
        public static function 
    factory() {
        
            if( 
    self::$_instance === NULL ) {
                
                
    self::$_instance = new self();
            
            }
            
            return 
    self::$_instance;
        
        }
        
        public function 
    select$columns ) {
        
            
    $size count$columns );
            
            if( 
    $size <= ) {
            
                if( 
    $columns == '' ) {
                
                    
    $_columns '*';
                
                } else if( 
    $columns == '*' ) {
                
                    
    $_columns '*';
                
                } else {
                    
                    
    $_columns $columns;
                    
                }
                
            } else {
                
                
    $_columns '';
                
    $comma '';
                
                for( 
    $i 0$i $size$i++ ) {
                
                    
    $k $columns$i ];
                
                    
    $_columns .= $comma $k;
                    
    $comma ', ';
                
                }
            
            }
            
            
    $this->_options'columns' ] = $_columns;
            
            return 
    $this;
        
        }

        public function 
    where$statement$value ''$type 'AND' ) {
        
            if( 
    $value instanceof MySQL_Drivers_MySQLi_Select ) {
                        
                
    $statement str_replace'?'$value->__toString(), $statement );

            } else if( 
    is_numeric$value ) !== false ) {
            
                
    $statement str_replace'?'$value$statement );
            
            } else if( 
    is_string$value ) !== false ) {
            
                
    $statement str_replace'?''\'' $value '\''$statement );

            } else {
                
                
    $statement str_replace'?'$value$statement );
               
            }
            
            
    $this->_options'where' ][] = Array(

                
    'statement' => '( ' $statement ' )',
                
    'type'        => $type

            
    );

            return 
    $this;

        }
        
        public function 
    _and$statement$value ) {
        
            
    $this->where$statement$value'AND' );

            return 
    $this;
                
        }

        public function 
    _or$statement$value ) {

            
    $this->where$statement$value'OR' );
            
            return 
    $this;

        }

        public function 
    limit$from$to '' ) {
        
            if( 
    $to !== '' ) {
            
                
    $this->_options'limit' ] = $from ',' $to;
            
            } else {
            
                
    $this->_options'limit' ] = $from;
            
            }
            
            return 
    $this;

        }

        public function 
    order$column$order ) {

            
    $this->_options'order' ][] = Array(

                
    'column' => $column,
                
    'order'     => $order
            
            
    );

            return 
    $this;
                    
        }
        
        public function 
    from$table ) {
            
            
    $this->_options'table' ] = $table;

            return 
    $this;
                
        }
        
        public function 
    join$table$on ''$type 'INNER' ) {
        
            
    $type strtoupper$type );
            
            if( isset( 
    $this->_options'join' ] ) !== false ) {
            
                
    $this->_options'join' ] .= $type ' JOIN ' $table . ( ( $on != '' ) ? ' ON ' $on '' );
            
            } else {
            
                
    $this->_options'join' ] = $type ' JOIN ' $table . ( ( $on != '' ) ? ' ON ' $on '' );
            
            }
            
            return 
    $this;
            
        }
        
        public function 
    __toString() {
        
            if( isset( 
    $this->_options'columns' ] ) !== true || $this->_options'columns' ] == '' || isset( $this->_options'table' ] ) !== true || $this->_options'table' ] == '' ) {
            
                throw new 
    GNR_Exception'Invalid select statement.' );
                
            } else {
            
                
    /**
                 * Basic query
                 */
                
    $query 'SELECT ' $this->_options'columns' ] . ' FROM ' $this->_options'table' ];
                
                if( isset( 
    $this->_options'join' ] ) !== false ) {
                
                    
    $query .= ' ' $this->_options'join' ];
                
                }
                
                
    /**
                 * Where
                 */
                
    $where 0;
                
                if( isset( 
    $this->_options'where' ] ) !== false && count$this->_options'where' ] ) > ) {
                
                    foreach( 
    $this->_options'where' ] as $index => $array ) {
                                        
                        
    $statement    $array'statement' ];
                        
    $type        $array'type' ];
                                            
                        if( 
    $statement != '' ) {
                        
                            if( 
    $where === ) {
                                
                                
    $query .= ' WHERE ';
                            
                            }

                            if( 
    $where ) {
                            
                                
    $query .= ' ' $type ' ';
                            
                            }
                            
                            
    $query .= $statement;
                        
                            
    $where++;

                        }
                    
                    }
                
                }
                
                
    /**
                 * Order by
                 */
                
                
    $order 0;
                
                if( isset( 
    $this->_options'order' ] ) !== false && count$this->_options'order' ] ) > ) {

                    foreach( 
    $this->_options'order' ] as $index => $value ) {

                        
    $column        $value'column' ];
                        
    $orderBy    $value'order' ];

                        if( 
    $column != '' ) {

                            if( 
    $order === ) {

                                
    $query .= ' ORDER BY ';

                            }
                            
                            if( 
    $order ) {

                                
    $order .= ', ';

                            }
                            
                            if( 
    $orderBy !== 'ASC' && $orderBy !== 'DESC' ) {
                            
                                
    $oderBy 'ASC';
                            
                            }

                            
    $query .= '`' $column '` ' $orderBy '';

                            
    $order++;

                        }

                    }

                }
                
                
    /**
                 * Limit
                 */
                
    if( isset( $this->_options'limit' ] ) !== false ) {
                    
                    
    $query .= ' LIMIT ' $this->_options'limit' ];
                
                }
                
                return 
    $query;
            
            }
        
        }

    }

  5. #5
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    Great minds think alike! haha. kidding of course. yours is way more complex and nicer.

  6. #6
    Join Date
    May 2007
    Posts
    10,481
    Tokens
    3,140

    Latest Awards:

    Default

    With what you two old geeks are up to I am going to say...

    Awesome

    Can I has?

    Edit: Dentafrice, is that your car's 'Spec' in your sig?
    Chippiewill.


  7. #7
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    haha it's just me working on it

    Yes, that's my truck's specs lol.

  8. #8

    Default

    I've looked into method chaining before, it's alright but personally I prefer ORM because it's much nicer and easier to manage such as ActiveRecord or DataMapper - should check them out.

    Also Simon there's a typo on line 252 you've typed "$oderBy" instead of "$orderBy".

  9. #9
    Join Date
    Oct 2006
    Location
    Peterborough, UK
    Posts
    3,855
    Tokens
    216

    Latest Awards:

    Default

    Quote Originally Posted by Iszak View Post
    I've looked into method chaining before, it's alright but personally I prefer ORM because it's much nicer and easier to manage such as ActiveRecord or DataMapper - should check them out.

    Also Simon there's a typo on line 252 you've typed "$oderBy" instead of "$orderBy".
    Did you see the part that said old?


    visit my internet web site on the internet
    http://dong.engineer/
    it is just videos by bill wurtz videos you have been warned

  10. #10
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    Quote Originally Posted by Iszak View Post
    I've looked into method chaining before, it's alright but personally I prefer ORM because it's much nicer and easier to manage such as ActiveRecord or DataMapper - should check them out.

    Also Simon there's a typo on line 252 you've typed "$oderBy" instead of "$orderBy".
    I've just AR before. Really nice, but like I said.. I'm just playing around.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •