Notice for object conversion - php

I'm doing a search engine for eBay-API, though I can't figure out what I'm doing wrong here, I get the error of:
Notice: Trying to get property of non-object in D:\xamp\htdocs\webapp\queries.php on line 431
Line 431 is:
if ($darkarteli->Item->SellingStatus->CurrentPrice <= 10 && $darkarteli->Item->SellingStatus->CurrentPrice >= 25 && $darkarteli->ListingType == 'FixedPriceItem')
Full code snippet:
foreach ($temparrrayforproducts as $darkartelis){
global $itemidsfromgetsellerlist;
$nd=[];
array_push($nd, $darkartelis->ItemArray);
foreach($nd as $darkartelis->Item){
if ($darkarteli->Item->SellingStatus->CurrentPrice <= 10 && $darkarteli->Item->SellingStatus->CurrentPrice >= 25 && $darkarteli->ListingType == 'FixedPriceItem'){
array_push($itemidsfromgetsellerlist, $darkarteli->Item->ItemID);
}
}
}
var_dump ($itemidsfromgetsellerlist);
Beautified var_dump() for $darkartelis: https://pastebin.com/xi34Vzn7
EDIT: I tried this code and it doesn't give out any errors, but it doesn't pass through the if statement even though there are 10-25 priced items which are fixedpriceitems also
foreach($temparrrayforproducts as $ndd){//pro kiekviena getsellerlist
global $itemidsfromgetsellerlist;
$nd=[];
array_push($nd, $ndd->ItemArray->Item); // tada per get seller list item arr
foreach($nd as $k=>$nds){ // per kiekviena itema
if (is_object($nds)&&!empty($nds)){
if ($nds->SellingStatus->CurrentPrice <= 10.00 && $nds->SellingStatus->CurrentPrice >= 25.00 && $nds->ListingType == 'FixedPriceItem'){
echo "Hallelujah";
array_push($itemidsfromgetsellerlist, $nds->ItemID);
}
}
}
}

Related

Prestashop: notice eval()'d code [8] Array to string conversion

I've been searching through this and other sites and can't solve the notice I got every time I open the back office product page.
Notice on line 279 in file /home/librer16/public_html/tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code
[8] Array to string conversion
Piece of code:
...
else {
//var_dump('renderTemplate', $_template->has_nocache_code, $_template->template_resource, $_template->properties['nocache_hash'], $_template->parent->properties['nocache_hash'], $_output);
//Line 279
if (!empty($_template->properties['nocache_hash']) && !empty($_template->parent->properties['nocache_hash'])) {
// replace nocache_hash
$_output = str_replace("{$_template->properties['nocache_hash']}", $_template->parent->properties['nocache_hash'], $_output);
$_template->parent->has_nocache_code = $_template->parent->has_nocache_code || $_template->has_nocache_code;
}
}
...
The website works, but this notice is very annoying.
Any suggestion to remove the notice?
Try to replace the line with this one:
if (is_array($_template->properties) && !empty($_template->properties['nocache_hash']) && is_array($_template->parent->properties) && !empty($_template->parent->properties['nocache_hash']))
Anyway, if you deactivated Debug Mode warningins won't show anymore.
Good luck.

IF ELSE in OOP PHP

I am writing an if else statement in OOP;
The conditions are:
1. Get all requests from BSC from 789789 or 987897
2. If RNB is starting 17 and are 15 characters long
3. Those values with cost as 15 send to URL 1
4. Else send to URL 2
If it matches the statement it is sent to a specific URL, if it does not it sent to another URL. I have rewritten the conditional statement below:
if($request->BSC = 789789 or 987897 && $request->RNB = 17 && $request->BRN strlen = 15 && $request->Cost = 11){
send to URL 1
} else{
..Send to URL 2
}
My question is conditional statement correct?
Your question is a little unclear,
I try to fix your if statement just to show you the correct syntax!:
if( ($request->BSC == 789789 || $request->BSC == 987897) && strpos($request->RNB,'7') == 0 && $request->BRN == 15&& $request->Cost == 11){
//send to URL 1
} else{
//send to URL 2
}
This line :
strpos($request->RNB,'7') == 0
Means: RNB should start with 7, for example : 765433 or 789998 or 712342 . ..

Undefinet offset error

My code sometimes give code undefinet offset error.
Error:
[03-Sep-2015 13:06:44] NOTICE: "Undefined offset: 6"
File: /home/mdmeds/public_html/includes/pages/game/class.ShowBuildingsPage.php | Line: 111
and
[04-Sep-2015 17:38:57] NOTICE: "Undefined offset: 8"
File: /home/mdmeds/public_html/includes/pages/game/class.ShowBuildingsPage.php | Line: 111
This is the part of the code
$Element = $CurrentQueue[$QueueID - 2][0]; /**this give the error*/
$BuildEndTime = $CurrentQueue[$QueueID - 2][3];
unset($CurrentQueue[$QueueID - 1]);
$NewQueueArray = array();
foreach($CurrentQueue as $ID => $ListIDArray)
{
if ($ID < $QueueID - 1) {
$NewQueueArray[] = $ListIDArray;
} else {
if($Element == $ListIDArray[0] || empty($ListIDArray[0]))
continue;
$BuildEndTime += BuildFunctions::getBuildingTime($USER, $PLANET, $ListIDArray[0]);
$ListIDArray[3] = $BuildEndTime;
$NewQueueArray[] = $ListIDArray;
}
}
I read lot of articles about this kind of errors but i do not know how to fix my code. Can someone help me please ?
You are trying to play with indexes that you are not sure they even exist...
things like
$CurrentQueue[$QueueID - 2]
is a guess... Get to find another way..
In this piece of code $CurrentQueue[$QueueID - 2][0], the key is generated dynamically based on $QueueID. WHen you get the error, it means that the specified key is not available in the array $CurrentQueue.
To avoid such run time exceptions, you can do something like this
if (!empty($CurrentQueue[$QueueID - 2])) {
// the actual functionality goes here.
}

WP-Polls set IP limit at 2

