Welcome Guest, Not a member yet? Register   Sign In
Access functions or session data from a config file
#1

(This post was last modified: 07-24-2015, 06:12 AM by Lykos22.)

Hi all, I'd like to ask a question please.

I'm having the content (title, text etc) of my web pages stored in arrays like this:

PHP Code:
$this->data['homepage_content'] = array(
 array(
 
'id' => 'simple',
 
'icons' => array('icon-1''icon-2''icon-3'),
 
'title' => 'This is my Page Title',
 
'text' =>  array(
 
'Lorem ipsum text lorem ipsum text lorem ipsum text lorem ipsum text lorem ipsum text lorem ipsum text lorem ipsum text lorem ipsum text ' anchor('somelink''blah') . 'lorem ipsum text lorem ipsum text lorem ipsum text'.
 ),
 
'link' => array(
 
'url' => 'somepage',
 
'label' => 'Link text',
 ), 
 )
 ); 

So in my views I echo the array keys in order to display the content.

In order to avoid to put too much content in my controllers, I though to store all this page content in a config file.
My question is if its possible to do something like that in cases where I have some session data or some functions in my content too.

Example:

PHP Code:
$this->data['another_page_content'] = array(
 array(
 
'id' => 'simple',
 
'icons' => array('icon-1''icon-2''icon-3'),
 
'title' => 'Hello ' $this->session->userdata('username'),
 
'text' =>  array(
 
'Lorem ipsum text lorem ipsum text lorem ipsum text lorem ipsum text lorem ipsum text lorem ipsum text lorem ipsum text lorem ipsum text ' anchor('somelink''blah') . 'lorem ipsum text lorem ipsum text lorem ipsum text' my_function('foo'),
 ),
 
'link' => array(
 
'url' => 'somepage',
 
'label' => 'Link text',
 ), 
 )
 ); 
Reply
#2

You probably could do that, as long as everything is in the correct state when you load the file.

Generally, I would lean towards putting the data into a model until I could work out a better way of storing it. Then, once the data is migrated to a storage medium (whether it's a database, a file, or whatever), the model could be updated to retrieve the data and return it to the controller without having to change the remaining code.
Reply
#3

This seems to work in these cases:

PHP Code:
// this is in my config file
$config['some_paragraphs'] = array(
    
'This is a paragraph',
    
'This is another paragraph',
    
'This is another paragraph',
    
'This is another paragraph',
    
'This is another paragraph',
    
'This is another paragraph',
);

// this is in the controller
$this->data['content'] = $this->config->item('some_paragraphs'); 

The problem comes when I try to mix some other config values, or some session data that I want to output with the rest content, for instance:

PHP Code:
// in config file
$config['more_content'] = anchor('homepage','Go to homepage') . ' hello user: ' get_username('admin');

// in helper file
function get_username($role){
    
$CI =& get_instance();

    
$config $CI->config->item($role);
    
$username $CI->session->userdata('username');

    return 
$config[$username];
}

// in the controller
$config['additional_text'] = anchor('logout','Logout') . ' Hello user: ' get_username('user'); 
Reply
#4

(This post was last modified: 07-27-2015, 07:19 AM by ivantcholakov. Edit Reason: Typos )

Within the configuration file instead of

Code:
'title' => 'Hello ' . $this->session->userdata('username'),

place

Code:
'title' => 'Hello {{username}}', // Or some other preferred syntax.

and then within the view replace the template "variable" with the actual username.
Reply
#5

(07-27-2015, 07:18 AM)ivantcholakov Wrote: Within the configuration file instead of


Code:
'title' => 'Hello ' . $this->session->userdata('username'),

place


Code:
'title' => 'Hello {{username}}', // Or some other preferred syntax.

and then within the view replace the template "variable" with the actual username.

Sorry, I 'm not sure if I had understand this correct, could you be more thorough a little bit, please???
Reply
#6

Put a placeholder into the data ({{username}} in his example), retrieve the session data in your controller, then use str_replace() or some other method to replace your placeholder with the session data before sending it to the view.
Reply
#7

Exactly, thank you @mwhitney.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB