I get these error from nowhere... What is wrong?
Warning: Division by zero in
/home//public_html/wp-content/plugins//includes/classes/controller/SearchPageController.php
on line 60
Warning: Invalid argument supplied for foreach() in
/home/vineanimals/public_html/wp-content/plugins/*/includes/classes/controller/ImportPageController.php
on line 70
Warning: array_merge(): Argument #1 is not an array in
/home//public_html/wp-content/plugins//includes/classes/controller/AImportPageController.php
on line 75
line codes:
line 60: $last = ceil($load_products_result['total'] / $load_products_result['per_page']);
line 70-75:
foreach ($product['sku_products']['variations'] as $var) {
if (isset($var['image'])) {
$product['all_images'][] = $var['image'];
}
}
$product['all_images'] = array_merge($product['all_images'], $this->woocommerce_model->get_images_from_description($product['description']));
}
A) Check your SQL query or Array feed
B) Then change
$last = ceil($load_products_result['total'] / $load_products_result['per_page']);
to:
$last = ( isset($load_products_result['per_page']) ? ceil($load_products_result['total'] / $load_products_result['per_page']) : 0)
C) and change the foreach as below:
if (is_array($product['sku_products']['variations'])){
foreach ($product['sku_products']['variations'] as $var) {
if (isset($var['image'])) {
$product['all_images'][] = $var['image'];
}
}
if (isset($product['description']) && is_array($product['all_images']) && is_array($this->woocommerce_model->get_images_from_description($product['description'])) )
$product['all_images'] = array_merge($product['all_images'], $this->woocommerce_model->get_images_from_description($product['description']));
}
Related
I got a whole heap of (foreach()) warnings on my server log. Could they be causing sql timeout on my db via a sql connection leak?
I'm not entirely sure how to resolve the problem but i read that i need to add this bit of code:
if (is_array($values) || is_object($values))
{
foreach ($values as $value)
{
...
}
}
...into my .php but dont really know where to add it or why? sorry noob here again.
Here's my code section for: PHP Warning:
Invalid argument supplied for foreach() in
/hermes/bosnacweb03/bosnacweb03aa/b1245/ipg.tinsflowershop/panga/classes/ImageType.php
on line 148
(Line 148 starts - foreach ($results as $result) {)
/**
* Finds image type definition by name and type
* #param string $name
* #param string $type
*/
public static function getByNameNType($name, $type = null, $order = 0)
{
static $is_passed = false;
if (!isset(self::$images_types_name_cache[$name.'_'.$type.'_'.$order]) && !$is_passed) {
$results = Db::getInstance()->ExecuteS('SELECT * FROM `'._DB_PREFIX_.'image_type`');
$types = array('products', 'categories', 'manufacturers', 'suppliers', 'scenes', 'stores');
$total = count($types);
foreach ($results as $result) {
foreach ($result as $value) {
for ($i = 0; $i < $total; ++$i) {
self::$images_types_name_cache[$result['name'].'_'.$types[$i].'_'.$value] = $result;
}
}
}
$is_passed = true;
}
I assume that all the warnings on my log are the same issue? eg:
PHP Warning: Invalid argument supplied for foreach() in
/hermes/bosnacweb03/bosnacweb03aa/b1245/ipg.tinsflowershop/panga/classes/Category.php
on line 594
I am a beginner.Why following codes show three errors?:
1) Warning: array_keys() expects parameter 1 to be array, integer given in C:\wamp\www\wordpress\wp-content\themes\testtheme\functions.php on line 851
2) Warning: Invalid argument supplied for foreach() in C:\wamp\www\wordpress\wp-content\themes\testtheme\functions.php on line 851
851 line:
foreach (array_keys($team_points + $team_points2) as $key) {
$total_points_final[$key] = (isset($team_points[$key]) ? $team_points[$key] : 0) + (isset($team_points2[$key]) ? $team_points2[$key] : 0);
}
3) Fatal error: Unsupported operand types in C:\wamp\www\wordpress\wp-content\themes\testtheme\functions.php on line 859
859 line:
foreach (array_keys($total_points_final + $team_points3) as $key) {
$total_points_final2[$key] = (isset($total_points_final[$key]) ? $total_points_final[$key] : 0) + (isset($team_points3[$key]) ? $team_points3[$key] : 0);
}
All codes:
$total_points=0;
$team_points;
$team_points2;
$team_points3;
foreach($team_wins as $tw_key=>$tw_val){
$team_points[$tw_key]=$tw_val*3;
}
foreach($team_drawn as $tw_key=>$tw_val){
$team_points2[$tw_key]=$tw_val*1;
}
$total_points_final = array();
$total_points_final2 = array();
foreach (array_keys($team_points + $team_points2) as $key) {
$total_points_final[$key] = (isset($team_points[$key]) ? $team_points[$key] : 0) + (isset($team_points2[$key]) ? $team_points2[$key] : 0);
}
foreach($team_loses as $tw_key=>$tw_val){
$team_points3[$tw_key]=$tw_val*0;
}
foreach (array_keys($total_points_final + $team_points3) as $key) {
$total_points_final2[$key] = (isset($total_points_final[$key]) ? $total_points_final[$key] : 0) + (isset($team_points3[$key]) ? $team_points3[$key] : 0);
}
You can only supply array_keys() a valid array which it inturn returns the keys of the array passed into it as an argument.
It would never except any variable type
You can create a new empty array like this:
$arr = [];
Then add those values to the empty array like this:
$arr[] = $post_id;
$arr[] = $post_total;
Now you can call the new array in your array_keys($arr)
1. array_keys() => The array_keys() function takes only array parameter, you cannot pass integer or string.
example:
$array = array("name"=>"xyz", "email"=>"xxxxxxx#xxxx.xxx");
array_keys($array); //Valid
array_keys(2);//Invalid/Error
2. foreach
foreach is a looping statement, you cannot pass inetger or string to loop it,
pass array to foreach, you cannot pass empty array to foreach,
check empty condition before passing to foreach
example:
foreach($array as $key=>$value) { //valid input for foreach
echo $value;
}
I have a wordpress function that calls an API via Zebra_cURL and then uses json_decode to render the data appropriately. Yet there is erratic behavior and the following two errors are occurring: Trying to get property of non-object in [template file] on line 42 and through lines 50.
The lines in question are below:
//Course display callback function
function display_courses($result) {
$result->body = json_decode(html_entity_decode($result->body));
$title = $result->body[0]->{'Title'};
$term = $result->body[0]->{'Term'};
$meetings = $result->body[0]->{'Meetings'};
$status = $result->body[0]->{'Status'};
$course_number = $result->body[0]->{'OfferingName'};
$clean_course_number = preg_replace('/[^A-Za-z0-9\-]/', '', $course_number);
$credits = $result->body[0]->{'Credits'};
$instructor = $result->body[0]->{'InstructorsFullName'};
$description = $result->body[0]->{'SectionDetails'}[0]->{'Description'};
}
And then later I get an error Invalid argument supplied for foreach() in [template file] on line 64 which is:
//Call callback function
function parse_courses($result) {
$result->body = json_decode(html_entity_decode($result->body));
$course_data = array();
foreach($result->body as $course) {
[code]
}
[more code]
}
What exactly am I doing wrong here to cause these errors?
Here's a link to the full template file. Apologies for the vast code dump but I'm a bit in over my head with this.
You must always verify the output of functions/methods if the result if "as expected"
In your case, you did not check what json_decode() has returned
function parse_courses($result) {
$result = json_decode(html_entity_decode($result->body));
if ((!is_array ($result) && !is_object($result)) ||
(is_array($result) || count($result) == 0) ||
(json_last_error() != JSON_ERROR_NONE)) { // only for PHP >= 5.3.0
// log the error or warning here ...
// $input = $result;
// $output = print_r ($result, TRUE);
// Only for PHP >= 5.3.0
// json_last_error();
// json_last_error_msg();
return -1;
}
$result->body = $result;
$course_data = array();
foreach($result->body as $course) {
[code]
}
[more code]
}
I've got Undefined Offset error, but I don't know what's wrong.
Here's the code
<?php
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
$fileName = "$documentRoot/Tutorials/PHP/Assignments/Assignment 3/data/quote.txt";
$filePointer = fopen($fileName, 'r');
$lineCounter = 0;
$display = "";
while(true)
{
$line = fgets($filePointer);
list($firstName, $lastName, $contactMethod, $phoneMail, $resideCity, $comments) = explode("|", $line);
if (!isset($comments))
{
$comments = "";
}
$lineCounter++;
if(feof($filePointer))
{
break;
}
if($lineCounter % 2 == 0)
{
$style = "style = 'background-color:white';";
}
else
{
$style = "style = 'background-color:lightgray';";
}
//Write to table
print"<tr $style>";
print"<td>$firstName</td>";
print"<td>$lastName</td>";
print"<td>$contactMethod</td>";
print"<td>$phoneMail</td>";
print"<td>$resideCity</td>";
print"<td>$comments</td>";
print"</tr>";
}
fclose($filePointer);
?>
I added:
if (!isset($comments))
{
$comments = "";
}
Because I assume the offset error appears because I didn't type in anything in comments. However, I still get this error.
Please help.
Here's the error message:
Notice: Undefined offset: 5 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 4 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 3 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 2 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
Notice: Undefined offset: 1 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32
The problem is before your isset... so when you do explode and pass results to list... that expects 6 parametes only receive 5 or less and you get the error.
I suggest the following replacement:
$line = fgets($filePointer);
$data=explode("|", $line);
while(count($data)<6) $data[]="";
list($firstName, $lastName, $contactMethod, $phoneMail,
$resideCity, $comments) = $data;
That will add enough data for list to set vars.
and should work!
Happy coding!
I am building my first wordpress plugin and the error log is throwing up the following errors.
[06-Jul-2014 20:07:21 UTC] PHP Warning: array_merge(): Argument #2 is not an array in \wp-content\plugins\test-plugin\screen.php on line 49
[06-Jul-2014 20:07:21 UTC] PHP Warning: array_merge(): Argument #2 is not an array in \wp-content\plugins\test-plugin\screen.php on line 50
[06-Jul-2014 20:07:21 UTC] PHP Warning: end() expects parameter 1 to be array, null given in \wp-content\plugins\test-plugin\screen.php on line 323
[06-Jul-2014 20:07:21 UTC] PHP Warning: Invalid argument supplied for foreach() in \wp-content\plugins\test-plugin\screen.php on line 324
For the errors on line 49 & 50 the code is:
function register_settings() {
global $menu;
global $submenu;
$this->menus = array_merge(array(), $menu);
$this->submenus = array_merge(array(), $submenu);
$this->settings = get_option( $this->settings_name );
register_setting( 'admin-theme', $this->settings_name );
}
For the errors on lines 323 & 324 the code is:
function admin_menu() {
global $menu;
global $submenu;
// update menu
end( $menu );
foreach ($menu as $k=>&$v){
$id = explode(' <span', $v[0]);
$slug = 'menu_'.strtolower( str_replace( ' ','_',$id[0] ) );
$slug_hide = $slug.'_hide';
if($id[0] != NULL && $this->get_setting($slug) !== NULL){
$v[0] = $this->get_setting($slug). ( isset($id[1]) ? ' <span '.$id[1] : '' );
}
if( $this->get_setting($slug_hide) ){
unset($menu[$k]);
}
// update the submenu
if( isset($submenu[$v[2]]) ){
foreach ($submenu[$v[2]] as $key=>&$val){
$id = explode(' <span', $val[0]);
$slug_sub = $slug.'_'.strtolower( str_replace( ' ','_',$id[0] ) );
$slug_sub_hide = $slug_sub.'_hide';
if($id[0] != NULL && $this->get_setting($slug_sub) !== NULL){
$val[0] = $this->get_setting($slug_sub). ( isset($id[1]) ? ' <span '.$id[1] : '' );
}
if( $this->get_setting($slug_sub_hide) ){
unset( $submenu[$v[2]][$key] );
}
}
}
}
}
I can't seem to figure it out so if anyonecanhelp or point me in the right direction it would be greatly appreciated.
first you need to check your both variables is array
with is_array() like if(is_array($menu))
if not use type cast to convert variable in array
if(is_array($menu))
$this->menus = array_merge(array(), $menu);
else
$this->menus = array_merge(array(), (array)$menu);
if(is_array($submenu))
$this->submenus = array_merge(array(), $submenu);
else
$this->submenus = array_merge(array(), (array)$submenu);