How do I fix a undefined index error in Drupal 7? - php

Problem
I am getting a "Undefined index: node in include()" notice in Drupal for the below line of code. I've tried the below solution but I am still receiving the error. Any ideas?
Code
$url = drupal_lookup_path('alias', 'node/' . $related['node']->nid);
The solution I tried
isset($related['node']->nid) ? $related['node']->nid : "";
Question
Does anyone know why this error continues to occur?

The error suggests the $related array variable doesn't have a node index.
First, make sure you're retrieving the node object correctly.
Then, perhaps try it like this to avoid errors:
<?php
$url = '';
if (isset($related['node']) && is_object($related['node'])) {
$nid = $related['node']->nid;
$url = drupal_lookup_path('alias', "node/$nid");
}

Related

Warning on "$_SERVER['REQUEST_URI'] = MSU_REQUEST_URI;"

the line $_SERVER['REQUEST_URI'] = MSU_REQUEST_URI;
Fills my errorslog with
Use of undefined constant MSU_PHPEX_PS - assumed 'MSU_PHPEX_PS' (this
will throw an Error in a future version of PHP)
So I was thinking to solved it with $_SERVER['REQUEST_URI'] = 'MSU_REQUEST_URI';
Than is the warning gone but the script isn't working any longer.
Any ideas?
The Problem you're having is that MSU_REQUEST_URI is undefined
This means whatever value you expected the constant MSU_REQUEST_URI to have, has not been set at the time of you executing $_SERVER['REQUEST_URI'] = MSU_REQUEST_URI;,
By surrounding MSU_REQUEST_URI with quotes like this 'MSU_REQUEST_URI' you are assigning the String Value (literally) "MSU_REQUEST_URI" to $_SERVER['REQUEST_URI'].
So as #alithedeveloper has asked in the comments:
What is MSU_REQUEST_URI supposed to be and how/where is it supposed to get it's value from?
You won't be able to solve your issue without figuring out why MSU_REQUEST_URI is not set.
Thanks for the response I found a other page with code about the MSU_REQUEST_URI
<?php
if(defined('MSU_REQUEST_URI')) {
return;
}
if(version_compare(PHPBB_VERSION, '3.1', '>=')) {
global $phpEx;
// Fixing Symphony rewrite compatibility
$_SERVER['PATH_INFO'] = '';
$_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
define('MSU_REQUEST_URI', $_SERVER['REQUEST_URI']);
if(
in_array(
basename($_SERVER['SCRIPT_NAME']),
array(
'viewtopic.'.$phpEx,
'viewforum.'.$phpEx,
'search.'.$phpEx,
'memberlist.'.$phpEx,
'faq.'.$phpEx,
'viewonline.'.$phpEx,
'ucp.'.$phpEx
)
)
) {
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'].(!empty($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : '');
}
if(!defined('PHPBB_USE_BOARD_URL_PATH')) {
define('PHPBB_USE_BOARD_URL_PATH', true);
}
}
?>
Is that helping

PHP Array breaking

This works on my test environment, but on my live server there is a later version of PHP which is throwing up an error and breaking my program
The code is
$oldFile = fopen("D:/ftpfolderreport/report/" . $last_file, "r");
while(!feof($oldFile))
{
$buffler = fgets($oldFile);
$bufflerArray = explode(",", $buffler);
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}
fclose($oldFile);
This line:
$oldFileArray[$key] = $bufflerArray[1];
Is throwing out this error
Notice: Undefined offset: 1 in D:\apps\wamp\www\Compliance2\compareFtpReports.php on line 57
I think this is to do with how I'm adding the $key variable inside the argument. I've tried it as ["$key"] and ['$key'] but it doesn't like it.
I have tried defining the key variable earlier in the program but still doesn't like it. I've been searching around online but can't find anything of help. Anyone any ideas?
Thanks,
Stephen.
add checks for empty
if (!empty($bufflerArray[1])) {
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}

YQL Query Returning Errors

So I have been working on a school project and have gotten this code for a website to work sometimes but other times it returns the error:
Notice: Trying to get property of non-object in C:\xampp\htdocs\schoolproj\getdata.php on line 27
Notice: Trying to get property of non-object in C:\xampp\htdocs\schoolproj\getdata.php on line 27
Ask
Notice: Trying to get property of non-object in C:\xampp\htdocs\schoolproj\getdata.php on line 39
Notice: Trying to get property of non-object in C:\xampp\htdocs\schoolproj\getdata.php on line 39
for the php code:
<html>
<body>
<?php echo $_POST['name']; ?>!<br>
<?php
$endpoint = "http://query.yahooapis.com/v1/public/yql";
$ticker = "'".$_POST["ticker"]."'";
$query = urlencode("env 'store://datatables.org/alltableswithkeys';select * from yahoo.finance.quotes where symbol in (".$ticker.")");
$ch = curl_init($endpoint.'?q='.$query. '&format=json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (curl_error($ch)){
die(curl_error($ch));
}
curl_close($ch);
//echo'<pre>';
$result = json_decode($result);
$symbol = $result->query->results->quote->symbol;
print_r($symbol);
?>
Ask
<?php
$Ask = $result->query->results->quote->Ask;
print_r($Ask);
?>
</body>
</html>
I was wondering if anyone had some advice as to how I could permanently fix the problem or have some sort of error handling. I am new to this so any help would be great. Thanks!
The problematic lines if the code you gave is the whole getdata.php file are:
l.27: $symbol = $result->query->results->quote->symbol;
l.39: $Ask = $result->query->results->quote->Ask;
And the error is telling you that at one point in this your are accessing a property of something that is not an object.
Considering you are saying it sometimes work and sometimes doesn't, it's likely that there are occasional errors, either in your query (depending on your input) or with datatables.org (see this old question on developer.yahoo which indicates that queries would fail when datatables.org doesn't respond).
Then if there is an error, the json you receive will have a structure like below (this is an error I got initially when trying your code because I had forgottent to enclose the ticker in quotes).
{
"error": {
"lang":"en-US",
"description":"Query syntax error(s) [line 1:95 mismatched input 'in' expecting ISNOTNULL]"
}
}
In this result you don't have the query attribute and thus is fails when you try to access it. You should then first check if there is an error (looking for the error attribute), and only if there is none try and access the query results.
To check for the error, you could use something like
if (property_exists($result, "error")) {
// your error handling
} else {
// your current code accessing the results
}

Warning: Invalid argument supplied for foreach() in /home/content/03/11247403/html/wp-content/themes/kerdowney-2810/t_destinations.php on line 183

i'm working on a WordPress site, i keep on getting this warning. i thought it has something to do with my magic fields maybe i named something wrong but its still giving me the same error.
How do i solve this problem.
I have this piece of code on t_destinations.php
$ras = getGroupOrder('recommended_accommodation_title' , $ras);
foreach ($ras as $ra) :
$ras_title = get('recommended_accommodation_title', $ras);
$ra_link = get('recommended_accommodation_link', $ra);
$link = !empty($ra_link) ? $ra_link : '#';

Can't use function return value in write context, PHP can't figure out why

This is my code :
PHP:
if(isset($_COOKIE("cookie_roof_angle")) && isset($_COOKIE("cookie_roof_direction")))
{
$roof_angle = intval($_COOKIE("cookie_roof_angle"));
$roof_direction = $_COOKIE("cookie_roof_direction");
$solarsell_page05_rendement = mysql_query("SELECT value FROM solarsell_page05_pvgis WHERE angle = $roof_angle AND azimut = " . $roof_direction. " ");
echo $solarsell_page05_rendement;
}
else
{
echo "no values";
}
I'm getting this error message :
Fatal error: Can't use function return value in write context in C:\xampp\htdocs\Development\phpFunctions.php on line 19
After some searching around on the web & stackoverflow.com, I found out it may be caused by the isset function, could anyone please explain if this is the problem and why?
If this is not the problem, maybe I did something wrong in my code part, but I can't figure out why.
The Cookies where both set when I got the error.
Sincerly,
Harmen Brinkman.
Syntax error:
$_COOKIE["cookie_roof_angle"] instead of $_COOKIE("cookie_roof_angle")

Categories