How to assign html code to variable in codeigniter? - php

I want to assign some html code to variable and that variable is in controller.
headerview.php
<header id="header" class="header">
<div class="container">
<!-- LOGO -->
<div class="logo"><img src="<?php echo base_url()?>public/images/logo.png" alt=""></div>
<!-- END / LOGO -->
<!-- NAVIGATION -->
<nav class="navigation">
<div class="open-menu">
<span class="item item-1"></span>
<span class="item item-2"></span>
<span class="item item-3"></span>
</div>
<!-- MENU -->
<ul class="menu">
<?php if($isAdmin ){ ?>
<?php
if($title=='Dashboard'){
?>
<li class="current-menu-item">Admin Dashboard</li>
<?php
}else{
?>
<li>Admin Dashboard</li>
<?php
}
?>
<?php } else { ?>
<?php
if($title=='Dashboard'){
?>
<li class="current-menu-item">User Dashboard</li>
<?php
}else{
?>
<li>User Dashboard</li>
<?php
}
?>
<?php } ?>
<?php
if($title=='Courses' || $title=='Add/Edit Categories' || $title=='Upload Manager'){
?>
<li class="current-menu-item">
Courses
<ul class="sub-menu">
<li>Categories</li>
<li>Courses</li>
<li>Upload Manager</li>
</ul>
</li>
<?php
}else{
?>
<li>
Courses
<ul class="sub-menu">
<li>Categories</li>
<li>Courses</li>
<li>Upload Manager</li>
</ul>
</li>
<?php
}
?>
<?php
if($title=='Users' || $title=='User Roles' || $title=='Subscription'){
?>
<li class="current-menu-item">
Users
<ul class="sub-menu">
<li>Roles</li>
<li>Subscription</li>
</ul>
</li>
<?php
}else{
?>
<li>
Users
<ul class="sub-menu">
<li>Roles</li>
<li>Subscription</li>
</ul>
</li>
<?php
}
?>
<?php
if($title=='Sales' || $title=='Payout Details'){
?>
<li class="current-menu-item">
Sales
<ul class="sub-menu">
<li>Transactions</li>
<li>Payouts</li>
</ul>
</li>
<?php
}else{
?>
<li>
Sales
<ul class="sub-menu">
<li>Transactions</li>
<li>Payouts</li>
</ul>
</li>
<?php
}
?>
</ul>
<!-- END / MENU -->
<!-- LIST ACCOUNT INFO -->
<ul class="list-account-info">
<!-- MESSAGE INFO -->
<!-- END / MESSAGE INFO -->
<!-- NOTIFICATION -->
<!-- END / NOTIFICATION -->
<li class="list-item account">
<?php
$userId=$this->session->userdata('cp_adminid');
$profilePic="";
$userQ=$this->Adminmodel->getuser();
foreach ($userQ->result() as $rowuser){
$profilePic=$rowuser->profilePic;
}
?>
<div class="account-info item-click">
<?php if($profilePic!=''){ ?>
<img alt="" src="<?php echo base_url();?>private/<?php echo $userId;?>/<?php echo $profilePic;?>">
<?php }else{?>
<img src="<?php echo base_url()?>public/images/default-profile_pic.png" alt="">
<?php }?>
</div>
<div class="toggle-account toggle-list">
<ul class="list-account">
<li><i class="icon md-config"></i>Setting</li>
<li><i class="icon md-arrow-right"></i>Sign Out</li>
</ul>
</div>
</li>
</ul>
<!-- END / LIST ACCOUNT INFO -->
</nav>
<!-- END / NAVIGATION -->
</div>
</header>
This header is come to all pages in application. So I want it to assign some variable called headerContent and it is located in Admin_controller. That render function called from any another controller that extends Admin_controller and then it loads the headeview to that pages.
Admin_controller
class Admin_controller extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model("Adminmodel","",true);
$headerview = 'headerview';
$this->render($headerview); # calling render() function in same class
}
protected $headerview = 'headerview';
protected function render($headerContent) {
$view_data = array( 'headerContent' => $headerContent);
$this->load->view($this->headerview);
}
}
Another controller below that extends Admin_controller called this render function.
Adminhome_controller.php
<?php
require APPPATH . '/controllers/admin/Admin_controller.php';
class Adminhome extends Admin_Controller{
public function __construct(){
parent::__construct();
$this->load->model("Adminmodel","",true);
public function index(){
$data['content']=$this->load->view("admin/adminhomeview",'',true);
$data['title']='Dashboard';
$adminId=$this->session->userdata('cp_adminid');
$permissions=$this->Adminmodel->getpermissions($adminId);
$row = $permissions->row();
$data['isAdmin'] = 0;
if($row->view == 1 or $row->add == 1 or $row->edit == 1 or $row->deleteRole == 1 ){
$data['isAdmin'] = 1;
}
$this->render($headerContent);
// $this->load->view("admin/headerview",$data);
}
}
?>
But it showing error like Message: Undefined variable: headerContent in Adminhome_controller.php.

you culd create a template view file containing
template view:
<html>
<?php $data = array('permission' => $permission); ?>
<?php $this->load->view('templateincludes/header',$data); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('templateincludes/footer'); ?>
</html>
controler:
$data = array('permission' => '1', 'main_content' => 'folder_to_your_view/view_file_you_want_to_load');
$this->load->view('template', $data);
header view:
<?= $permission ?>

But it showing error like Message: Undefined variable: headerContent
in Adminhome_controller.php
There is no variable with the name $headerContent inside extended class. Seems like you want access the parent/base class's variable.
To overcome this, you can declare protected field inside Parent Controller (Admin_controller.php), and access it inside child controller :
class Admin_controller extends CI_Controller{
protected $headerContent; // declare protected field
protected $headerview = 'headerview';
function __construct()
{
parent::__construct();
$this->load->model("Adminmodel","",true);
$headerview = 'headerview';
$this->render($headerview); # calling render() function in same class
}
protected function render($headerContent) {
$view_data = array( 'headerContent' => $headerContent);
$this->headerContent = $headerContent; // assign value into $headerContent field
$this->load->view($this->headerview);
}
}
And you can access it directly inside child class (Adminhome_controller.php) by using :
$this->render($this->headerContent);

Related

My route somehow keep redirecting to wrong url

so
i try make a pos(point of sale) program using ci3
im trying add a feature where admin can make new menu and submenu
but something went wrong
when i goto controller "sistem"
my route show "sitename/sistem/sistem/menu" this route supposed to "sitename/sistem/menu". another controller also effected when i goto "dashboard" controller the route show
"sistem/dashboard"
this is sistem controller
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Sistem extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('M_menu');
$this->load->model('M_user');
if (!$this->session->userdata('email')) {
redirect('auth');
}
}
public function index()
{
$this->menu();
}
public function menu()
{
$data['judulpage'] = 'Manajemen Menu';
$data['datamenu'] = $this->M_menu->getmenus()->result_array();
$data['user'] = $this->M_user->getuserdata()->row_array();
$this->form_validation->set_rules('menutambah', 'Menu', 'required');
// $this->form_validation->set_message([
// 'required' => 'Field menu add harus diisi'
// ]);
if ($this->form_validation->run() == false) {
$this->load->view('template/header', $data);
$this->load->view('template/sidebar', $data);
$this->load->view('sistem/menu', $data);
$this->load->view('template/footer');
} else {
$menu['menu'] = $this->input->post('menutambah');
$this->M_menu->insertmenu($menu);
$this->session->set_flashdata('flash','Sukses');
redirect('sistem');
}
}
this is the sidebar.php i think this is the problem but i cant find the solution
<?php
$role_id = $this->session->userdata('role_id');
$queryMenu = "SELECT `user_menu`.`id`, `menu`, `icon_menu`
FROM `user_menu`
JOIN `user_access_menu` ON `user_menu`.`id` = `user_access_menu`.`menu_id`
WHERE `user_access_menu`.`role_id` = $role_id";
$menus = $this->db->query($queryMenu)->result_array();
?>
<!-- BEGIN: Main Menu-->
<div class="main-menu menu-fixed menu-dark menu-accordion menu-shadow" data-scroll-to-active="true">
<div class="main-menu-content">
<ul class="navigation navigation-main" id="main-menu-navigation" data-menu="menu-navigation">
<li class=" navigation-header"><span>MENU</span><i class=" feather icon-minus" data-toggle="tooltip" data-placement="right" data-original-title="Apps"></i>
</li>
<?php foreach($menus as $menu) : ?>
<?php
$menuid = $menu['id'];
$querysubmenu = "SELECT * FROM `user_sub_menu` WHERE `menu_id` = $menuid AND `is_active` = 1";
$submenus = $this->db->query($querysubmenu)->result_array();
?>
<li class=" nav-item"><i class="<?php echo $menu['icon_menu']; ?>"></i><span class="menu-title"><?php echo strtoupper($menu['menu']); ?></span>
<ul class="menu-content">
<?php foreach($submenus as $submenu) : ?>
<li>
<a class="menu-item" href="<?php echo $submenu['url']; ?>"><i class="<?php echo $submenu['icon']; ?>"></i><span><?php echo $submenu['title']; ?></span></a>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
i take the url from database.
u can see from inspect mode, the href redirecting to sistem/menu which is coorect url but when i click it, it goto sistem/sistem/menu
You do not need to call double <?php ?> tag.
just do this
<?php echo base_url($submenu['url']); ?>
or simplify the echo syntax with
<?= base_url($submenu['url']); ?>
fixed href="<?php echo base_url(); ?> i add / this before php tag but somehow it worked but still directing to wrong url localhost/sistem/menu it supposed to localhost/site/sistem/menu, so i add base_url to add my sitename and it work, thx
<li>
<a class="menu-item" href="<?php echo base_url(); ?><?php echo $submenu['url']; ?>">
<i class="<?php echo $submenu['icon']; ?>"></i><span><?php echo $submenu['title']; ?></span></a>
</li>

