Navigation with Codeigneter Framework - php

Basicly I have a contreller that named site.php , and have views:header.php,nav.php,content.php,footer.php etc. problem is how can run content_about.php?
Im trying this url:site/about but I get an error on browser!
Code is that:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class site extends CI_Controller {
public function index()
{
$this->home();
}
public function home()
{
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_home");
$this->load->view("site_footer");
}
public function about()
{
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_about");
$this->load->view("site_footer");
}
}

Try this one.
public function about()
{
$data=array();
$data['main']='content_about'; //only the content part without header,nav and footer
$this->load->view('template',$data);
}
in view make template.php and put this lines
<?=$this->load->view('site_header.php');?>
<?=$this->load->view('site_nav.php');?>
<?=$this->load->view($main);?>
<?=$this->load->view('site_footer');?>
please let me know if you face any problem.

Related

using codeigniter,admin wants to approve user's registration request. But itsn't happening properly

here is my controller page..
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class ApproveUser extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('admin/Approve_Model');
}
public function index()
{
$this->load->model('admin/Approve_Model');
$result['data']=$this->Approve_Model->getView();
if(!empty($this->session->userdata('user_id')))
{
$this->load->view('admin/header');
$this->load->view('admin/approveUser',$result);
$this->load->view('admin/footer');
}
else{
redirect(site_url('Pages/index'));
}
}
while running the code, it is redirecting to my index page
Try this:
if(!isset($this->session->userdata('user_id')))
{
$this->load->view('admin/header');
$this->load->view('admin/approveUser',$result);
$this->load->view('admin/footer');
}
else
{
redirect(site_url('Pages/index'));
}

Dynamic data in codeigniter

I am new in codeigniter and i am working on ecommerce template. By default whenever index() has been hit, it's showing all products in different sections (in html) and I want to make this dynamic so should I use queries in index() for get different type of records or there is any other way? This is my code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
//echo "hello world";
$this->load->view('index');
}
}
?>
First need to create database and table as per your requirement.
Then you can create model for reference check this link : https://www.codeigniter.com/userguide3/general/models.html
<?php
class Blog_model extends CI_Model {
public function get_data_first()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
public function get_data_second()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
}
?>
After making model go to your controller and include it like:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
$this->load->model('my_model');
$data['first_list'] = $this->my_model->get_data_first();
$data['second_list'] = $this->my_model->get_data_second();
//echo "hello world";
$this->load->view('index', $data);
}
}
?>
Then use you param in index file like :
<html>
<head></head>
<body>
<?php
if($first_list){
foreach($first_list as $each){
echo $each->my_param;
}
}
?>
<?php
if($second_list){
foreach($second_list as $each){
echo $each->my_param2;
}
}
?>
</body>
</html>
I hope this helpful for you.
In Models folder write Home_model.php file for querying database.
Suppose you have a table called 'products'.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home_model extends CI_Model{
public function __construct()
{
parent::__construct();
}
public function getProducts()
{
$this->db->from('products');
$query=$this->db->get();
$out = $query->result_array();
return $out;
}
}
The function 'getProducts' will get you all the products from 'products' table.
Now in your controller load the 'database' library and the model.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('home_model');
$this->load->library('database');
}
public function index(){
$products = array();
$products = $this->home_model->getProducts();
$this->load->view('index',$products);
}
}
In index function function of controller you can call the 'getProduct' function of model.And can pass the data to view.
Documentation link for model.
enter link description here
I hope this helps.

Calling controller/action is not working as expected?

Click link as controller/function (signout/signout) is not working .. I mean if I click link controller/action it's not going to that called function.But controller/action/action (signout/signout/signout) is working in my CI framework..
//not working
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
//working
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
MY CONTROLLER Signout.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Signout extends CI_Controller {
public function __construct() {
// Call the CI_Model constructor
parent::__construct();
$this->db = $this->load->database('default', true);
}
public function index() {
}
public function signout() {
$this->load->view("signout_signout");
}
}
Here I want controller/action(signout/signout) to work ! Because 2 worked method url(signout/signout/signout) is a little long ,so don't like it . How can i manage that?
Call signout() from your index() action. Change:
public function index() {
}
to
public function index() {
$this->signout();
}
or use this and eliminate signout() altogether:
public function index() {
$this->load->view("signout_signout");
}
It was working fine as i check your code.
Try test this code and see
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function welcome()
{
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
}
public function signout()
{
echo "signout";
}
public function index()
{
}
}