I'm using wp-polls and need to limit the number of votes per IP. Although this is usually set at 1 by default, I need to limit the number of votes per IP to 2.
I've been reading documentation, playing with the plugin code, and looking around on google and SO and can't seem to find the proper method.
I've considered using cookies, but these are harder to catch since I could just as well empty my cookies and voilà, it's done.
I need a limit because I don't want people to vote endlessly
Looking at the database, I imagine this has something to do with the pollip_ip field (from the (prefix)_pollsip) table, but not being familiar with editing WP Plugins, this is as far I as I got to go.
As a reference, here is some failed code
wp-polls.php, line 1323
// original code
// if($check_voted == 0) {
// proposed by #birgire
if( $check_voted == 0 || ( is_array( $check_voted ) && 2 >= count( $check_voted ) ) ) {
wp-polls.php, line 1323
// original code
// if($check_voted == 0) {
// alternative of code proposed by #birgire
if( ($check_voted == 0) || count($check_voted) <= 2)
I couldn't see any available filters for this, when I skimmed through the plugin source.
I wonder if it would work, if you replace line #1323 of the wp-polls.php file:
if($check_voted == 0) {
with:
if( $check_voted == 0
|| ( is_array( $check_voted ) && 2 >= count( $check_voted ) ) ) {
to limit the number of votes per IP to 2.
Additionally:
Replace line #140 with:
if( !is_array($check_voted) && intval($check_voted) > 0
|| (is_array($check_voted) && sizeof($check_voted) > 1)
|| ($poll_active == 0 && $poll_close == 1)) {
But I don't recommend in general to modify plugin files, since it will be restored in the next plugin update.
Sidenote: The plugin is not using the recommended $wpdb->prepare() when preparing the SQL for $wpdb->query().

php google weather error handling issue

I have a php script that interacts with the google weather api.Everything works ok, but if I test it with a false location like "blahblah" I get a slew of errors in the browser despite having some validation in my script. The php code is as follows:
<?php
function googleWeather($location){
$url = "http://www.google.com/ig/api?weather=" . urlencode($location);
$xml = simplexml_load_file($url);
$current_conditions = $xml->xpath("/xml_api_reply/weather/current_conditions");
$temp_c = $current_conditions[0]->temp_c['data'];
$temp_f = $current_conditions[0]->temp_f['data'];
$result = sprintf("Current temperature is %s° Celsius %s° Fahrenheit",$temp_c,$temp_f);
return $result;
}
$loc = "blahblah";
$goog = googleWeather($loc);
if($goog == false){
echo "An error occurred";
}
else{
echo $goog;
}
?>
The error that is rendered in the browser is as follows:
( ! ) Notice: Undefined offset: 0 in C:\wamp2\www\phpAcademy\GoogleWeatherApi\TMP5c2a1z4emd.php on line 11
Call Stack
# Time Memory Function Location
1 0.1086 370080 {main}( ) ..\TMP5c2a1z4emd.php:0
2 0.1086 370184 googleWeather( ) ..\TMP5c2a1z4emd.php:23
( ! ) Notice: Trying to get property of non-object in C:\wamp2\www\phpAcademy\GoogleWeatherApi\TMP5c2a1z4emd.php on line 11
Call Stack
# Time Memory Function Location
1 0.1086 370080 {main}( ) ..\TMP5c2a1z4emd.php:0
2 0.1086 370184 googleWeather( ) ..\TMP5c2a1z4emd.php:23
( ! ) Notice: Undefined offset: 0 in C:\wamp2\www\phpAcademy\GoogleWeatherApi\TMP5c2a1z4emd.php on line 12
Call Stack
# Time Memory Function Location
1 0.1086 370080 {main}( ) ..\TMP5c2a1z4emd.php:0
2 0.1086 370184 googleWeather( ) ..\TMP5c2a1z4emd.php:23
( ! ) Notice: Trying to get property of non-object in C:\wamp2\www\phpAcademy\GoogleWeatherApi\TMP5c2a1z4emd.php on line 12
Call Stack
# Time Memory Function Location
1 0.1086 370080 {main}( ) ..\TMP5c2a1z4emd.php:0
2 0.1086 370184 googleWeather( ) ..\TMP5c2a1z4emd.php:23
Google returns the following for an invalid location
<xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"><problem_cause data=""/></weather></xml_api_reply>
This xml doesnt have the element "current_conditions" inside "weather". Thats casusing the error.
Check if exists then proceed which can be done with some thing like
if($current_conditions){
// you code here
} else {
return false;
}
I replaced blahblah with real city and it started to work.
Probably you enter your city address with wrong syntax.
Try to change your location, for example Zurich
The errors are pretty self-explanatory, $current_conditions doesn't contain what you think it does:
$current_conditions = $xml->xpath("/xml_api_reply/weather/current_conditions");
$temp_c = $current_conditions[0]->temp_c['data'];
// ^^^ undefined index
$temp_f = $current_conditions[0]->temp_f['data'];
// ^^^ Trying to access property of non object
You are not validating the result of $current_conditions:
if (is_array($current_conditions) && isset($current_conditions[0]))
{
$temp_c = $current_conditions[0]->temp_c['data'];
$temp_f = $current_conditions[0]->temp_f['data'];
}
Furthermore, you may need to validate the existence of the object properties, as well as the existence of the data key in those properties:
if (is_array($current_conditions) && isset($current_conditions[0]))
{
$cur = $current_conditions[0];
if (isset($cur->temp_c) && isset($cur->temp_c['data']))
{
$temp_c = $cur->temp_c['data'];
}
if (isset($cur->temp_f) && isset($cur->temp_f['data']))
{
$temp_f = $cur->temp_f['data'];
}
}
This is quite verbose, but to be absolutely sure to avoid these notices - it's what you have to do. You can and should turn off error reporting once you go live with your application. Notices are meant to help you debug code, if you understand how they're being generated and can safely ignore them, it may be OK.
And, if you feel confident that you know the API will return FALSE for an invalid query, just check if $current_conditions === FALSE and you can skip all the extra validation.

Categories