sending data view to controller in codeigniter

my view
<div class="dropdown1">
<button class="dropbtn1">Asia</button>
<div class="dropdown1-content">
<li>
<ul style="height:520px; overflow: auto">
<?php foreach ($result as $row) {?>
<li>
<a href="<?php echo base_url(); ?>index.php/MyWeb_Controller/countriesView/<?php echo $row['count_name']?>">
<?php echo $row['count_name'];?>
</a>
</li>
<?php } ?>
</ul>
</li>
</div>
</div>
my controller
public function countriesView($a){
$this->load->helper('url');
$this->load->view('Country_View');
var_dump($a);die();
}
the result is show like this
string(12) "%20%20BRUNEI"
i want to show only the country name 'BRUNEI'
You can use - (dash) instant of space because space value change in url like %20%. Try the following code i have replace space to - in view and revert it on controller
View :
<div class="dropdown1">
<button class="dropbtn1">Asia</button>
<div class="dropdown1-content">
<li>
<ul style="height:520px; overflow: auto">
<?php foreach ($result as $row) {?>
<li>
<?php $county_name = str_replace(' ','-',$row['count_name']); ?>
<a href="<?php echo base_url(); ?>index.php/MyWeb_Controller/countriesView/<?php echo $county_name; ?>">
<?php echo $row['count_name'];?>
</a>
</li>
<?php } ?>
</ul>
</li>
</div>
</div>
Controller :
public function countriesView($a){
$this->load->helper('url');
$this->load->view('Country_View');
$a = str_replace('-',' ',$a);
var_dump($a);die():
}

