-
Notifications
You must be signed in to change notification settings - Fork 0
Database Views
How would you like it if you could save your views in the database?
You could save all your views in the db or just views you would like to give users access to change. I also save my flat files like Privacy Policy, ToS, About Us and success page etc in the db.
[code]
CREATE TABLE view
(
id
int(11) NOT NULL auto_increment,
slug
varchar(20) NOT NULL,
Title
varchar(200) NOT NULL,
Body
longtext NOT NULL,
Groups
tinyint(4) NOT NULL,
PRIMARY KEY (id
),
UNIQUE KEY slug
(slug
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO view
(id
, slug
, Title
, Body
, Groups
) VALUES (1, 'privacy_policy', 'Privacy Policy', '
Controller file called dbview.php: [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class DbView extends Controller {
function setVars(){
$this->tpl['head'] = '';
$this->slug = $this->uri->segment(3, FALSE) ;
$this->uid = $this->session->userdata('uid');
}
function __construct()
{
parent::Controller();
$this->setVars();
}
function pages()
{
$this->getdb();
$this->title = $this->dbdata->Title;
$this->tpl['body'] = $this->phpWrapper($this->dbdata->Body);
$this->tpl['title'] = $this->title;
$this->load->view('layout/blank', $this->tpl);
}
function getdb()
{
if($this->slug == FALSE){
show_error('No direct script access allowed');
}else{
$query = $this->db->getwhere('view', array('slug' => $this->slug), 1);
if($query->num_rows() == 0) {
show_404('Page name');
}else{
$dbdata = $query->row_array();
$this->vid = $dbdata['id'];
$this->dbdata = (object) delids($dbdata);
}
}
}
function phpWrapper($content) {
ob_start();
$content = str_replace('<'.'?php','<'.'?',$content);
eval('?'.'>'.trim($content).'<'.'?');
$content = ob_get_contents();
ob_end_clean();
return $content;
}
} ?> [/code] I saved this in a helper file and do autoload arrays_helper.php [code] /************************** START UNSET/DEL IDS ****************************
- Removes id, *_id and also first and last date fields from database array ****************************************************************************/ function delids($array) { $array = (array) $array; if(isset($array[0])){ foreach($array as $k => $v){ $array[$k] = unsetids($v); } }else{ $array = unsetids($array); } return $array; }
function unsetids($array) { $keys = array_keys($array); foreach($keys as $v){ if($v=='id')unset($array[$v]); if($v=='last')unset($array[$v]); if($v=='first')unset($array[$v]); if(strstr($v, '_id'))unset($array[$v]); } return $array; } [/code]
If you need help or even better have improvements let me know, please. I watch this thread: [url=http://www.codeigniter.com/forums/viewthread/50756/]dbView Forum[/url]