PHP Error Undefined Offset, SQL Export tool - php

I keep getting the error:
PHP Notice: Undefined offset: 1 in /home/mydomain/mydomain.com/admin/model/tool/export_xls.php on line 150
I've looked at the other similar posts but none of those resolutions work on my code. I don't see any numbers related to the array, and I've double checked for missing closing } . I asked the code developer and all he did was get it to stop displaying the error, but it still shows up (several thousand times) in my error log. line 150 is the blank line above the code
"//Get all options values to each option"
Code:
public function get_all_options()
{
$this->load->model('catalog/option');
$all_options = $this->model_catalog_option->getOptions();
$options_final = array();
//Format options
foreach ($all_options as $key => $op) {
$options_final[$op['name'].'_'.$op['type']] = array(
'option_id' => $op['option_id'],
'option_name' => $op['name'],
'option_values' => array()
);
}
//Get all options values to each option
foreach ($options_final as $option_name => $op) {
$option_values = $this->model_catalog_option->getOptionValues($op['option_id']);
//Format option values
$option_values_final = array();
foreach ($option_values as $key => $op) {
$option_values_final[$op['name']] = $op['option_value_id'];
}
$options_final[$option_name]['option_values'] = $option_values_final;
}
return $options_final;
}

This is because you are accessing an array which index is not set . try something like this
//Format options
foreach ($all_options as $key => $op) {
$options_final[$op['name'].'_'.$op['type']]
= array(
'option_id' =>isset($op['option_id']) ?
:$op['option_id']:null,
'option_name' => isset($op['option_name'])
? :$op['option_name']:null,
'option_values' => array()
);
}

Related

PHP 8.0 - How to replace deprecated each() - with foreach()? [duplicate]

This question already has answers here:
How can I update code that uses the deprecated each() function?
(12 answers)
Closed last month.
I understand that the each function() is deprecated in PHP 8.0.
Updated: I'm getting a Fatal error: Uncaught Error: Call to undefined function each() line 742 - [line 742 is this line in the code snippet below: list($orig,$values) = each($where); ]
I am trying to replace it in the below code with foreach() as suggested in this post, but I am not experienced enough in PHP or coding to achieve this. Can anyone help?
public function getMetaboxConfig($type) {
static $cache;
if (!empty($cache[$type])) {
return $cache[$type];
}
do_action("pe_theme_metabox_config_$type");
$config =& PeGlobal::$config;
$metaboxes = PeGlobal::$config["metaboxes"];
$pmboxes = empty($config["metaboxes-$type"]) ? null : $config["metaboxes-$type"];
if ($custom = apply_filters("pe_theme_metabox_$type",$pmboxes)) {
//print_r(array_keys(PeGlobal::$config["metaboxes-view"]));
$keys = array_keys($custom);
foreach ( $custom as $key => $value ) {
$metaboxes[$key] = $custom[$key];
$where =& $metaboxes[$key]["where"];
list($orig,$values) = each($where);
if ($orig != $type) {
unset($where[$orig]);
$where[$type] = $values;
}
}
}
$cache[$type] = $metaboxes;
return $metaboxes;
}
The error I am seeing:
Error message
This is an exemple to foreach :
<?php
$myArr = [
'key1' => 'value1',
'key2' => 'value2',
];
foreach($myArr as $k => $v){
//first iteration : $k = 'key1', $v = 'value1'
//second iteration : $k = 'key2', $v = 'value2'
... your code
}
So, you don't need to use array_keys :
foreach ($custom as $key => $value) {
$metaboxes[$key] = $value;
...
}

How fix the "llegal string offset" while uploading excel file into database

