Welcome Guest, Not a member yet? Register   Sign In
Where to store static array ?
#2

(This post was last modified: 04-26-2017, 11:54 AM by PaulD.)

You could put them in a config file and use them as required. Personally I would put them in a countries model and call them as required.

For instance, create a file called Countries_model.php in you application/models directory:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Countries_model extends CI_Model {
    
    public 
$countries_list = array(
        
'England',
        
'America',
        
'France',
        
'Germany',
    );

    public function 
get_countries() { 
        
// return full list of countries
        
return $countries_list;
    }
    


Then in any controller:
PHP Code:
// load model
$this->load->model('countries_model');

// get countries list
$countries $this->countries_model->get_countries(); 

If you ever have to update your countries list you now only ever have to update it in one place. Your controllers remain clean and readable and models are easy to test too.

Or instead of a model you can do a similar thing in a library, or create your own helper. The docs are very good on all this.

Hope that helps,

Best wishes,

Paul.

PS Models are very, very cool and powerful and good usage makes your code super easy to modify and update. I often have more models than controllers :-)
Reply


Messages In This Thread
Where to store static array ? - by Coool6 - 04-26-2017, 11:38 AM
RE: Where to store static array ? - by PaulD - 04-26-2017, 11:50 AM
RE: Where to store static array ? - by kilishan - 04-26-2017, 12:00 PM
RE: Where to store static array ? - by PaulD - 04-26-2017, 12:36 PM
RE: Where to store static array ? - by Coool6 - 04-26-2017, 01:05 PM
RE: Where to store static array ? - by kilishan - 04-26-2017, 01:21 PM
RE: Where to store static array ? - by Coool6 - 04-26-2017, 01:39 PM
RE: Where to store static array ? - by marksman - 04-26-2017, 02:06 PM



Theme © iAndrew 2016 - Forum software by © MyBB