load menu on the header file using codeigniter - php

I'm new to Codeigniter and i have been trying to develop some part using it.
On my header file, i need to load my menu items and i have create a menu controller, menu model and a view.
Controller page
<?php
class Menu extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('menu_model');
}
public function index(){
$data['menuArray'] = $this->mainMenuDataLoad();
if($data['menuArray']){
$this->load->view('menu' , $data);
}
}
public function mainMenuDataLoad(){ /* create menus Array */
$rootMenuData = $this->menu_model->loadManuData();
if($rootMenuData){
for($e=0; $e<count($rootMenuData); $e++){
if($rootMenuData[$e]){
$data[$e] = array(
'title' => $rootMenuData[$e]['title'],
'menu_id' => $rootMenuData[$e]['menu_id'],
'url' => $rootMenuData[$e]['url'],
'menu_icon' => $rootMenuData[$e]['menu_icon'],
);
$get_sub = $this->mainMenuDataLoad($rootMenuData[$e]['menu_id']);
if($get_sub){
$data[$e]['sub'] = $get_sub;
}
}
}
return $data;
}
return false;
} }
this is my model page
class Menu_model extends CI_Model{
public function loadManuData(){
$this->db->select("*");
$this->db->from('tbl_menu');
$this->db->order_by("order", "DESC");
$query = $this->db->get();
if ($query->num_rows() > 0) {
$r=0;
foreach ($query->result() as $row) {
$data[$r]['root_id'] = $row->root_id;
$data[$r]['menu_id'] = $row->menu_id;
$data[$r]['title'] = $row->title;
$data[$r]['url'] = $row->url;
$data[$r]['menu_icon'] = $row->menu_icon;
$r++;
}
return $data;
}
return false;
}
public function __construct(){
parent::__construct();
}}
andon my menu view page i am looping the menu data.
But on my header.php if i try to call the menu controller like this
$this->load->controller('menu');
it gives me an error like this.
Fatal error: Call to undefined method CI_Loader::controller() on header.php
What am i doing wrong?.
Someone please guide me.
thanks in advance
menu.php view Page
<ul class="nav navbar-nav">
<?php
print_r($menuArray);
for($q=1; $q<count($menuArray); $q++){
?><li>
<a href="<?php echo base_url($menuArray[$q]['url']);?>">
<span class="<?php echo $menuArray[$q]['menu_icon'];?>">
<?php echo $menuArray[$q]['title'];?>
</span>
</a>
</li>
<?php }
?>
</ul>

you cant call a controller from view
i.e. in view page writing this code $this->load->controller('menu'); is not permissible.
The controller loads the view and model, its the controller that is the prime here
[More Edit:]
change your model to this
class Menu_model extends CI_Model{
public function __construct(){
parent::__construct();
}
public function get_menu()
{
$this->db->select("*");
$this->db->from('tbl_menu');
$this->db->order_by("order", "DESC");
$query = $this->db->get();
return $query;
}
}?>
then in the controller do this
public function index(){
$data['menuArray'] = $this->menu_model->mainMenuDataLoad()->result_array();
$this->load->view('header', $data);
$this->load->view('menu'); // u are passing data from here//
$this->load->view('landing_page');
$this->load->view('footer');
}
and finally the view
<?php
if(count($menuArray)>0)
{
for($q=0; $q<count($menuArray); $q++){
?><li>
<a href="<?php echo base_url($menuArray[$q]['url']);?>">
<span class="<?php echo $menuArray[$q]['menu_icon'];?>">
<?php echo $menuArray[$q]['title'];?>
</span>
</a>
</li>
<?php }
}?>
You also dont need the public function mainMenuDataLoad() function in controller

Better solution if you make a BaseController with a function and call it from extended controllers.
BaseController:
class BaseController extends CI_Controller
{
protected $data = array();
function __construct() {
parent::__construct();
$this->load->model('my_model');
}
protected function LoadContView($aContentView) {
$this->data['menu'] = $this->my_model->getMenu();
$this->load->view('common/ViHeader', $this->data);
$this->load->view($aContentView, $this->data);
$this->load->view('common/ViSidebar', $this->data);
$this->load->view('common/ViFooter', $this->data);
}
Mypage:
class Mypage extends BaseController{
function __construct() {
parent::__construct();
}
public function index() {
$this->LoadContView('my_view');
}
}
also use foreach( $menu_array as $menu_item) :)

Related

How to pass result from Model to Controller in CodeIgniter?

I want to get data from the database and display it on a webpage using CodeIgniter. I coded my controller, model and view as follows.
Controller;
//HomeController
<?php
class HomeController extends CI_Controller
{
public function index()
{
$this->load->model('HomeModel');
$data['records'] = $this->HomeModel->getData();
$this->load->view('HomeView',$data);
}
}
?>
Model;
//HomeModel
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$query = $this->db->query('SELECT * FROM data');
return $query->unbuffered_row('object');
}
}
?>
View;
//HomeView
<?php
echo "Recoeds from database<br>";
while($records)
{
echo $records->name." ".$records->age."</br>";
}
?>
But this code doesn't print anything on the screen.(echo "Records from database<br>"; )
So I tried the following code given in the CodeIgniter documentation and echoed the result in the model itself rather than return it to the controller and then to the views.It worked fine.
//HomeModel
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$query = $this->db->query('SELECT * FROM data');
while ($row = $query->unbuffered_row())
{
echo $row->name;
echo $row->age;
}
}
}
?>
My question is how do we return the result of the unbuffered_row() method into the controller and then to the view as per MVC architecture? We can get the output by echoing result at the model itself but it is against the purpose of the MVC architecture.
You should use return $query->result(); and then get the right object in the view or controller (that's up to you).
Try This One
controller
<?php
class HomeController extends CI_Controller
{
public function index()
{
$this->load->model('HomeModel');
$data['records'] = $this->HomeModel->getData();
$this->load->view('HomeView',$data);
}
}
?>
Model
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$data = $this->db->query('SELECT * FROM data');
return array('count'=>$data->num_rows(), 'data'=>$data->result(),'first'=>$data->row());
}
}
?>
View
<?php
echo "Recoeds from database<br>";
foreach($record['data'] as $row)
{
echo $row->name." ".$row->age;
}
?>
I simulated your code in local.Your mistake is here.$records is object .And your while loop has not condition for leaving the loop
<pre>
<?php
var_dump($records);
echo "Recoeds from database<br>";
foreach($records as $value) {
echo $value->name."<br>";
echo $value->age."<br>";
}
?>
</pre>

How to Pass ID variable to controller in CodeIgniter

I have a list of messages and when the click on the title it takes them to another view where they can see the expanded message.
This is the view from which I click the link.
Postings View.
Link
Message Controller:
class Message extends CI_Controller {
var $TPL;
public function __construct()
{
parent::__construct();
}
private function display()
{
$query = $this->db->query("SELECT FROM messages WHERE id = '$id';");
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
public function index()
{
$this->display();
}
}
Message View
<?$int=0;?>
<? foreach ($threads as $row) { ?>
<div class="row">
<div class="message">
<h3><?= $row['title']?></h3>
<p><?= $row['message']?></p>
<p><?= $row['member']?></p>
</div>
</div>
<hr>
<? $int++;?>
<? } ?>
This is easy. Change your code as follwoing
Link
Then change you display method as public and send ID param there like
public function display($id){
$this->db->where('id', $id);
$query = $this->db->get('messages');
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
And finally remove $int=0 & $int++; from view file as you are not using this. Now test
Change the link
Link
And in your controller
public function display() {
$id=$this->uri->segment(3);
if($id==null) {
redirect('Index');
}
else {
$this->db->where('id', $id);
$query = $this->db->get('messages');
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
}

How to show retrieve data from database into text field in codeigniter

it's my model code:
<?php
class Books_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_restaurants()
{
$sql = "SELECT id, names FROM restaurants ";
$query = $this->db->query( $sql );
return $query->result();
}
}
controller code:
<?php
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$this->user_data['result']=$this->Books_model->get_restaurants();
$this->load->helper(array('form','url'));
$this->load->view('restaurants/booking',$this->user_data);
}
}
What code I written in view file that the data show in text field?
Try the following :
In the Controller
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$data['results'] = $this->Books_model->get_restaurants();
$this->load->helper(array('form','url'));
$this->load->view('restaurants/booking',$data);
}
}
In the View:
<?php
foreach ($results as $result)
{?>
<label>Restaurant Name : </label>
<input type="text" value="<?php echo $result->names;?>" />
<?php } ?>
in the controller
<?php
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$data["result"]=$this->Books_model->get_restaurants();
//$this->load->helper(array('form','url')); not needed
$this->load->view('restaurants/booking',$data["result"]);
}
}
now in your view
<?php
// notice that CI strip the key "result" from the array $data to become a variable $result in the view
foreach ($result as $row)
{
echo $row->id."<br>";
echo $row->name."<br>";
echo "----";
}
?>
Note:
there is no member like this "$this->user_data['result']" in
codeigniter but there is "$this->session->user_data("data_name")" if
you want to store some data in the session, but then, no need to pass
it to the view as an argument you can call the session data from the
view directly

