I am somewhat confused on the proper way to perform the following. Is this a class a function or an object? (Not sure) So here goes (please be kind) I'm learning codeigniter/php simultaneously.
$is_live = $this->session->userdata('email');
$is_live = ucwords($is_live);
if (!$is_live == null)
echo 'Hello, '.$is_live.
' <b>(Not you? '.'<a href="' .base_url().
'main/logout">Log Out</a>) </b>';
I wrote this code, which checks if a session is set and if true echo the details. I currently have this code on my view/main
However I want to have this code displayed on all pages.
What is the proper way to do this?
- Do i create a
view/header
which is auto loaded on each page and insert thiscode
there? - Do I create a
class/public function
in ahelper
file and load it where needed? - Do I create a
class/public function
in alibrary
file and load it where needed? - Do I create a
public function
in a controller?
-EXTRA-
My assumption is option 1
but I'm curious what if I want to display this information in another location on a particular page? I don't want to have to copy/paste the code block because that is sloppy.
How would I set it up so that I can just call it where i need it?
e.g. $this->load->helper('check_for_sess');
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isLive
{
public function is_live() {
$is_live = $this->session->userdata('email');
$is_live = ucwords($is_live);
if (!$is_live == null)
echo 'Hello, '.$is_live.
' <b>(Not you? '.'<a href="' .base_url().'main/logout">Log Out</a>) </b>';
}
}
On the view
I tried this:
<?php
$isLive = new isLive;
$isLive->is_live();
?>
This however doesn't work and causes a ' function userdata() on a non-object error '
Can someone please explain the proper way to achieve my desired solution and the correct syntax. Thanks in advance!
-UPDATE-- Tried to create a library - still getting error.
#Created a library
class CheckSess
{
function IsLive()
{
$is_live = $this->session->userdata('email');
$is_live = ucwords($is_live);
if (!$is_live == null)
{
$check_msg = 'Hello, '.$is_live.
' <b>(Not you? '.'<a href="' .base_url().'main/logout">Log Out</a>) </b>';
}
return $check_msg;
}
}
#In the View
<?php echo $this->CheckSess->IsLive(); ?>
#in the controller
$this->load->library('CheckSess');
--UPDATE--
class CheckSess {
function IsLive()
{
$CI =& get_instance();
$is_live = $CI->session->userdata('email');
$is_live = ucwords($is_live);
if (!$is_live == null)
{
return 'Hello, '.$is_live.
' <b>(Not you? '.'<a href="' .base_url().'main/logout">Log Out</a>) </b>';
}
}
}
Set this^ is the libraries
folder
the view
shows <?php echo $this->CheckSess->IsLive(); ?>
and the controller
shows $this->load->library('CheckSess');
please advise. Thanks again!
ERROR---
( ! ) SCREAM: Error suppression ignored for ( ! ) Fatal error: Call to a member function IsLive() on a non-object in C:\wampserver\www\test-app\application\views\homepage.php on line 56 Call Stack
Time Memory Function Location
1 0.0005 151432 {main}( ) ..\index.php:0 2 0.0014 187792 require_once( 'C:\wampserver\www\test-app\system\core\CodeIgniter.php' ) ..\index.php:202 3 0.0277 1537000 call_user_func_array ( ) ..\CodeIgniter.php:359 4 0.0277 1537048 Main->index( ) ..\CodeIgniter.php:359 5 0.0283 1540200 CI_Loader->view( ) ..\main.php:8 6 0.0283 1540640 CI_Loader->_ci_load( ) ..\Loader.php:419 7 0.0287 1566584 include( 'C:\wampserver\www\test-app\application\views\homepage.php' ) ..\Loader.php:833