POST method array - php

I have a problem with creating array on POST method. Code:
<input class='form-control' name='ern[".$row['ID']."][value]' type='text' placeholder='€'>
$comp = isset($_GET['comp']) ? $_GET['comp'] : false;
$comp = ($comp !== false) ? preg_replace('/[^A-Za-z0-9]/', '', $comp) : false;
$ern = isset($_POST['ern']) ? $_POST['ern'] : false;
$ern = ($ern !== false) ? preg_replace('/[^0-9]/', '', $ern) : false;
echo $ern[$comp]['value'];
Geting errors:
Notice: Array to string conversion in
D:\Tinklapis\Graffiz-CMS\php\update_order.php on line 7
Warning: Illegal string offset 'value' in D:\Tinklapis\Graffiz-CMS\php\update_order.php on line 9
Notice: Uninitialized string offset: 0 in D:\Tinklapis\Graffiz-CMS\php\update_order.php on line 9
And nothing value printed.

Related

PHP Warning: Illegal string offset after update to php 7.3.X on line 71 [duplicate]

This question already has answers here:
Illegal string offset Warning PHP
(17 answers)
Closed 2 years ago.
The code below worked correctly in PHP 7.0, but after upgrading to 7.3 it now gives this warning:
PHP Warning: Illegal string offset on line 71
// Loop through routes
$array_result = "";
$array_index_result = "";
foreach($routes as $key => $route){
$route_parts = explode("/", $route);
$index = 0;
$match = TRUE;
// Reset array_result
$array_result = "";
foreach($route_parts as $route_part)
{
if(substr($route_part,0,1) != "$"){
if(isset($url_parts[$index])){
if($route_parts[$index]!=$url_parts[$index]) $match = FALSE;
}else{
$match = FALSE;
}
}else{
if(isset($url_parts[$index])){
$array_result[substr($route_parts[$index],1)] = $url_parts[$index];
}
}
if(isset($url_parts[$index])){
$array_index_result[$index] = $url_parts[$index];
$index++;
}
}
if($match && (count($route_parts) == count($url_parts)))
{
$Info = new ModRewriteInfo($array_result, $array_index_result);
return $Info;
}
}
// No match found
return FALSE;
Line 71 is
$array_result[substr($route_parts[$index],1)] = $url_parts[$index];
The code runs on PHP 7.0 but not in PHP 7.3. Why is that?
$array_result is a string.
$array_result = "";
In PHP 7.0 and before, assigning to an index of an empty string with [] would result in an array. From 7.1 onward, it stays a string and you get that warning.
See the documentation for backward incompatible changes in PHP 7.1: Assignment via string index access on an empty string
From the context of the rest of the code, it doesn't look like that variable should ever be a string, so you should initialize it with $array_result = []; instead.

count(): Parameter must be an array or an object that implements Countable, Uninitialized string offset: 0

I have a problem with my code it shows
count(): Parameter must be an array or an object that implements
Countable and Uninitialized string offset: 0
$site = $_POST['site'];
$url = $_POST['url'];
$query = '';
for($count = 0; $count<count($site); $count++){
$site_name = mysqli_real_escape_string($con, $site[$count]);
$url_name = mysqli_real_escape_string($con, $url[$count]);
if($site_name != '' && $url_name != ''){
$query .='INSERT INTO `url` (`site`, `http`) VALUES ("'.$site_name.'", "'.$url_name.'")';
}
}
The expected out is to insert data into the database
Make sure $_POST['site'] is an array that can implement Countable
$site = $_POST['site'] ?? []; // php 7
$site = is_array($_POST['site']) ? $_POST['site'] : [] // php <7
your $site variable is not countable,
use var_dump($site) to see it's value and type

What is wrong with the lines? (php)

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']));
}

Illegal String Offset 'class' in PHP

I received this warning
Warning: Illegal string offset 'class' in C:\xampp\htdocs\myweb\libraries\cms\html\html.php on line 971
Warning: Illegal string offset 'class' in C:\xampp\htdocs\myweb\libraries\cms\html\html.php on line 972
Warning: Illegal string offset 'class' in C:\xampp\htdocs\myweb\libraries\cms\html\html.php on line 972
On joomla 3.3 (file path \libraries\cms\html\html.php) and the code is:
public static function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = null)
{
static $done;
if ($done === null)
{
$done = array();
}
$attribs['class'] = isset($attribs['class']) ? $attribs['class'] : 'input-medium';//happen here
$attribs['class'] = trim($attribs['class'] . ' hasTooltip');//happen here
$readonly = isset($attribs['readonly']) && $attribs['readonly'] == 'readonly';
$disabled = isset($attribs['disabled']) && $attribs['disabled'] == 'disabled';
if (is_array($attribs))
{
$attribs = JArrayHelper::toString($attribs);
}
.......
Shows that it had to do with $attribs['class']. And if I'm correct illegal string offset could mean that $attribs is not an array but a string. So is there any way to correct this?
I'm on PHP5.4
This:
public static function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = null)
{
}
Should be:
public static function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = array())
{
}
Point is that by setting "null" to the variable by default, you actually say it's an empty string, or an empty variable. By setting "array()", you define the variable being an empty array.
Just by looking a little bit further, there is an array expected, since the code is looking for specific array_keys like: 'readonly' AND 'disabled'.

Undefined Offset error when read a file into table

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!

Categories