Codeigniter undefined variable: title , PHP error is encountered

I'm working on simple application consisting of simple form. My code really works fine in local environment. But when i uploaded it on a live server gives an error, unable to fetch database fields and i think there is an error in my model.
Here is my model class
class Model_get extends CI_Model
{
function getData($page) {
$query = $this->db->get_where('ci_tbl', array('page' => $page));
print_r($query->result());
return $query->result();
}
}
Here goes my view
<div id="content">
<?php
foreach ($results as $row) {
$title = $row->title;
$para1 = $row->para1;
$para2 = $row->para2;
}
echo heading($title, 1);
?>
<p><?php echo $title;?></p>
<p><?php echo $para1;?></p>
<p><?php echo $para2;?></p>
</div>
My controller goes as
<?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->model('model_get');
$data['results'] = $this->model_get->getData('home');
$this->load->view('header');
$this->load->view('nav');
$this->load->view('main_content',$data);
$this->load->view('footer');
}
public function about() {
$this->load->model('model_get');
$data['results'] = $this->model_get->getData('about');
$this->load->view('header');
$this->load->view('nav');
$this->load->view('about_page',$data);
$this->load->view('footer');
}
Make sure you actually return the results of the query:
//print_r($query->result());
return $query->result();
At the moment you've commented out return $query->result(); and you're just printing the result. The controller calling the model isn't going to get that information and therefore isn't passing through to the view.
Also check that the database connection is correct if moving from local to a public environment.
Actually you had an error
foreach ($results as $row) {
$title=$ row->title;
^^^^
It should be
$title = $row->title;
^^^
You need to update your query within model as
class Model_get extends CI_Model {
function getData($page) {
$query = $this - > db - > get_where('ci_tbl', array('page' => $page));
return $query->result_array();
}
}

How do I pass isset value to multiple views with codeigniter

I have a isset() value that calculate raw count and it displays the count in admin_messages.php page I want to pass the same value to view_home.php how can I do that?
here is view
<li>
<a href="#">
<i class="icon-home"></i> Inbox
<strong><?php if(isset($count)){echo $count;}?></strong>
</a>
</li>
here is my controller
function messages() {
$data['records'] = $this->mod_contactus->get_records();
$data['count'] =$this->mod_contactus->message_count();
$this->load->view('admin/admin_messages',$data);
}
The controller is your friend here. Try this - it uses the magic method __construct(). Obviously edit this to suit your needs.
<?php
class MyController extends CI_Controller {
private $message_count = 0;
// Code called here is executed when the class is initialised, don't forget to call parent::__construct(); to execute the Codeigniter init code too.
public function __construct() {
parent::__construct();
$this->load->model('mod_contact');
$this->message_count = $this->mod_contact->message_count();
}
public function messages() {
$data['records'] = $this->mod_contactus->get_records();
$data['count'] = $this->message_count;
$this->load->view('admin/admin_messages',$data);
}
public function another_function() {
$data['records'] = $this->mod_contactus->get_records();
$data['count'] = $this->message_count; // same value
$this->load->view('admin/another_function',$data);
}
}

Categories