Storing and accessing settings from database query

I am trying to figure out the best way to accomplish this so feel free to criticize my thought process.

My tool has different locales and each locale can have different settings for the tool. For example, Arizona could have the top 5 stats turned on where as California does not.

I pull all of the settings based on the locale and have an array of the data:

    SimpleXMLElement Object
(
    [settings] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [settingName] => Home Page Stats
                    [settingDescription] => Show the Top 5 Teammate / Teamleader stats?
                    [requireValue] => 0
                    [localeID] => 14
                    [status] => 1
                    [value] => SimpleXMLElement Object
                        (
                        )

                    [settingID] => 3
                )

            [1] => SimpleXMLElement Object
                (
                    [settingName] => Test
                    [settingDescription] => Testing
                    [requireValue] => 1
                    [localeID] => 14
                    [status] => 1
                    [value] => 66
                    [settingID] => 5
                )

        )

)

What I need to be able to do though is get information based on a current setting. They will never be in the same order so I can’t do it by location in the array. I need to be able to access the data based on the setting name.

I have tried a few things with iterating over the array and getting the value I need at that time but it seems like its not a great way to do it.

Is there a better way to store / access this data?

I just need to be able to say If setting name "Home page stats has a status of 1 then then get me its value.

I feel like I may be making this more complicating than it should be.

Have you tried setting a key in an associative array? Each site get’s a unique identifier and you grab the site specific value with $o->setting[$siteID] would give you the proper setting object? Granted it would be possible for code running on a site to potentially query the settings for a different site. If [settings] was also a SimpleXMLElement Object you could use and xpath query to find the right element.

SimpleXMLElement Object
(
    [settings] => Array
        (
            [Home Page] => SimpleXMLElement Object
                (
                    [settingName] => Home Page Stats
                    [settingDescription] => Show the Top 5 Teammate / Teamleader stats?
                    [requireValue] => 0
                    [localeID] => 14
                    [status] => 1
                    [value] => SimpleXMLElement Object
                        (
                        )

                    [settingID] => 3
                )

            [Test] => SimpleXMLElement Object
                (
                    [settingName] => Test
                    [settingDescription] => Testing
                    [requireValue] => 1
                    [localeID] => 14
                    [status] => 1
                    [value] => 66
                    [settingID] => 5
                )
            [Another Site] => SimpleXMLElement Object
                (
                    [settingName] => Another Site
                    [settingDescription] => Another Site
                    [requireValue] => 1
                    [localeID] => 14
                    [status] => 1
                    [value] => 66
                    [settingID] => 5
                )
        )
)

Beyond that, have you thought about having a small config file (.ini, .json, or .xml) for each site and just read it as you load the site? Or move to a pure SimpleXML structure instead of intermixing an array into it? It would make the whole structure traversable with xpath (though it may be more process intensive than the functionality is worth).

This seems like the perfect application of the strategy pattern. The basic idea is that you have a bunch of different strategies (these are database connection settings in you scenario) and based on a condition (this is the locale in your scenario) you execute a specific strategy.

The best example I’ve seen to describe this pattern has to do with a checkout application that needs to calculate tax based on your country of origin. There are lots of ways to implement this. The example below uses an if statement based on your country. If you needed to support 2 or 3 countries, then this would suffice. If you needed to support 20 countries, then you would want to put the logic into different objects.

class TaxStrategy {
    public double CalculateTaxByCountry(String country, double amount) {
       if (country equals "Canada") {
           return 0.12 * amount;
       }
       else if (country equals ("USA")) {
           return 0.05 * amount;
       }
    }
}

class Main {
    public static void main(String[] args) {
        TaxStrategy taxStrategy = new TaxStrategy();
        String countryOfOrigin = "Canada"; // you would get this from the user
        double amount = 15.84;
        double tax = taxStrategy.CalculateTaxByCountry(countryOfOrigin);
    }
}

More information on this pattern can be found here: http://www.oodesign.com/strategy-pattern.html There is an example in this link that shows how to implement it with classes. The example I provided shows the basic idea of how the pattern works on its simplest level.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật