<div class="collapse navbar-collapse" id="navbar-collapse-1">
<ul class="nav navbar-nav">
<?php
$this->load->model('cms_model');
if (isset($article_data)) {
} else {
$article_data = $this->cms_model->get_end_user_cms_menu(); (this line gives error)
}
foreach ($article_data as $row) { ?>
<li class="dropdown"> <a href='<?php echo site_url('endusers/end_cms_controller/cms_type_selected?type_id=' . $row->type_id) ?>'>
<?php echo $row->type_name; ?>
</a> </li>
<?php } ?>
</ul>
<div class="nav-search-wrap">
<input class="txt-search" type="text" name="search" placeholder="Search">
</div>
</div><!-- /.navbar-collapse -->
This is the code from my view file it givs the error on line number i highlighted. The error is " Fatal error: Call to a member function get_end_user_cms_menu() on a non-object in E:\xampp\htdocs\Santulan\application\views\endusers\header.php on line 158"
About the other problem: The issue was inside your controller. I post the corrected version with explanatory comments:
public function index(){
$str=$this->input->post('search');
$search_id=$this->cms_model->search_tag_id($str);
//echo $id= $search_id; -> you cannot echo $search_id becouse it's an array: $query->result() from search_tag_id() model function.
//$query->result() is allways an array
//FOR DEBUGGING: when you want to see inside $query->result(), use: echo '<pre>'.print_r($query->result(), true).'</pre>';
// instead you use:
$id = $search_id->article_id;
$search_data['article']=$this->cms_model->search_article($id);// function for search article
if($search_data){
$this->load->view('endusers/cms_view', $search_data); // this is ok, if you call for $article and $article_image inside the view
}else{
echo "No result found";
}
}
First, you must call $this->load->model('cms_model'); in your controller, not in your view.
Then, in the controller also, not in your view, you could do:
if (!isset($article_data)) {
$article_data = $this->cms_model->get_end_user_cms_menu();
}
$data['list'] = '';
foreach ($article_data as $row){
$data['list'] .= "<li class=\"dropdown\"> $row->type_name </li>";
}
Then, also in your controller, when you load the view, add $data:
$this->load->view('your_viwe', $data);
And in your view:
<div class="collapse navbar-collapse" id="navbar-collapse-1">
<ul class="nav navbar-nav">
<?php echo $list;?>
</ul>
<div class="nav-search-wrap">
<input class="txt-search" type="text" name="search" placeholder="Search">
</div>
</div>
And your-re good to go.
Related
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():
}
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);
I want to fetch some data (student details) from database but my code is not working.
This is my Controller
public function Student_Detail()
{
$student_id = $this->uri->segment(3);
$record = $this->WebAdmin_Model->Student_Details($student_id);
$this->load->view('admin/template/header.php');
$this->load->view('admin/students/student_details', $record);
$this->load->view('admin/template/footer.php');
}
This is my Model
public function Student_Details($student_id)
{
$query = $this->db->query("SELECT s.`student_id`,
s.`std_email`,
s.`std_fname`,
s.`std_lname`
FROM `student_main` AS s
WHERE s.`student_id` = $student_id");
return $query->result();
}
This is my View
<section class="panel">
<div class="user-heading">
<img src="<?=base_url();?>images/profile-avatar.jpg" alt="">
</div>
<ul class="nav nav-pills nav-stacked">
<li> <?php echo $record->std_fname; ?></li>
<li> <?php echo $record->std_lname; ?></li>
<li> <?php echo $record->std_email; ?></li>
</ul>
</section>
Note that there is not problem with the query. I want to know how to fetch student details. It gives me the following error. Message : Undefined variable: record
Try this:
In your controller:
$data['record'] = $this->WebAdmin_Model->Student_Details($student_id);
$this->load->view('admin/students/student_details', $data);
And in your view:
<ul class="nav nav-pills nav-stacked">
<?php
foreach($record->$row){
?>
<li> <?php echo $row->std_fname; ?></li>
<li> <?php echo $row->std_lname; ?></li>
<li> <?php echo $row->std_email; ?></li>
<?php
}
?>
</ul>
You need to pass your model data into record array then pass into view
Controller
$student_id = $this->uri->segment(3);
$record['data'] = $this->WebAdmin_Model->Student_Details($student_id);// store data into record array
$this->load->view('admin/template/header.php');
$this->load->view('admin/students/student_details', $record);
$this->load->view('admin/template/footer.php');
Use foreach loop to retrieve data
Views
<?php
foreach($data as $record)// use foreach loop
{ ?>
<li> <?php echo $record->std_fname; ?></li>
<li> <?php echo $record->std_lname; ?></li>
<li> <?php echo $record->std_email; ?></li>
<?php } ?>
You need to do it like this, because codeigniter try to extract variable from $record and it fails. So to make possible, make it,
$record['record'] = $this->WebAdmin_Model->Student_Details($student_id);
and then in your view,
<ul class="nav nav-pills nav-stacked">
<li> <?php echo $record->std_fname; ?></li>
<li> <?php echo $record->std_lname; ?></li>
<li> <?php echo $record->std_email; ?></li>
</ul>
Because codeigniter will extract variable from $record array
please help me, i've problem for get a value of database
in one page i called result() like code below :
<li><i class="fa fa-twitter"></i> Your Bird
<ul>
<?php foreach ($burung->result() as $burung) {
echo "<li>$burung->burung</li>";
}?>
</ul>
</li>
and next code :
<div class="row">
<?php foreach ($burung.result() as $burung) {
echo "
<div class=\"col-md-3\">
<div class=\"panel panel-default\">
<div class=\"panel-body bk-primary text-light\">
<div class=\"stat-panel text-center\">
<div class=\"stat-panel-number h1 \">24</div>
<div class=\"stat-panel-title text-uppercase\">$burung->burung</div>
</div>
</div>
Full Detail <i class=\"fa fa-arrow-right\"></i>
</div>
</div>";
}?>
</div>
but the when i run this page doesn't work with message :
Call to undefined function result()
Change this
<?php foreach ($burung.result() as $burung) {
to this
<?php foreach ($burung->result_array() as $row) {
and inside code $row['burung']
Use/Change result_array() instead of result(). Read this Result Arrays in Codeigniter.com
I'm trying to create a Mega Menu using PHP and I'm having a problem getting the structure to output correctly. I've hard-coded the Mega Menu to test everything and it works fine, but obviously I need PHP to create it for me.
I have an example with the Mega Menu hard-coded so everyone can see what I'm trying to create:
http://www.libertyeaglearms.com/dev
Or here's the code:
DESIRED OUTPUT:
<div id="wrapper">
<ul class="mega-menu">
<li class="mega-menu-drop">
Firearms
<div class="mega-menu-content">
<div class="column">
<h4>Rifles</h4>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
<div class="column">
<h4>Handguns</h4>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
<div class="column">
<h4>Shotguns</h4>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
</div>
</li>
<li class="mega-menu-drop">
Archery
<div class="mega-menu-content">
<div class="column">
<h4>Bows</h4>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
<div class="column">
<h4>Arrows</h4>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
</div>
</li>
</ul>
</div>
CURRENT OUTPUT: (very messed up. be warned, lol)
<div id="wrapper">
<ul class="mega-menu">
<li class="mega-menu-drop">
archery
<div class="mega-menu-content">
<div class="column">
<h4>compound</h4>
<ul>
<h4>bows</h4>
<ul>
</div>
</li>
<li class="mega-menu-drop">
firearms
<div class="mega-menu-content">
<div class="column">
<h4>rifle ammunition</h4>
<ul>
<h4>ammunition</h4>
<ul>
</div>
</li>
</ul>
</div>
HERE MY PHP:
$json = json_decode($category->buildDepartments(NULL),true);
function buildDepartments($array,$parent)
{
$html = '';
foreach($array as $category)
{
if($category['parent'] == $parent)
{
if($category['parent'] == NULL)
{
$html .= '<li class="mega-menu-drop">' . "\n\t\t\t\t";
$html .= ''.$category['category_name'].'' . "\n\t\t\t\t";
$html .= '<div class="mega-menu-content">' . "\n\t\t\t\t\t";
$html .= '<div class="column">' . "\n\t\t\t\t\t\t";
$html .= buildDepartments($array,$category['category_id']);
$html .= '</div>' . "\n\t\t\t";
$html .= '</li>' . "\n\t\t\t";
}
else
{
$html .= buildDepartments($array,$category['category_id']);
$html .= '<h4>'.$category['category_name'].'</h4>' . "\n\t\t\t\t\t\t";
$html .= '<ul>' . "\n\t\t\t\t";
}
}
}
return $html;
}
print(buildDepartments($json,NULL));
HERE'S MY DATABSE:
EDIT AFTER BOUNTY
Building off what icktoofay suggested, I cannot seem to figure out the foreach() loops. The problem I'm getting is I get the department name inside the mega menu when I should see category and sub categories. I think the problem is I need to perform a loop with a specific parent id in order to get all the children, but I'm not really sure if that's the problem. Here's the code:
<div id="wrapper">
<ul class="mega-menu">
<?php $json = json_decode($category->buildDepartments(NULL)); ?>
<?php foreach($json as $category): ?>
<li class="mega-menu-drop">
<?php if($category->parent === NULL){echo $category->category_name;} ?>
<div class="mega-menu-content">
<?php foreach($category as $subcategory): ?>
<div class="column">
<?php if($category->category_id === $category->parent){echo '<h4>' . $category->category_name . '</h4>';}?>
<ul>
<?php foreach($category as $subcategory)
{
echo '<li>' . $category->category_name . '</li>';
}
?>
</ul>
</div>
<?php endforeach; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
Since it's a fixed depth and the format for each level is different, recursion may not be appropriate. Additionally, putting everything in strings is inelegant. Instead, you may want to try weaving the HTML and loops together like this:
<div id="wrapper">
<ul class="mega-menu">
<?php foreach($categories as $category): ?>
<li class="mega-menu-drop">
<?php echo $category->name; ?>
<div class="mega-menu-content">
<?php foreach($category->subcategories as $subcategory): ?>
<!-- and so on -->
<?php endforeach; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
The code I've used here assumes you've already got it from a sort-of-flat-array to a tree-like structure. For example, if we've got a Category class:
class Category {
public $id;
public $parentID;
public $name;
public $parent;
public $subcategories;
public function __construct($id, $parentID, $name) {
$this->id = $id;
$this->parentID = $parentID;
$this->name = $name;
$this->parent = null; // will be filled in later
$this->subcategories = array(); // will be filled in later
}
}
And an array of associative arrays like you might get from a database call (which we'll call $flatCategories), we can build a bunch of not-yet-connected Category instances like this:
$categories = array();
foreach($flatCategories as $flatCategory) {
$categories[$flatCategory['id']] =
new Category($flatCategory['category_id'],
$flatCategory['parent'],
$flatCategory['category_name']);
}
Then we can connect them all up into a hierarchal structure:
foreach($categories as $category) {
if($category->parentID !== null) {
$category->parent = $categories[$category->parentID];
$category->parent->subcategories[] = $category;
}
}
Now they're all linked up and we only care about keeping references to the roots:
$roots = array();
// This could, of course, be merged into the last loop,
// but I didn't for clarity.
foreach($categories as $category) {
if($category->parentID === null) {
$roots[] = $category;
}
}
Now $roots contains all the root categories, and it's fairly simple to traverse. In my example at the top, I was assuming $categories had something similar to $roots in it.
you need to generate something called a super tree and use a recursive function to echo out each branch, this will allow for an unlimited depth tree, but, it also allows for an unknown depth tree.
First, SQL:
SELECT category_id, parent, category_name
Second, use SQL to create tree:
$super_tree = array();
while(($row = mysql_fetch_assoc($res)) != NULL) {
$parent = $row['parent'] == NULL ? 0 : $row['parent']; //clean up the parent
$super_tree[$parent][$row['category_id']] = $row['category_name'];
}
Third, echo out the tree
function echo_branch($tree_branch, $tree_root) {
echo("<ul>\n"); //open up our list for this branch
foreach($tree_branch as $id => $name) { //foreach leaf on the branch
echo("<li>"); //create a list item
echo("{$name}"); //echo out our link
if(!empty($tree_root[$id])) { //if our branch has any sub branches, echo those now
echo_branch($tree_root[$id], $tree_root); //pass the new branch, plus the root
}
echo("</li>\n"); //close off this item
}
echo("</ul>\n"); //close off this list
}
echo_branch($super_tree[0], $super_tree); //echo out unlimited depth tree structure
this allows for unlimited depth in your tree structure and allows for simple code to be able to create the structure within the code base.
All that you would need to do now is add in your extra's such as classes and your extra html elements in the correct places.
if you are looking to track the current depth of the tree to be able to echo different things depending on the depth, you can make the following alterations
In the function definition
function echo_branch($tree_branch, $tree_root, $depth) {
In the recursive call within the if
echo_branch($tree_root[$id], $tree_root, $depth++);
In the initial call
echo_branch($super_tree[0], $super_tree, 0);
Look like you are using the variable
$category
when you should use
$subcategory
Try this:
<div id="wrapper">
<ul class="mega-menu">
<?php $json = json_decode($category->buildDepartments(NULL)); ?>
<?php foreach($json as $category): ?>
<li class="mega-menu-drop">
<?php if($category->parent === NULL){echo $category->category_name;} ?>
<div class="mega-menu-content">
<?php foreach($category as $subcategory): ?>
<div class="column">
<?php if($subcategory->category_id === $category->parent){echo '<h4>' . $category->category_name . '</h4>';}?>
<ul>
<?php foreach($subcategory as $subcategory2)
{
echo '<li>' . $subcategory2->category_name . '</li>';
}
?>
</ul>
</div>
<?php endforeach; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>