Get value of a button to change in CodeIgniter using logic from controller?

I'm trying to change the value of a button between login/logout depending on if a session is in progress. The logic works perfectly. The issue I have is sending the actual "login" or "logout" label from the controller (Navigation) to the view (navigation). How do I do this? I have tried sending over TPL but the button is blank (although functions upon being clicked).
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// start the session
if(isset($_SESSION)){
session_start();
}
var $TPL = array();
if(isset($_SESSION["accesslevel"])){
$this->TPL["outPut"] = "Logout";
}
else{
$this->TPL["outPut"] = "Login";
}
class Navigation extends CI_Controller {
public function index()
{
$this->template->show('navigation');
}
}
view:
<ul>
<li>Home</li>
<li>Members</li>
<li>Admin</li>
<li><?php $outPut ?></li>
</ul>
Is $output accessible in your view?
You need to pass $TPL from controller to your view and Write session code inside constructor.
Constructor :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Navigation extends CI_Controller {
private $TPL = array();
function __construct() {
parent::__construct();
if(isset($_SESSION["accesslevel"])){
$this->TPL["outPut"] = "Logout";
}
else{
$this->TPL["outPut"] = "Login";
}
}
public function index()
{
$this->template->show('navigation', $this->TPL);
}
}
why don't check in view if there's a session? if the answer is yes that means that the user is logged and you'll show him the log-out button, otherwise you'll show him the login button.
<?php if ($this->session->userdata('id')){ ?>
<div class="float-right">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<?php echo anchor('User/user_profile/'.$this->session->userdata('id'), 'My profile', array('class' => 'nav-link')); ?>
</li>
<li class="nav-item">
<?php echo anchor('User/logout', 'Log out', array('class' => 'nav-link')); ?>
</li>
</ul>
</div>
<?php }else{ ?>
<div class="float-right">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<?php echo anchor('User/login', 'Login', array('class' => 'nav-link')); ?>
</li>
<li class="nav-item">
<?php echo anchor('User/register', 'Register', array('class' => 'nav-link')); ?>
</li>
</ul>
</div>
<?php } ?>

ZF2 pagination Passing Value From Route

