TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   TalkPHP Developer Team (http://www.talkphp.com/talkphp-developer-team/)
-   -   The Config Class (http://www.talkphp.com/talkphp-developer-team/3820-config-class.html)

Orc 12-30-2008 10:55 PM

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;
    }




Village Idiot 12-30-2008 11:30 PM

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.

Orc 12-30-2008 11:31 PM

Quote:

Originally Posted by Village Idiot (Post 20892)
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.

Village Idiot 12-30-2008 11:33 PM

Quote:

Originally Posted by Orc (Post 20893)
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.

Orc 12-30-2008 11:34 PM

Quote:

Originally Posted by Village Idiot (Post 20894)
Please don't use sprintf to format the query then; it's not necessary.

How is it not? It makes it a ton easier.

Village Idiot 12-30-2008 11:35 PM

Quote:

Originally Posted by Orc (Post 20895)
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.

Orc 12-30-2008 11:41 PM

Quote:

Originally Posted by Village Idiot (Post 20896)
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?

Village Idiot 12-30-2008 11:43 PM

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?

Orc 12-30-2008 11:46 PM

Quote:

Originally Posted by Village Idiot (Post 20898)
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. *!*

Village Idiot 12-30-2008 11:54 PM

Quote:

Originally Posted by Orc (Post 20899)
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 (Post 20899)
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).

Orc 12-31-2008 12:13 AM

Quote:

Originally Posted by Village Idiot (Post 20900)
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

Village Idiot 12-31-2008 12:15 AM

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.

Tanax 12-31-2008 02:03 AM

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.

Orc 12-31-2008 02:07 AM

Quote:

Originally Posted by Tanax (Post 20908)
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

Tanax 12-31-2008 02:18 AM

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!

Orc 12-31-2008 02:22 AM

Quote:

Originally Posted by Tanax (Post 20910)
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..

Tanax 12-31-2008 02:24 AM

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?

Orc 12-31-2008 02:47 AM

Quote:

Originally Posted by Tanax (Post 20912)
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?

Tanax 12-31-2008 12:34 PM

Ah, I see. Great! ^^

Orc 01-16-2009 04:23 AM

Updated (chars)


All times are GMT. The time now is 05:36 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0