view file application/views//view..php not found [duplicate] - php

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.');
}

Related

Laravel/PHP error: Undefined variable: email2 [duplicate]

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"

PHP CLASS Undefined variable (But it is defined) [duplicate]

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;

Undefined Variable in PHP? [duplicate]

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

How to resolve "Fatal error: Call to a member function query()" [duplicate]

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.
Just had the database.php file changed from from MySQL to PDO. But I'm getting this error:
Fatal error: Call to a member function query() on a non-object in /home/mypath/mydomain.com/library/database.php on line 19
It's working fine on the site of the individual who made the change. But when uploaded to my site it's generating this error, not sure why. I use DreamHost. What's wrong?
Line 19:
$result = $pdo->query($sql);
Code prior:
<?php
require_once 'config.php';
try {
$conn = new PDO("mysql:host=$servername;dbname=onlysecret_db", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
function dbQuery($sql)
{
global $pdo;
$result = $pdo->query($sql);
return $result;
}
It's because you're accessing $pdo, which is a variable that doesn't exist.
Change $pdo to $conn:
function dbQuery($sql)
{
global $conn;
$result = $conn->query($sql);
return $result;
}
...but I would advise against using global and instead pass the $conn as a parameter to the function.

Cannot spot the issue with PHP Index error-possible multiple threads issue [duplicate]

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.
I'm struggling to find the problem with the following piece of PHP code:
The code is as follows
class WorkerThreads extends Thread
{
private $from_list;
public function __construct($x,$host,$users_email,$pass,$inbox)
{
$this->from_list = array(); # holds the unique froms extracted from headers
}
public function run()
{
# Get Froms
if (preg_match('/From\:\ (.+)/i', $headers, $matches, PREG_OFFSET_CAPTURE)) {
$from = trim(str_ireplace("From: ", "", $matches[0][0]));
if (!array_key_exists($from, $this->from_list)) {
$this->from_list[$from] = 1;
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
}
} else {
echo "NO FROM <br/><rb/>";
}
The following error occurs:
Notice: Undefined index: Viva in /var/www/BAMCode/yahoofroms.php on line 198
FROM: Viva-
The offending line 200 is
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
There seems to be a problem with the array
This will work perfectly fine if you don't use multiple threads.
I think many threads are trying to modify and read the same list. Try synchronizing the modification/read of the from_list. We may use a mutex to synchronize read/write to the from_list.
Check the code below:
<?php
class WorkerThreads extends Thread
{
private $from_list;
private $mylock;
public function __construct($x,$host,$users_email,$pass,$inbox)
{
$this->from_list = array(); # holds the unique froms extracted from headers
$this->mylock = Mutex::create();
}
public function __destruct()
{
Mutex::destroy($this->mylock);
$this->mylock = null;
}
public function run()
{
# Get Froms
if (preg_match('/From\:\ (.+)/i', $headers, $matches, PREG_OFFSET_CAPTURE)) {
$from = trim(str_ireplace("From: ", "", $matches[0][0]));
Mutex::lock($this->mylock);
if (!array_key_exists($from, $this->from_list)) {
$this->from_list[$from] = 1;
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
}
Mutex::unlock($this->mylock);
} else {
echo "NO FROM <br/><rb/>";
}
}
}
?>

Categories