codeigniter

umaxim
umaxim used Ask the Experts™
on
Hi i just begin to study codeigniter and i make my own cms so i need to create a function to take information from database and put in on header like name of website name of the page. I do not want to copy the same 10 line of code over and over in each file. I just want to create some function so i can run it and it automaticaly will put all that information to the array. In regular php with out framework i was just create a regular function but what you can recomend me to do in this type of framework.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
A good introduction to CI is at SitePoint. If you are new to CI I would recommend reading it to get an understanding of how everything fits together. Also keep a bookmark to the CI user guide handy, it's very well documented and includes example usage for each function.

First you will need to create a database table with your site details. You will need to edit application/config/database.php and set your DB hostname, DB name, username and password.

My database is called "site" with table "header"
id  |  sitename
1   |  Test CMS  

Open in new window


Next create a file called application/models/home_model.php
<?php

class Home_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
	}

	function get_header()
	{
		$this->db->select('sitename');
		$this->db->from('header');

		return $this->db->get();
	}
}

Open in new window


Next create a view for your header HTML in application/views/site_header.php
<html>
<head>
<title><?php echo $header->sitename; ?></title>
</head>
<body>
<h1><?php echo $header->sitename; ?></h1>

Open in new window


Should also create a footer view to close off the tags in application/views/site_footer.php
</body>
</html>

Open in new window


Now you need a controller to tie your model and view together in application/controllers/home.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->model('Home_model');
        }

	public function index()
	{
		$res = $this->Home_model->get_header();
		$header['header'] = $res->row();
		
		$this->load->view('site_header', $header);
		$this->load->view('site_footer');
	}
}

Open in new window


You should then be able to browse to http://yourserver/ci_install/index.php/home and see that your title is Test CMS and you have a H1 in the page body also saying Test CMS.
also try this simple tutorial,

cms

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial