This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
I am having an error Message: Undefined variable: posts. This is what I received after I sent an email using Codeigniter email.
I need to send an html output through CI email.
Controller:
function email() {
$this->load->library('email');
$config = array (
'mailtype' => 'html',
'wordwrap'=> TRUE,
'charset' => 'utf-8',
'priority' => '1'
);
$this->email->initialize($config);
$this->email->from('mywebsite.com', 'My website');
$this->email->to('sender#gmail.com');
$this->email->subject('Test');
$data = $this->data['posts'] = $this->paypal->getRows();
$message = $this->load->view('Receipt', '$data', true);
$this->email->message($message);
$this->email->send();
}
View Receipt.php (this is the view I want to send in an email)
foreach ($posts as $row) {
$row->room_type;
}
See how to pass data from controller to view
$data['posts'] = $this->paypal->getRows();
$message = $this->load->view('Receipt', $data, true);
No need '' in view when you assign array to view.
Related
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
i was trying to send an email and when i want to whrite the email address of the destination (the email is a parameter of the function), i have this error. Can someone help me.
this is my function:
public function email($name, $email2) {
$data = array('name'=> $name);
Mail::send('mail',$data,function($message) {
$message->to($email2, $name)->subject('Deposit Confirmation');
$message->attach(public_path('document.pdf'));
$message->from('xxx#xxx.pt', 'XXX');
});
}
this is my error: "Undefined variable email2"
thank you
When you use anonymous function to pass variables you should use use construction
public function email($name, $email2) {
$data = array('name'=> $name);
Mail::send('mail',$data,function($message) use ($name, $email2) {
$message->to($email2, $name)->subject('Deposit Confirmation');
$message->attach(public_path('document.pdf'));
$message->from('xxx#xxx.pt', 'XXX');
});
}
Here you can find more examples about anonymous functions https://www.php.net/manual/en/functions.anonymous.php
Here simple example from official documentation
$message = "hello";
// Inherit $message
$example = function () use ($message) {
var_dump($message);
};
$example();
// Output: string(5) "hello"
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
I have made an admin page and user page, and i want to show the list of users who have registered in database when the admin logs in.
For that i've created a model as follows,
public function get_last_ten_entries()
{
$query = $this->db->query("SELECT username FROM public");
return $query->result();
}
and this i am accessing through a view i have created, to which, when admin logs in, he is redirected, as follows,
<h1><?php echo $data;?></h1>
through controller,
$this->load->model('loginmodel');
$login_id = $this->loginmodel->login_valid($username, $password);
if($login_id){
$this->load->library('session');
$this->session->set_userdata('user_id','$login_id');
$data['query'] = $this->loginmodel->get_last_ten_entries();
$this->load->view('admin/account', $data);
}
but when i execute this i get,
A PHP Error was encountered Severity: Notice
Message: Undefined variable: data
Filename: admin/account.php
Line Number: 11
Just use $query instead of $data in the view
MODEL
public function get_last_ten_entries()
{
$query = $this->db->query("SELECT username FROM public");
return $query->result();
}
CONTROLLER
$this->load->model('loginmodel');
$data['query'] = $this->loginmodel->get_last_ten_entries();
$login_id = $this->loginmodel->login_valid($username, $password);
if($login_id){
$this->load->library('session');
$this->session->set_userdata('user_id','$login_id');
$data['query'] = $this->loginmodel->get_last_ten_entries();
$this->load->view('admin/account', $data);
}
VIEW
<?php
foreach($query as $row){
echo $row['name'];
}
?>
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I just can't figure out why this error is even showing up:
Notice: Undefined variable: authTime in /.../classname.class.php on line 33
class ClassName {
private $authTime = null;
const API_URL = '...';
const CLIENT_ID = '...';
const CLIENT_SECRET = '...';
private static $TOKEN = NULL;
public function __construct() {
$this->_authTime = $authTime; // <----- Line 33
if(!self::$TOKEN OR $this->AuthTime('GET') > 3600 OR !$this->_authTime) {
self::authorise();
}
}
public function getAuthTime() {
return $this->_authTime; // Returns NULL
}
I see $authTime is not defined within the constructor. I think you want to do:
$this->_authTime = $this->authTime;
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
This is my function:
public function showDataall($result)
{
$q = $this->conn->prepare($result) or die("failed!");
$q->execute();
while ($r = $q->fetch(PDO::FETCH_ASSOC))
{
$data[] = $r;
}
return $data;
}
This function perfectly work in old xampp but new xampp return a Notice:
Undefined variable: data in /opt/lampp/htdocs/live/demo/model/config.php on line 152
Declare variable before using it :
If your query returns no data, your current code will never actually create the $data array, and therefore when you try and return it, this error will happen.
public function showDataall($result)
{
$q = $this->conn->prepare($result) or die("failed!");
$q->execute();
$data = array();
while ($r = $q->fetch(PDO::FETCH_ASSOC))
{
$data[] = $r;
}
return $data;
}
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
public static function view($name, array $vars = null){
if(preg_match('/\\\\/', $name)){
$view_data = explode('\\', $name);
if(count($view_data) == 3)
$file = APP_PATH.DS.'views'.DS.$view_data[0].DS.$view_data[1].DS.'view.'.$view_data[2].'.php';
else
$file = APP_PATH.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php';
}
else{
$file = APP_PATH.DS.'views'.DS.'view.'.$name.'.php';
}
if(!is_readable($file)){
throw new Exception('view file application'.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php not found.');
}
else{
if(isset($vars)){
extract($vars);
}
require($file);
}
}
[21-Apr-2015 13:10:30 UTC] PHP Notice: Undefined variable: view_data in /home/realitycards/public_html/test/system/load.class.php on line 28
your variable $view_data only gets defined in the first if statement. It looks like in the if statement below that you are using $view_data even though it hasn't been set.
if(!is_readable($file)){
throw new Exception('view file application'.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php not found.');
}
You either need to set $view_data in your else statement, or in the exception above, use the $file variable that you've already set:
if(!is_readable($file)){
throw new Exception('view file '. $file .' not found.');
}