Undefined variable : variable from controller to view - codeigniter

Hi i dont know why its not any more working.
i got undefinied variable if i try to echo the variable in the view.
Here the controller
class save_settings extends CI_Controller {
function save()
{
$data['test'] = 'content';
$this->load->view('help', $data);
}
}
and view
<!DOCTYPE HTML>
<html lang="de">
<head></head>
<body>
<?php echo $test ; ?>
</body>
</html>

Some issue to check -
1) Correct view name and path
2) Correct URL - /save_settings/save
3) Try to print and exit something to check whether the controller is loading perfectly.
function save()
{
$data['test'] = 'content';
echo '<pre>'; print_r($data); exit; // <== Debug
$this->load->view('help', $data);
}
// Should give
Array(
[test] => content
)

Related

Trying to get property of non-object - Undefined variable: data/ Codeigniter

i'm try to get news_id from database but when go to view say : error -> Trying to get property of non-object / Message: Undefined variable: data
this model - >
class Model1 extends CI_Model {
public function get_art()
{
$query = $this->db->get('entries');
return $query->result();
}
}
here controller Code - >
class Home extends CI_Controller
{
public function members()
{
$this->load->model('model1');
$data=$this->model1->get_art();
$this->load->view('members', $data);
}
}
and this Full View - >
<html>
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<h1>
<? echo $data->body; ?>
</h1>
</body>
</html>
This is because of you invalid pass data to view. At controller replace line
$data=$this->model1->get_art();
with
$data["query"] = $this->model1->get_art();
Then at view you will have var $query with results of your database query.
You can use it like this:
<h1>
<? foreach($query as $row) {
echo $row->body;
}
?>
</h1>

Codeigniter links for search results

I'm a new to CodeIgniter and I have a problem when I display a list of items, I want each items to be a link, so when the user clicks that particular item, it will show the details of that item. Or perhaps give some suggestion on what I should google for. I have literally no idea whats terms or keywords that should I search about.
controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller
{
public function index()
{
$this->getListOfAgent();
}
function getListOfAgent()
{
$this->load->model("agentDB_model");
$data['results'] = $this->agentDB_model->getAllAgentInfo();
$this->load->view("viewAgent_view", $data);
}
function userInformation()
{
//how to i get the selected item which the user clicked and show the details accordingly?
}
}
model
<?php
class agentDB_model extends CI_Model
{
function getAllAgentInfo()
{
$query = $this->db->query("SELECT * FROM user");
return $query->result();
}
function addAgent($newAgent)
{
$this->db->insert("user", $newAgent);
}
}
view
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<div id="content">
<h1>Home Page</h1>
<p>List Of Agent in database</p>
</div>
<?php
foreach($results as $row)
{
echo "<a href = 'Site/userInformation'>$row->userName</a>";
echo "</br>";
echo "</br>";
}
//$this->load->controller(Site/userInformation);
?>
<div id="footer">
<p>Copyright (c) 2012 basicsite.com</p>
</div>
</body>
</html>
You need to tell the Site/userInformation method for what record you want the information. You use URI segments for this.
So, in your view, change the following line:
echo "<a href = 'Site/userInformation'>$row->userName</a>";
to:
echo "<a href = 'Site/userInformation/$row->userID'>$row->userName</a>";
Then, in your controller method, add the parameter to the method declaration:
function userInformation($userID)
{
// now, you use the model to get the correct record from the db based on
// the $userID and display the information
}

Echo data with eloquent

I am trying to get data out of my table, and send that array of output over to my view and there echo out specific parts of it.
I get an exception error: Undefined index bassengId.
index.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bassengweb</title>
</head>
<body>
<?php
if(isset($htt))
{
echo $htt['malingsId'];
}
?>
</body>
</html>
routes.php
Route::get('/', 'HomeController#showIndex');
Route::post('/data', 'HomeController#showInput');
homecontroller.php
public function showIndex()
{
return View::make('index');
}
public function showInput()
{
$htt = hvertredjetime::all();
return View::make('index')->with('htt', $htt);
}
If I try to just echo the $htt variable from index, I get:
[{"malingsId":1,"dato":"25.02.2014","tid":"12:44:00","frittKlor":"4.00","bundetKlor":"5.00","totalKlor":"9.00","ph":"7.00","autoPh":"8.00","autoKlor":"9.00","redox":"5.00","bassengId":1}]
I am a little stuck here, being new to this and not really seeing what I do wrong.
By using the ::all() method you are returning an array
public function showInput()
{
$htt = hvertredjetime::all();
return View::make('index')->with('htt', $htt);
}
In the View
#if(! is_null($htt))
#foreach($htt as $item)
<p>{{ $item->bassengId }}</p>
#endforeach
#endif
If you want to access data like this, you should do:
return View::make('index')->with(array('htt' => $htt));
Instead, return it like this:
return View::make('index')->with($htt);
And then, in view, access it directly:
{{ bassengId }}
You should access to the variable like this :
if(isset($htt))
{
echo $htt->bassengId;
}
or if you're using blade template :
#if(isset($htt))
{{ $htt->bassengId }}
#endif

