Notice: Trying to get property of non-object debug error - php

When the 404 page is triggered, I get a
Notice: Trying to get property of non-object
on the following lines of code when in debug mode
$custom = get_post_custom($post->ID);
and
$parent_id = $parent_id_2 = $post->post_parent;
I know this is suppose to happen because the 404 page triggers because the are no post to show in the first place. To get rid of these notices I need to do a check first to see if there is a post to show. My problem is, exactly how do I do it. Any suggestions?

I would tend to do something like
if (isset($post)){
//action goes here
}
so the action is only performed if post has been set.

Related

Trying to fix a "Trying to get property of non-object in * on line *" error

Im aware this code might be vulnerable to SQL injection, it doesn't matter, this is a private home project that will never go public, the only person that can access this other than me is my dad and brother.
Im making a site where you press certain buttons, it sends you to a shop, but the shop doesn't load because of this error:
<b>Notice</b>: Trying to get property of non-object in <b>shop.php</b> on line <b>12</b>
What could be wrong? I have tried looking for solutions on the internet but I found none.
This is the lines of code that this is affecting:
if (!isset($HTTP_RAW_POST_DATA)) {
$HTTP_RAW_POST_DATA = file_get_contents("php://input");
$doc = new DOMDocument();
$doc->loadXML($HTTP_RAW_POST_DATA);
$intShopId = $doc->getElementsByTagName('intShopId');
$shop_id = $intShopId->item(0)->nodeValue;
The last line in this case is line 12.
I know the reason of this is because $intShopId is not an object. I think it needs to be a global variable passed as a parameter, but in this individual case I cant figure out whats wrong and i would appreciate some help.
There might be some grammatical and/or spelling errors in the question here, I apologize if this is the case. I'm from Sweden.
The problem is going to be with this line:
$intShopId = $doc->getElementsByTagName('intShopId');
since there is no such tag as an <int> - or even any tag that even begins with that, then that method is returning null, or something else that isn't helpful.
you are probably looking to use a method like :
$doc->getElementById('intShopId');

Error: "Call to member function on a non-object..." in Magento

When I place the following code at the bottom of my new_grid.phtml template file, it works fine, without any any errors. But when it is placed at the top of the file, I get a fatal error: "Call to member function getRelatedLinkCollection() on a non-object in C:\wamp\www\MYSITE\magento\app\design\frontend\MYSITE\default\template\catalog\product\widget\new\content\new_grid.phtml on line 32.
<?php
foreach ($_product->getRelatedLinkCollection() as $link) {
$dats= $link->getLinkedProductId();
}
?>
I would like to get this code working specifically at the top of the file, because I'd like to output generated HTML in a particular structure. Any idea why I'm getting this error and what I can do to correct it?
Thank you!
Can you post the contents of your new_grid.phtml file?
Otherwise, make sure the foreach loop is immediately after the line which contains $_product =

uri_to_assoc(n) doesn't work after routing

So this currently loads the page for me.
/controllername/view/id/27/title/shoes
However, I want the user to be able to type in:
/controllername/27/shoes
to view the file. So in the routing file, I made a rule like this,
$route['controllername/(:num)/(:any)'] = "controllername/view/id/$1/title/$2";
The original address works with uri_to_assoc(n) because I have it as
$array = $this->uri->uri_to_assoc(3);
$id = $array['id'];
$title = $array['title'];
But once I route it and use the new address to access the file, I get the errors:
Message: Undefined index: id
Message: Undefined index: title
I do not get these errors with my original url way of accessing it. I guess the value of uri_to_assoc(3) changes once the url is changed but I thought the routing function would take care of that. Can anyone help me get rid of these variable errors?
In this case you'll want ruri_to_assoc:
http://codeigniter.com/user_guide/libraries/uri.html
$this->uri->ruri_to_assoc(n)
This function is identical to [uri_to_assoc], except that it creates
an associative array using the re-routed URI in the event you are
using CodeIgniter's URI Routing feature.
You should still validate your array indices anyways, in case the URL itself is invalid.

PHP variable passing

I'm working with drupal. I have this page something.com/node/13. Inside I have this script
<?php
$door = $_GET["variable"];
echo $door;
?>
When I open the URL like this something.com/node/13?variable=1231 I get the following error:
Error message
Notice: Undefined index: variable in eval() (line 2 of /var/www/html/modules/php/php.module(74) : eval()'d code).
and no output. Any ideas what I might be doing wrong?
The error, partcilarly in drupal 7 simply means that you aren't getting any value for $_GET['variable']. This would be because the url doesn't have a value (someurl?variable=foo). You can avoid the notice by prefixing an # like this:
$door = #$_GET['variable'];
but that won't make the variable return any data. It's pretty unclear from the code you've posted what you're trying to accomplish. You might consider starting there. There is no reason why you can't use $_GET variables in drupal. I do so all the time.
Use drupal_get_query_parameters
Because the rewrite rules with Drupal, you don't grab the $_GET values cleanly directly like that.

receiving error undefined index on two variables

Using adLDAP.php class
receiving following error: Notice: Undefined index: memberof in /web/ee_web/include/adLDAP.php on line 762
line 762: if (is_array($groups[0]["memberof"])) {
Also receiving error: Notice: Undefined index: count in /web/ee_web/include/adLDAP.php on line 982
line 982: $entries[0]["memberof"]["count"]++;
Unsure of what I need to do to resolve these error messages, it seems that the script is working fine, but I'd like get rid of these errors.
Using: http://adldap.sourceforge.net/wiki/doku.php?id=api
You could edit the code to something along the lines of:
if(isset($groups[0]["memberof"]))
{
if (is_array($groups[0]["memberof"])){ ... }
}
And
if(isset($entries[0]["memberof"]["count"]))
{
$entries[0]["memberof"]["count"]++;
}
It will keep you from getting the errors, though it won't necessarily handle some pretty lax sanity checking by the original author of the code.
It seems like you have your error_level set to show every possible error/warning/notice, that's why you're getting it.
If the script is working fine, then it's not an error, simply a missing check the coder forgot to put in the library.
To get rid of thos messages, you have 2 options:
a) Use # before the calls you do to that library, such as
$var = #the_function(param1);
This will avoid those messages for just that line.
b) Set the error level to something like this with error_reporting():
error_reporting(E_ALL ^ E_NOTICE);
This will affect the whole script you're running.
It's up to you what to use depending on the project.

Categories