I am trying to import an Excel file into my table using the MaatWebsite package. But every time I get errors like these:
Illegal string offset 'Emp'
I am following this tutorial. And here is the code I have tried:
$path = $request->file('attendance_data')->getRealPath();
$data = Excel::load($path)->get();
if($data->count() > 0)
{
foreach($data->toArray() as $key => $value)
{
foreach($value as $row)
{
$insert_data[] = array(
'employee_card' => $row['Emp'],
'attendance_date' => $row['Date'],
'attendance_time' => $row['On'],
);
}
}
if(!empty($insert_data))
{
DB::table('attendance_logs')->insert($insert_data);
}
Any help will be appreciated
I can't comment so I've posted this as an answer.
Have you tried to die and dump the $row to check that it is an array of key => value pairs as you're expecting? The error looks to show up when trying to access a key of an array with numeric keys. This could also happen if $row is a string, as you can access string characters using a numeric index similar to an array.
$path = $request->file('attendance_data')->getRealPath();
$data = Excel::load($path)->get();
if($data->count() > 0) {
foreach ($data->toArray() as $key => $value) {
foreach ($value as $row) {
// Print the data of $row to see what it actually is
// and kill the process
dd($row);
$insert_data[] = array(
'employee_card' => $row['Emp'],
'attendance_date' => $row['Date'],
'attendance_time' => $row['On'],
);
}
}
if (!empty($insert_data)) {
DB::table('attendance_logs')->insert($insert_data);
}
}
This answer looks to be related.

Silverstripe: Instagram Widget - Undefined property: stdClass::$data

Getting this error from an instagram widget:
[Notice] Undefined property: stdClass::$data
GET /
Line 146 in /massi/widget_imagegallery/ImageGalleryWidget.php
Here is the Widget.php code. Line 146 is "foreach ($json->data ....":
$numberOfImages = $this->MaxImageCount;
if (!is_numeric($numberOfImages) || $numberOfImages === 0) {
$numberOfImages = 500;
}
$service = new RestfulService(sprintf('https://api.instagram.com/v1/users/%s/media/recent/?access_token=%s&count=%d', $this->InstagramUserID, $this->InstagramAccessToken, $numberOfImages));
$xml = $service->request()->getBody();
$json = json_decode($xml);
$arrayList = arrayList::create();
$current = 1;
if ($json) {
$index = 0;
foreach ($json->data as $listItem) {
if ($current <= $numberOfImages) {
$data = array(
'ImageNormal' => $listItem->images->standard_resolution->url,
'Shape' => $this->ImageShape( $counter ),
'ExtraClasses' => $this->ImageExtraClasses( $counter ),
'ImageGalleryType' => $this->ImageGalleryType,
'DataSource' => $this->DataSource,
'Pos' => $index,
'Title' => $listItem->caption->text
);
$imageGalleryItemsList->push($data);
}
$counter ++;
$index ++;
}
}
I've got the correct access token and user ID in the settings. It appears that the code isn't getting anything back from Instagram when it requests the data?
The Instagram API returns a json object with all the media stored under the data property:
https://www.instagram.com/developer/endpoints/users/#get_users_media_recent_self
Which explains the foreach ($json->data as $listItem).
So if the data property doesn't exist that could mean either the api returned an error or there is no data.
Dump the json data and you'll know what's wrong.

SimpleXML expecting array error PHP

ok below is my code
<?php
// Last 10 Jobs
function last10IT(){
$xml = simplexml_load_file('http://www.cv-library.co.uk/cgi-bin/feed.xml?affid=101899');
$new_array = array();
//$limit = 5;
//$c = 0;
foreach ($xml->jobs->job as $job) {
// if ($limit == $c) {
// break;
// }
$jobref = $job->jobref;
$title = $job->title;
$date = $job->date;
$new_array[$jobref.$date] = array(
'jobref' => $jobref,
'date' => $date,
'title' => $title,
'salary' => $job->salary,
'location' => $job->location,
);
}
}
ksort($new_array);
$showl = 10;
$n = 0;
foreach ($new_array as $date => $listing) {
print $listing['title'] . PHP_EOL;
}
?>
All I want it to do is filter by category & display a max of 10 results
for example
IT
so is there a way I can pass the category value into the function that I want it to filter by
instead of having to replicate for each category
All I get is :
Warning: ksort() expects parameter 1 to be array, null given in
C:\wamp\www\RECRUITMENTFAIR\functions.php on line 28
Please help guys
It something SO simple causing this error but its driving me mad because I just cannot see it
it is relative simple: you try to use the variable $new_array out of scope:
it is defined within your last10IT() function, but the function ends after the first foreach.
you should either return the array and call the function to get the array or move the part with the ksort and the printing into the function, depending on your needs.
Also was having issues by not having the PHP Extension xmlrpc enabled !
what a tool !
Thats why i was getting failed to open half the time

Checkboxes with watchdog_severity_levels()

I have this code that gets me out some checkboxes with the watchdog severities:
/**
* Checkbox for errors, alerts, e.t.c
*/
foreach (watchdog_severity_levels() as $severity => $description) {
$key = 'severity_errors' . $severity;
$form['severity_errors'][$key] = array(
'#type' => 'checkbox',
'#title' => t('#description', array('#description' => drupal_ucfirst($description))),
'#default_value' => variable_get($key, array()),
);
return system_settings_form($form);
}
I set this $key in my code as:
$key = array_filter(variable_get($key,array()));
I think this set is wrong as the drupal gets me out error.
After that set of $key I call it with the following foreach statement could someone help me with that thing?
foreach ($key as $value) {
if ($value == 'warning') {
blablblablabla....
}
elseif ($value == 'notice') {
blablablbalbal....
}
}
Using your logic, you would store following keys severity_errors0, severity_errors1, severity_errors2, ... in the variable table because the $severity key of your foreach is just the index.
Wouldn't it be easier to store an array of selected severity levels as one entry in the variable table?
Here some example code which does the job for you:
// Retrieve store variable
$severity_levels = variable_get('severity_levels', array());
// Declare empty options array
$severity_options = array();
// Loop through each severity level and push to options array for form
foreach (watchdog_severity_levels() as $severity) {
$severity_options[$severity] = t('#description', array(
'#description' => drupal_ucfirst($severity),
));
}
// Generate checkbox list for given severity levels
$form['severity_levels'] = array(
'#type' => 'checkboxes',
'#options' => $severity_options,
'#default_value' => array_values($severity_levels),
);
return system_settings_form($form);
Now to retrieve the selected severity levels, you do something like this:
// Retrieve store variable
$severity_levels = variable_get('severity_levels', array());
foreach ($severity_levels as $level => $selected) {
if (!$selected) {
// Severity level is not selected
continue;
}
// Severity level is selected, do your logic here
// $level
}
You need to add some debugging to figure out where exactly it's going wrong. Would recommend using dpm() to inspect the code at some of the key stages like 1) after building the form, 2) when you assign the array to $key and 3) before starting the final foreach loop so you can pinpoint where it's going wrong and address it.

Categories