CodeIgniter, header controller is not loading index method

Firstly I am getting no errors, I am trying to create an is_logged_in() method in my header model in Code Igniter, but nothing in the index method of the controller will load. I added die(); into it and even that wont execute, Here is my code:
header.php - controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Header extends CI_Controller {
public function index() {
print_r($this->session->all_userdata());
$data = array();
$data['title'] = 'Wenso - Timesheet';
$username = $this->session->userdata('username');
$this->load->view('template/header', $data);
$this->load->model('header_model');
$is_logged_in = $this->header_model->is_logged_in($username);
die($is_logged_in);
}
}
header_model.php - Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Header_model extends CI_Model {
public function is_logged_in($username){
$q = $this
->db
->where('email_address', $username)
->limit(1)
->get('users');
die($q->last_query());
if($q->row('username') != $username){
return FALSE;
} else {
return TRUE;
}
}
}
Note: none of the die() functions in my code work.. Anything I add into the index function of the controller (which to my understanding is loaded by default) does not get executed...
Thanks in advance
public function __construct() { parent::__construct(); }
Add this method at your model else you wont have $this->db loaded
As AdrienXL pointed out the controller is only loaded whern the url /controller_name is called.. This wasn;t the case in my user case scenario.
Also something worth pointing out as Sevtilo mentioned above if you create a construct method in CodeIgniter you ovewrite the dafult calls for things such as $this->db class, using:
public function __construct() {
parent::__contsruct();
}
Will get the parent classes contsructor.
Regards
Ric
If you want to call this code transparently (ie without having to put any extra mess in the uri) then move the code into the constructor of an extension called MY_Controller.php in application/core that looks a bit like this...
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
print_r($this->session->all_userdata());
$data = array(); $data['title'] = 'Wenso - Timesheet';
$username = $this->session->userdata('username');
$this->load->view('template/header', $data);
$this->load->model('header_model');
$is_logged_in = $this->header_model->is_logged_in($username);
die($is_logged_in);
}
}
And then in your application/controllers files extend this class like
class Some_controller extends MY_Controller{
function __construct (){
parent::__construct();
}
public function index(){
//your header code will be run before this or any other method in this class
}
}
And the code from MY_Controller.php will run before any of your methods.

Passing common title to all view and models CodeIgniter

I have this controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('text');
}
public function index()
{
$this->home();
}
public function home()
{
$data['title']="Somesite";
$this->load->view("view_home", $data);
}
public function blog()
{
$data['title']="Somesite";
$this->load->view("view_blog", $data);
}
public function answers()
{
$data['title']="Somesite";
$this->load->view("view_answers", $data);
}
}
As you may see $data['title'] is same for all functions, how to make it simpler, to include at the beggining and not to write it in every function, repeat again, and then transfer to view.
Is there a way to tranfer it to function?
Before construct function add this:
public $data = array();
Then in the construct function write:
$this->data['title']="Somesite";
And finally before load view add this:
$data = $this->data + $data;
Now you have same $title everywhere.
Here si simple solution and elegant for transfering one variable to all views :)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
//Class-wide variable to store stats line
protected $title;
function __construct()
{
parent::__construct();
$data->title = "Some site";
$this->load->vars($data);
}
I'm using this method in every projects.
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
//Global variable
public $outputData = array();
public $loggedInUser;
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->helper('url');
$this->load->view('users/users');
}
public function register() {
parent::__construct();
$this->load->helper('url');
$this->load->model('users_model');
//get country names
$countryList = $this->users_model->getCountries();
$this->outputData['countryList'] = $countryList;
$this->outputData['pageTitle'] = "User Registration";
$this->load->view('users/register',$this->outputData);
}
}
View file
<?php if(isset($pageTitle)) echo $pageTitle; ?>
<?php
if(isset($countryList)){
print_r($countryList);
}
?>

Categories