PHP Notice: Undefined index: - php

function drupal_build_css_cache($css) {
$data = '';
$uri = '';
$map = variable_get('drupal_css_cache_files', array());
// Create a new array so that only the file names are used to create the hash.
// This prevents new aggregates from being created unnecessarily.
$css_data = array();
foreach ($css as $css_file) {
$css_data[] = $css_file['data'];
}
PHP Notice: Undefined index: data on line 3606.
Line 3606 being: $css_data[] = $css_file['data'];

To avoid this error just test if the table fields was initialized with isset () .
// Before using $css_file['data'];
if (isset($css_file['data']))
{
$css_data[] = $css_file['data'];
}

It is just notice, that you have undefined index; you should first check if is it set:
if (isset($css_file['data'])) {
$css_data[] = $css_file['data'];
}
Otherwise you can turn off notice reporting (put this line at the beginning of your code):
error_reporting(E_ALL ^ E_NOTICE);

PHP notice is telling you that $css_file has no key data. Check what you receive in $css variable.

First, you should show what you've tried to fix the issue. As the error shows, it's caused because $css_file['data'] is undefined. You haven't shown us where $css_file is defined. $css_file['data'] is most likely not being defined.

Related

Accessing array data returned from Spotify API

I am using jwilsson's spotify-web-api-php to return data from the Spotify API using PHP.
Also I'm using digitalnature's php-ref to return info about variables.
This is my code, apologies for the fact it's probably full of errors, misconceptions, rubbish etc.
// https://stackoverflow.com/questions/4345554/convert-a-php-object-to-an-associative-array
function objectToArray($r)
{
if (is_object($r)) {
if (method_exists($r, 'toArray')) {
return $r->toArray(); // returns result directly
} else {
$r = get_object_vars($r);
}
}
if (is_array($r)) {
$r = array_map(__FUNCTION__, $r); // recursive function call
}
return $r;
}
$session = new SpotifyWebAPI\Session(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
);
$session->requestCredentialsToken();
$accessToken = $session->getAccessToken();
$api = new SpotifyWebAPI\SpotifyWebAPI();
$api->setAccessToken($accessToken);
$search = $api->search('fugazi','artist');
// convert object to array
$fff = objectToArray($search);
// php-ref to view info about the $fff array
r($fff);
// try to access the total value
$v1 = $fff[0]['artists']['total'];
This is what the php-ref looks like for the $fff variable:
I tried to access the total attribute via:
$v1 = $fff[0]['artists']['total'];
But receive these errors:
PHP Notice: Undefined offset: 0 in C:\websites\spotify\search.php on line 49
PHP Notice: Trying to access array offset on value of type null in C:\websites\spotify\search.php on line 49
PHP Notice: Trying to access array offset on value of type null in C:\websites\spotify\search.php on line 49
Sorry for asking a silly question and for not understanding what I'm doing, but, what am I doing wrong?

A PHP Error: Only variables should be passed by reference in Codeigniter

I'm stuck in this error. Can someone help me out? I am using Codeigniter 3.1.10
Here's a snippet of my code in system/core/Common.php
function &load_class($class, $directory = 'libraries', $param = NULL)
{
static $_classes = array();
// Does the class exist? If so, we're done...
if (isset($_classes[$class]))
{
return $_classes[$class];
}
$name = FALSE;
// Look for the class first in the local application/libraries folder
// then in the native system/libraries folder
foreach (array(APPPATH, BASEPATH) as $path)
{
if (file_exists($path.$directory.'/'.$class.'.php'))
{
$name = 'CI_'.$class;
if (class_exists($name, FALSE) === FALSE)
{
require_once($path.$directory.'/'.$class.'.php');
}
break;
}
}
I keep on receiving this error message:
Notice: Only variables should be passed by reference in
/customers/5/d/b/tsoft.se/httpd.www/kanban/system/codeigniter/Common.php
on line 148
A PHP Error was encountered Severity: Notice
Message: Only variables should be passed by reference
Filename: codeigniter/Common.php
Line Number: 148
**Note : Line 148 is this return $_classes[$class];
As stated in a comment on this github issue, since you are using CodeIgniter version 3.1.10, you need to replace your system folder.
The file system/codeigniter/Common.php has been moved to system/core/Common.php on your version.

Function undefined variable error without use of global

Recently have been reviewing how classes work in PHP. I have stumbled across this code and I am trying to understand why WAMP is giving me notices as opposed to my host.
$settings['ip_forwarded_check'] = 1;
function get_ip()
{
$ip = 0;
if(!preg_match("#^(10|172\.16|192\.168)\.#", $_SERVER['REMOTE_ADDR']))
{
$ip = $_SERVER['REMOTE_ADDR'];
}
if($settings['ip_forwarded_check'])
{
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
preg_match_all("#[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}#s", $_SERVER['HTTP_X_FORWARDED_FOR'], $addresses);
}
elseif(isset($_SERVER['HTTP_X_REAL_IP']))
{
preg_match_all("#[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}#s", $_SERVER['HTTP_X_REAL_IP'], $addresses);
}
if(is_array($addresses[0]))
{
foreach($addresses[0] as $key => $val)
{
if(!preg_match("#^(10|172\.16|192\.168)\.#", $val))
{
$ip = $val;
break;
}
}
}
}
if(!$ip)
{
if(isset($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
}
return $ip;
}
The errors I will get are:
Notice: Undefined variable: settings in
Notice: Undefined variable: addresses in
I can fix all the problems by adding a "global: $settings; global $addresses;" but I was wondering how to prevent this? I know many claim I should not use globals in functions and so that leads me to wonder the correct way to go about this. I would also like to know why one host will show me warnings/notices and the other does not? I do not have the notices turned off for my host.
Both your problems stem from the fact that you are using uninitialized variables. If you write
$foo = "abc";
in your code, then, if $foo was not existent before, it will be created and initialized. However, if you do something like this:
$settings['ip_forwarded_check'] = 1;
then you try to refer an item of an associated array, which was never created. Let's create it:
$settings = array();
$settings['ip_forwarded_check'] = 1;
You will also need to initialize $addresses, but I do not know about your task enough to give you any advice about the way you should initialize it.
Error reporting is probably switched off on your server, which should be the reason you do not see the error messages there.

Issue with loading user class

I have a small problem with the following php code. On the input for the name, I'm trying to show their current username, by $user->username, only it gives me the following error:
Notice: Undefined variable: user in /home//domains//public_html/dev/edit_account.php on line 36
Notice: Trying to get property of non-object in /home//domains//public_html/dev/edit_account.php on line 36
in the game_header.php file I have
$user = new User($_SESSION['id']);
And thought it would work, but sadly it does not.
I have also tried
$user = new User($_SESSION['id']);
on the edit_account.php page, but I was getting the same error.
Here's the edit_account.php code.
Does anyone know what I might be doing wrong here?
include "game_header.php";
$_GET['type'] = isset($_GET['type']) && ctype_alpha($_GET['type']) ? trim($_GET['type']) : '0';
switch($_GET['type']) {
case 'profileoptions' : profile_options(); break;
default : profile_options();
}
function profile_options() {
echo '
';
include 'game_footer.php';
}
When encased into a function you must do the following:
global $user ; //Bring a global variable to the current scope.
echo $user->username ; //Then you can access it and its properties.
So it must start with:
function profile_options() {
global $user ;
//The rest of code
}
However, I recommend you to create a parameter:
function profile_options(User $user){
//Much code
}
Then call it where $user is accessible:
profile_options($user) ;

Stuck into a Notice: Undefined variable: c

I have this index.php file
# Loading configurations.
loadConfiguration('database');
loadConfiguration('site');
# Initializing the database connection.
$db = new database($c['db']['host'], $c['db']['username'], $c['db']['password'], $c['db']['name']);
unset($c['db']['password']);
And loadConfiguration() is set as follow:
function loadConfiguration($string) { require(path.'config/'.$string.'.con.php'); }
I checked that database.con.php and site.con.php are into the config/ folder.
And the only error i get is a Notice: Undefined variable: c in the following line
$db = new database($c['db']['host'], $c['db']['username'], $c['db']['password'], $c['db']['name']);
Here's the database.con.php
# Ignore.
if (!defined('APP_ON')) { die('Feel the rain...'); }
$c['db']['host'] = 'localhost';
$c['db']['username'] = 'root';
$c['db']['password'] = '';
$c['db']['name'] = 'name';
Here's the site.con.php
# Ignore.
if (!defined('APP_ON')) { die('Feel the rain...'); }
/* Site informations */
$c['site']['name'] = 'Namek';
$c['site']['mail'] = 'mail#whocares.com';
$c['site']['enable-email'] = false;
$c['site']['debug-mode'] = true;
What am i doing wrong?
The required code is executed within the function, so $c is a local variable and not accessible in global scope.
You may either use some fancy design pattern to make $c global (e.g. use a Registry (R::$c, R::get('c'), an Environment ($this->env->c), ...) or you could change your configuration loader function to return the name of the file and require outside of it:
require_once configFile('database');
function configFile($name) {
return path . 'config/' . $name . '.con.php';
}
Please don't use global variable according security purpose. you can set your variable in registry variable and then access it.

Categories