How to save and access reusable data arrays in codeigniter?
I am relatively new to codeigniter and am struggling to understand how would save and access data from multiple controllers so was wondeing if anyone here could help me understand this.
I need to create a function inside codeigniter which returns a data array for me to pass to a view and create a form dropdown.
Below is the code that will be used in the function
switch($type) {
case 'text':
$data = array (
'equals' => 'Equals',
'notequal' => 'Does Not Equal',
'startswith' => 'Starts With',
'endswith' => 'Ends With',
'contains' => 'Contains',
'notcontain' => 'Does Not Contain',
);
break;
case 'numeric':
$data = array (
'equals' => 'Equals',
'notequal' => 'Does Not Equal',
'less' => 'Less Than',
'greater' => 'Greater Than',
'equalless' => 'Less Than Or Equal To',
'equalgreater' => 'Greater Than Or Equal To'
);
break;
case 'date':
$data = array (
'equals' => 'Equals',
'notequal' => 'Does Not Equal',
'before' => 'Before',
'after' => 'After',
'equalbefore' => 'Before Or Equal To',
'equ开发者_运维技巧alafter' => 'After Or Equal To'
);
break;
}
I will then be passing the data array that is returned from this function into a view using it to create a form dropdown using the following code:
echo form_dropdown('conditions', $data);
I am not sure whether I need to create a helper for this or something else?
Thanks for looking
Still not exactly sure what you're trying to accomplish, but generally in the past for dropdown content that I need to use many times across a project, I'll contain the values within a helper.
like this...
conditions_helper.php
<? if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('hotel_types_dropdown')) {
function date_dropdown_values() {
$data = array (
'equals' => 'Equals',
'notequal' => 'Does Not Equal',
'before' => 'Before',
'after' => 'After',
'equalbefore' => 'Before Or Equal To',
'equalafter' => 'After Or Equal To'
);
return $data;
}
and you would continue to format your different dropdown setups with corresponding function names.
So once you add it to your autoload.php or load it within the controller you want to use it in. Just call it with date_dropdown_values()
and it will return your array for use in a dropdown.
Then in your controller...
switch($type) {
case 'text':
$data->dropdown_values = text_dropdown_values();
break;
case 'numeric':
$data->dropdown_values = numeric_dropdown_values();
break;
case 'date':
$data->dropdown_values = date_dropdown_values();
break;
}
$this->load->view('path/to/view', $data);
And then in your view
<?php echo form_dropdown('conditions', $dropdown_values); ?>
精彩评论