TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-30-2008, 10:55 PM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default The Config Class

UPDATE

Heres what I have so far in this config class:

LATEST
PHP Code:
class TalkPHP_Config {

    
// -- Public members -- //
    
public static $configinfo = array();


    
// -- Private members -- //
    
    
private $iSettingID;


    private 
$sSettingname;


    private 
$sSettingvalue;


    
// -- Protected members -- //


    
protected $dbobj;

    
// -- Deprecated
    // protected $query;
    

    
public function __construct($dbObj) {

        if ( 
is_object($dbObj) && $dbObj instanceof DBMysql )
        {
            
$this->dbobj $dbObj;
        } else throw new 
Exception"Database Object failed, error called." );

        
self::grabOptions();

    }

    public function &
option($optionname$optionvalue)
    {

        if ( isset(
$optionname) && isset($optionvalue) )
        {

            
$this->sSettingname  $optionname;
            
$this->sSettingvalue $optionvalue;

        } 
        
        return 
false;

    }

    
/**
     * Create Options
     *
     * @return boolean
     */
    
public function create($optionname$optionvalue)
    {
        
        
$this->sSettingname $optionname;
        
$this->sSettingvalue $optionvalue;
        
        if ( isset(
$this->sSettingname)
        &&  isset(
$this->sSettingvalue) )
        {
            
$this->sSettingname  $this->dbobj->secure($this->sSettingname);
            
$this->sSettingvalue $this->dbobj->secure($this->sSettingvalue);

            
$this->dbobj->exeQuery("INSERT INTO `options` VALUES('','$this->sSettingname','$this->sSettingvalue');");

            return 
true;

        }

        return 
false;

    }

    
/**
     * Remove Options
     *
     * @return boolean
     */
    
public function remove($optionid)
    {
        
        
$this->iSettingID $optionid;

        if ( 
is_numeric($this->iSettingID) )
        {
            
            
$this->dbobj->exeQuery("DELETE FROM `options` WHERE option_id = $this->iSettingID");
            return 
true;
            
        }

        return 
false;
    }

    
/**
     * Edit Options
     *
     * @return boolean
     */
    
public function edit()
    {
        if ( isset(
$this->sSettingname)
        && isset(
$this->sSettingvalue) )
        {
            
$this->sSettingname  $this->dbobj->secure($this->sSettingname);
            
$this->sSettingvalue $this->dbobj->secure($this->sSettingvalue);
            
$name2id self::Name2Id();
            
            
$this->dbobj->exeQuery("UPDATE `options`
            SET `option_name` = '
$this->sSettingname', 
            `option_value` = '
$this->sSettingvalue'
            WHERE option_id = 
$name2id");

            return 
true;

        }
        
        return 
false;
    }

    
/**
     * Grab all the Options
     *
     */
    
private function grabOptions()
    {
        
$options $this->dbobj->exeQuery("SELECT option_id, option_name, option_value FROM `$this->table`");

        if ( 
$options->rows() > )
        {

            while ( 
$options->getFetch(falsetrue) )
            {
                
                
$this->configinfo["$options[option_name]"] = $options[option_value];
                
            }

            return 
true;

        }
        
        return 
false;

    }

    
/**
     * Converts the Name to the Id of the settings
     * 
     * @return int
     */
    
private function Name2Id()
    {

        if ( isset(
$this->sSettingname) )
        {
            
$this->sSettingname $this->dbobj->secure($this->sSettingname);

            
$name2id $this->dbobj->exeQuery("SELECT option_id, option_name FROM `$this->table` WHERE option_name = '$this->sSettingname'");

            if ( 
$name2id->rows() > )
            {
                
$name2id->getFetch(falsetrue);

                return 
$name2id["option_id"];
            }

        } 
//else trigger_error( self::ERR_SETTINGS_NAME_EMPTY,  E_USER_ERROR );

        
return false;
    }


__________________
VillageIdiot can have my babbies ;d

Last edited by Orc : 01-16-2009 at 05:03 AM.
Orc is offline  
Reply With Quote
Old 12-30-2008, 11:30 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Outside of our PM convo (this will go for every coder).

Please code with allman style indents to conform with project standards.
Please do not use sprintf to secure queries, it is an unnecessary layer of processing that takes longer to do than just compiling the query.
__________________

Village Idiot is offline  
Reply With Quote
Old 12-30-2008, 11:31 PM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
Outside of our PM convo (this will go for every coder).

Please code with allman style indents to conform with project standards.
Please do not use sprintf to secure queries, it is an unnecessary layer of processing that takes longer to do than just compiling the query.
sprintf does not secure queries, Tanax's class secures the queries. :P sprintf formats the values in the query string.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-30-2008, 11:33 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
sprintf does not secure queries, Tanax's class secures the queries. :P sprintf formats the values in the query string.
Please don't use sprintf to format the query then; it's not necessary.
__________________

Village Idiot is offline  
Reply With Quote
Old 12-30-2008, 11:34 PM   #5 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
Please don't use sprintf to format the query then; it's not necessary.
How is it not? It makes it a ton easier.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-30-2008, 11:35 PM   #6 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
How is it not? It makes it a ton easier.
Sprintf takes longer to process, I don't see how it makes putting together a string a bit easier.
__________________

Village Idiot is offline  
Reply With Quote
Old 12-30-2008, 11:41 PM   #7 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
Sprintf takes longer to process, I don't see how it makes putting together a string a bit easier.
How long does it take to process? got a benchtest?
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-30-2008, 11:43 PM   #8 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

No, I have no bench marks. But calling a function will always take longer than using a standard operator. I'm sick of arguing with you about this, I could have almost written the class in the time I spend typing these messages. Do you want me to?
__________________

Village Idiot is offline  
Reply With Quote
Old 12-30-2008, 11:46 PM   #9 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
No, I have no bench marks. But calling a function will always take longer than using a standard operator. I'm sick of arguing with you about this, I could have almost written the class in the time I spend typing these messages. Do you want me to?
So why not allow it to take longer, it doesn't hurt :P

Not really, I'm taking the opportunity to do it, and you seem so strict at things.. You sound a bit arrogant.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-30-2008, 11:54 PM   #10 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
So why not allow it to take longer, it doesn't hurt :P
It doesn't help either. When given a choice between two methods, neither proposing any real advantage over the other, you pick the faster one.

Quote:
Originally Posted by Orc View Post
Not really, I'm taking the opportunity to do it, and you seem so strict at things.. You sound a bit arrogant.
I am trying to get standard procedure set up so our code looks the same across the script. Once we get basic things like that in place I won't be micro-managing people like I seem to be doing now.

As for being strict, I am only strict on general things like indentation and general structure. Once we get to smaller things, I wont care. I won't even look at the source most of the time when we move forward. However, when starting a project, what kind of project leader am I if I make no decisions? We saw where not being decisive got us for a month or so. I am sorry if I come across as arrogant, but I don't want to be asked for proof when I give a reason as simple as it takes longer (I'm always more compelled to answer "I'll do it, any particular reason you feel that way?" replies).
__________________

Village Idiot is offline  
Reply With Quote
Old 12-31-2008, 12:13 AM   #11 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
It doesn't help either. When given a choice between two methods, neither proposing any real advantage over the other, you pick the faster one.


I am trying to get standard procedure set up so our code looks the same across the script. Once we get basic things like that in place I won't be micro-managing people like I seem to be doing now.

As for being strict, I am only strict on general things like indentation and general structure. Once we get to smaller things, I wont care. I won't even look at the source most of the time when we move forward. However, when starting a project, what kind of project leader am I if I make no decisions? We saw where not being decisive got us for a month or so. I am sorry if I come across as arrogant, but I don't want to be asked for proof when I give a reason as simple as it takes longer (I'm always more compelled to answer "I'll do it, any particular reason you feel that way?" replies).

lol read pm
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-31-2008, 12:15 AM   #12 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Already replied to it.

I have decided to let the coders compile queries however they want, I will later review the code and see if they should be standardized. Now I have to finish packing for my trip, I may not be on till tomorrow morning where I will finalize my replacement.
__________________

Village Idiot is offline  
Reply With Quote
Old 12-31-2008, 02:03 AM   #13 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

PHP Code:
// -- I can't exactly understand Tanax's Class :/ -- //
            
$this->configinfo $this->dbobj
                           
->exeQuery($this->queryformat)
                            ->
loadFetch(true,$this->queryformat); 
You don't need to use $this->queryformat in your loadFetch if you've already executed the query with the exeQuery function.

Also, I think(since you're assigning the fetched results to a variable) you need to use the getFetch().

You could do like this:
PHP Code:
            $this->configinfo $this->dbobj
                           
->exeQuery($this->queryformat)
                            ->
loadFetch(true)->getFetch(); 
or like this:
PHP Code:
            $this->configinfo $this->dbobj
                            
->loadFetch(true,$this->queryformat)->getFetch(); 
or even like this:
PHP Code:
            $this->configinfo $this->dbobj
                            
->getFetch(truetrue$this->queryformat);
// 1st true is because you didn't use the loadFetch.
// 2nd true is because you want assoc
// 3rd parameter is the sql 
Hope you understand more now. Read my SQL thread if not! Or PM me.
__________________
Tanax is offline  
Reply With Quote
Old 12-31-2008, 02:07 AM   #14 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
PHP Code:
// -- I can't exactly understand Tanax's Class :/ -- //
            
$this->configinfo $this->dbobj
                           
->exeQuery($this->queryformat)
                            ->
loadFetch(true,$this->queryformat); 
You don't need to use $this->queryformat in your loadFetch if you've already executed the query with the exeQuery function.

Also, I think(since you're assigning the fetched results to a variable) you need to use the getFetch().

You could do like this:
PHP Code:
            $this->configinfo $this->dbobj
                           
->exeQuery($this->queryformat)
                            ->
loadFetch(true)->getFetch(); 
or like this:
PHP Code:
            $this->configinfo $this->dbobj
                            
->loadFetch(true,$this->queryformat)->getFetch(); 
or even like this:
PHP Code:
            $this->configinfo $this->dbobj
                            
->getFetch(truetrue$this->queryformat);
// 1st true is because you didn't use the loadFetch.
// 2nd true is because you want assoc
// 3rd parameter is the sql 
Hope you understand more now. Read my SQL thread if not! Or PM me.

I rewrote a lot of the code of my config class now so no need, plus you told me in a pm how to do it correctly. :P oh and you still haven't told me how to actually call the array elements. ;P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-31-2008, 02:18 AM   #15 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

How do you mean? What array elements? How to use the array you've fetched?
Should be something like this:

PHP Code:
//The simple way of getting a fetch:
$this->configinfo $this->dbobj->getFetch(truetrue$this->queryformat);

//Check something, or whatever you want:
if($this->configinfo['ActiveTemplate'] == 5)
{

     
//Do something..

Should be like that!
__________________
Tanax is offline  
Reply With Quote
Old 12-31-2008, 02:22 AM   #16 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
How do you mean? What array elements? How to use the array you've fetched?
Should be something like this:

PHP Code:
//The simple way of getting a fetch:
$this->configinfo $this->dbobj->getFetch(truetrue$this->queryformat);

//Check something, or whatever you want:
if($this->configinfo['ActiveTemplate'] == 5)
{

     
//Do something..

Should be like that!
Oh I see.. I'm stupid.. Nevermind.. Sigh..
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-31-2008, 02:24 AM   #17 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

One more thing though!
On your createConfig function, you set the SQL, but you don't execute it with the DB, is it supposed to be like that?
__________________
Tanax is offline  
Reply With Quote
Old 12-31-2008, 02:47 AM   #18 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
One more thing though!
On your createConfig function, you set the SQL, but you don't execute it with the DB, is it supposed to be like that?
I've removed createConfig function, and the othe config functions, cause it wasn't what Village Idiot wanted, so once I'm done with the new code, I'll update the op. sound good?
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-31-2008, 12:34 PM   #19 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Ah, I see. Great!
__________________
Tanax is offline  
Reply With Quote
Old 01-16-2009, 04:23 AM   #20 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Updated (chars)
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 04:23 PM


All times are GMT. The time now is 05:47 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design