Codeigniter undefined variable: title , PHP error is encountered - php

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();
}
}

Related

fetch database records in codeigniter

I'm learning CodeIgniter From this link. I've a little confusion in fetching the data from database.
<h2><?php echo $title; ?></h2>
<?php foreach ($news as $news_item): ?>
<h3><?php echo $news_item['title']; ?></h3>
<div class="main">
<?php echo $news_item['text']; ?>
</div>
<p>View article</p>
While displaying the data in view they have used a variable $news in foreach loop. However this variable is never defined anywhere
Copied from here
$query = $this->db->get('mytable');
// Produces: SELECT * FROM mytable
The second and third parameters enable you to set a limit and offset clause:
$query = $this->db->get('mytable', 10, 20);
// Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
You'll notice that the above function is assigned to a variable named $query, which can be used to show the results:
$query = $this->db->get('mytable');
foreach ($query->result_array() as $row)
{
echo $row['title'];
}
Try like this hope this will give you what you want simply make a controller and call the associated model in the constructor and perform the function what your want it will return the array so you will be eassilly fetch and use it in your view.
Controller code
class TestController extends Controller{
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('User');
}
public function index(){
$data['all_user']=$this->User->getAllUser();
$this->load->view('index',$data);
}
}
this is my model code
class User extends CI_Model{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getAllUser()
{
$this->db->select('*');
$this->db->from('users');
return $this->db->get()->result();
}
}
just follow the steps
make a controller
make a function inside the controller
call the associated model function into it and you will definately get the result.
Have you seen this Controller code ? (above the code that you pick, in the tutorial)
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
$news is passed through as $data element from the Controller to the View

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>

REST app + codeigniter + database

I did a REST app + codeigniter + database following: https://github.com/chriskacerguis/codeigniter-restserver , works perfect, wonderful.
Right now my json file getting static data, how can i get data from database?
i wrote a file and i can get from database the data, but i have no idea how to get the data from database in json, can you guys help me? thank you.
That file return to me that static json, works perfect, I would like get data from database.
/controllers/hello.php
<?php
include (APPPATH.'/libraries/REST_Controller.php');
class Hello extends REST_Controller {
function world_get(){
$data = new stdClass();
$data->name = 'Mark ';
$this->response($data, 200);
}
}
These files below I can get data from database but in html, how can I get data in json format?
/controllers/site.php
<?php
Class Site extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function index(){
$this->load->model('data_model');
$data['rows'] = $this->data_model->getAll();
$this->load->view('home', $data);
}
}
/models/data_model.php
<?php
class Data_model extends CI_Model {
function getAll(){
$q = $this->db->query("SELECT * from data");
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data [] = $row;
}
return $data;
}
}
}
views/home.php
<htmL>
<body>
<pre>
<?php foreach ($rows as $r)
{
echo '<h1>' . $r->title . '</h1>';
}
?>
</pre>
<?php foreach ($rows as $r) : ?>
<h1> <?php echo $r->author; ?></h1>
<h1> <?php echo $r->contents; ?></h1>
<?php endforeach; ?>
</body>
</htmL>
You are making API.To return data you don't need to call view.You can do it simply this way
function index()
{
$this->load->model('data_model');
$data = $this->data_model->getAll();
$this->response($data, 200);
}
Your model function is OK but you can make it simple.
function getAll()
{
return $this->db->get('data')->result();
}
just use json_encode in your controller and little change in your model file
Controller
function index(){
$this->load->model('data_model');
$result= $this->data_model->getAll();// change this line
$data['rows']= json_encode($result);// add this line
$this->load->view('home', $data);
}
Model
<?php
class Data_model extends CI_Model {
function getAll(){
$q = $this->db->query("SELECT * from data");
if($q->num_rows() > 0) {
return $q->result();
}else{
return false;
}
}
}

load menu on the header file using codeigniter

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) :)

Why does this model return "non-object error", when it worked in the controller?

In my controller I went from getting the data right there (which worked fine):
$data['query'] = $this->db->query("MY DB QUERY");
$this->load->view('shopci_view', $data);
to grabbing the data from a class model function:
class Shopci_model extends CI_Controller {
function __construct()
{
parent::__construct(); //model constructor
}
//display sale itmes on shopci_view.php
function sale_items()
{
$query = $this->db->query('MY DB QUERY - SAME AS ABOVE');
return $query->result();
}
}
new controller function:
//load model, auto connect to db
$this->load->model('Shopci_model', '', TRUE);
//Display items for sale
$data['query'] = $this->Shopci_model->sale_items();
$this->load->view('shopci_view', $data);
ERROR:
Fatal error: Call to a member function result() on a non-object in
shopci_view
This is the line in the view that worked before I switched to the model (didn't change view):
<?php foreach($query->result() as $row): ?>
Any help is appreciated.
Thank You.
In your model you return the $query as a result() array to the controller.
So you cannot then use 'foreach $query->result()' again - because you have already done that;
This should work
<?php foreach($query as $row): ?>
OR if you want - just change the model from
return $query->result();
to
return $query;
In your model you want to return the data to pass to the controller. So in your model you would have
function sale_items()
{
$query = $this->db->query(SAME AS OLD);
return $query;
}
and then your controller will be the same and your foreach in your view should be the following
<?php foreach ($query as $row): ?>

Categories