jquery-mobile + codeigniter

What must be the problem with this code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $title; ?></title>
<?php
echo link_tag('assets/css/jquery.mobile-1.3.2.min.css', 'stylesheet');
echo script_tag('assets/js/jquery-1.10.2.min.js');
echo script_tag('assets/js/jquery.mobile-1.3.2.min.js');
?>
</head>
<body>
<div data-role="page">
<header data-role="header">
Show
<h3><?php echo $title; ?></h3>
<div data-role="controlgroup" data-type="horizontal" class="ui-btn-right">
My Account
Logout
</div>
</header>
I'am using jquery mobile for my client-side script and PHP(codeigniter) for server-side script.
When I refresh the page after including the in anchor the page now doesn't display the page anymore.
Can anyone tell what's wrong with the code or I'am just missing something.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Event_management_c extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('html', 'url', 'form');
}
public function index() {
$data['title'] = 'Events';
$data['reply_title'] = 'Reply Message';
$this->load->view('fragments/header', $data);
$this->load->view('fragments/nav', $data);
$this->load->view('events/index', $data);
$this->load->view('fragments/footer', $data);
}
}
?>
Problem here
Logout
to
Logout
you can use template library for codeigniter like TEMPLATE LIBRARY
and reload the script and style files.
Based on your extra answers from the comments; the problem is most likely that the function site_url() isn't defined. The result of this is a fatal error, which isn't shown to you due to settings in php concerning error_report.
To solve this, run $this->load->helper('url') in relevant controller, or simply add url to the helper array in application/config/autoload.php. Since this function is so common, I recommend to autoload it.

Laravel without Blade - Controllers and Views

I am more efficient at setting up my view logic with straight up php. Blade is cool but it's not for me. I am trying to translate all the Blade specific examples and docs to just php. I don't like the fact that I need to assign all the variables for my views in an array of View::make(). I did found all of this so far.
controllers/home.php:
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public function action_index()
{
$this->layout->name = 'James';
$this->layout->nest('content', 'home.index');
}
}
views/layouts/default.php:
// head code
<?php echo Section::yield('content') ?>
// footer code
views/home/index.php
<?php Section::start('content'); ?>
<?php echo $name ?>
<?php Section::stop(); ?>
I am greeted with this error: Error rendering view: [home.index] Undefined variable: name. I know that $this->layout->nest('content', 'home.index', array('name' => 'James')); works but that negates my point about having to send all my variables to an array. This can't be the only way.
The view templating docs doesn't seem to touch on doing variables with nested views from controllers.
you can pass variables this way;
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public function action_index()
{
$this->layout->nest('content', 'home.index')
->with('name', 'James');
}
}
Here's an example of how I'm templating with laravel.
Class Products_Controller extends Whatever_Controller {
public $layout = 'layouts.main';
public function get_index()
{
// .. snip ..
$view = View::make('home.product')
->with('product', $product); // passing all of my variable to the view
$this->layout->page_title = $cat_title . $product->title;
$this->layout->meta_desc = $product->description;
$this->layout->content = $view->render(); // notice the render()
}
}
my main layout looks like
<html>
<head>
<title> {{ $page_title }} </title>
<meta name="description" content="{{ $meta_desc }}" />
</head>
<body>
{{ $content }}
</body>
</html>
and the home/product page looks like
<div class="whatev">
<h1> {{ $product->title }} </h1>
<p> {{ $product->description }} </p>
</div>
Hope that helps you clear some things up
I know it has been a while on this question, but since it was asked, Laravel 4 has come out and there are newer ways to do things.
If you are reading this these days you should consider using View Composers to prepare the data for your views.
Example:
class MyViewComposer {
public function compose($view){
$view->title = 'this is my title';
$view->name = 'joe';
...
$view->propertyX = ...;
}
}
After setting up your view composer register it with the app:
View::composer('home.index', 'MyViewComposer');
For more information check out the laravel docs on view composers:
http://laravel.com/docs/responses

Resources