Within my Zend Framework project I have a "base-asset" module. Within this I am trying to create a pagination that will match the following route:
'route' => '/video/search[/:search][/:page_reference]',
The purpose of the value of search is so the same query will run on the database if the user clicks on any of the "next" page links created.
The value for page_reference is working fine. It is the value for search that I am not sure of how to populate for the paginate:
<li>
<a href="<?php echo $this->url(
$this->route,
['page_reference' => $this->previous , 'search' => $this->search]
); ?>"> << </a>
</li>
The following attempts have been unsuccessful:
$this->search
$this->params()->fromRoute('search')
The pagination is called from the view using
{{ paginationControl(result, 'sliding', ['actsministries/asset/searchpaginator', 'AMBase']) }}
Should I be setting the value for search in the Service?
1 - Create a view helper :
use Zend\View\Helper\AbstractHelper;
class Requesthelper extends AbstractHelper
{
protected $request;
//get Request
public function setRequest($request)
{
$this->request = $request;
}
public function getRequest()
{
return $this->request;
}
public function __invoke()
{
return $this->getRequest()->getServer()->get('QUERY_STRING');
}
}
2 - Module.php
public function getViewHelperConfig()
{
return array(
'invokables' => array(
),
'factories' => array(
'Requesthelper' => function($sm){
$helper = new YourModule\View\Helper\Requesthelper; \\the path to creatd view helper
$request = $sm->getServiceLocator()->get('Request');
$helper->setRequest($request);
return $helper;
}
),
);
}
3 - searchpaginator.phtml :
<?php
$parameterGet = $this->Requesthelper();
if ($parameterGet != ""){
$aParams = explode("&", $parameterGet);
foreach($aParams as $key => $value){
if(strpos($value, "page_reference=") !== false){
unset($aParams[$key]);
}
}
$parameterGet = implode("&", $aParams);
if($parameterGet != '')
$parameterGet = "&".$parameterGet;
}
?>
<?php if ($this->pageCount):?>
<div>
<ul class="pagination">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<li>
<a href="<?php echo $this->url($this->route); ?>?page_reference=<?php echo $this->previous.$parameterGet; ?>">
<<
</a>
</li>
<?php else: ?>
<li class="disabled">
<a href="#">
<<
</a>
</li>
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<li>
<a href="<?php echo $this->url($this->route);?>?page_reference=<?php echo $page.$parameterGet; ?>">
<?php echo $page; ?>
</a>
</li>
<?php else: ?>
<li class="active">
<?php echo $page; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<li>
<a href="<?php echo $this->url($this->route); ?>?page_reference=<?php echo $this->next.$parameterGet; ?>">
>>
</a>
</li>
<?php else: ?>
<li class="disabled">
<a href="#">
>>
</a>
</li>
<?php endif; ?>
</ul>
</div>
<?php endif; ?>
4 - index.phtml
<?php
// add at the end of the file after the table
echo $this->paginationControl(
// the paginator object
$this->paginator,
// the scrolling style
'sliding',
// the partial to use to render the control
'partial/searchpaginator.phtml',
// the route to link to when a user clicks a control link
array(
'route' => '/video/search'
)
);
?>
I hope that's what you look for

Category with Subcategory PHP/MySQL

i have MySql Table name 'category', in that table i have
id catname parent_id
1 animals
2 vegs
3 dog 1
4 cat 1
5 carrot 2
i just wanna display this data in html nested 'ul' like
<ul>
<li>Animals
<ul>
<li>dog</li>
<li>cat</li>
</ul>
</li>
<li>Vegs
<ul>
<li>Carrot</li>
</ul>
</li>
</ul>
with php, please help me to get this data with php(CodeIgniter) and display.
Here are the three components for your question: controller, model and view...
<!-- THE VIEW -->
<ul>
<?php foreach($main_cat->result() as $rowm) : ?>
<li><?php echo $rowm->catname ?>
<ul>
<?php
$id = $rowm->id;
$sec_cat = $this->yourmodel->get_secondary($id);
foreach($sec_cat->result() as $rows) :
?>
<li><?php echo $rows->catname ?></li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
<!-- THE CONTROLLER -->
<?php
class Welcome extends CI_Controller {
function index()
{
$data['main_cat'] = $this->yourmodel->get_main();
$this->load->view('welcome_view',$data);
}
}
?>
<!-- THE MODEL -->
<?php
class Yourmodel extends CI_Model {
function get_main()
{
$this->db->where('parent_id','');
$query = $this->db->get('category');
return $query;
}
function get_secondary($parent)
{
$this->db->where('parent_id',$parent);
$query = $this->db->get('category');
return $query;
}
}
?>

Categories