Asked  8 Months ago    Answers:  5   Viewed   31 times

I have a form that users fill out, and on the form there are multiple identical fields, like "project name", "project date", "catagory", etc. Based on how many forms a user is submitting, my goal is to:

  1. loop over the number of forms
  2. create individual SQL insert statements

However, PHP throws me a NOTICE that I don't seem to understand:

Notice:

Notice: Uninitialized string offset: 1 ...dataPasser.php on line 90

PHP

$myQuery = array();

if ($varsCount != 0)
{
  for ($i=0; $i <= $varsCount; $i++)
  {
    $var = "insert into projectData values ('" . $catagory[$i] . "', '" .  $task[$i] . "', '" . $fullText[$i] . "', '" . $dueDate[$i] . "', null, '" . $empId[$i] ."')";
    array_push($myQuery, $var);     
  }
}

There are references to this issue I am having, but they are not exact and I am having trouble deducing where the actual problem stems from. I would greatly appreciate any help in understanding what is causing the array to not initialize properly.

 Answers

13

This error would occur if any of the following variables were actually strings or null instead of arrays, in which case accessing them with an array syntax $var[$i] would be like trying to access a specific character in a string:

$catagory
$task
$fullText
$dueDate
$empId

In short, everything in your insert query.

Perhaps the $catagory variable is misspelled?

Wednesday, March 31, 2021
 
clean_coding
answered 8 Months ago
28

$options is a string in this context, not an array. You are trying to treat it like an array, which doesn't work. The logic in your code will never work; it's not clear what you're trying to do with $options[$id].

The reason you get

Uninitialized string offset: 0

is that $id is not initialized anywhere, so PHP treats it as 0. You are saying, "look at this string ($options) and get the array value at key 0," and PHP throws its hands up because that doesn't make sense.

Your various solutions won't work for several other reasons. stripslashes( isset( $options[$id] ); fails because (1) $options[$id] doesn't exist and (2) stripslashes() takes a string parameter, but you are handing it a boolean value. Same problem with esc_attr( isset( $options[$id] ) );

Friday, May 28, 2021
 
conmen
answered 5 Months ago
39

When you set cell values individually, you have the option of setting the datatype explicitly, but when you use the fromArray() method, you don't have this option.

However, by default, PHP uses a default value binder to identify datatypes from the values passed, and set the cell datatype accordingly. This default behaviour is defined in a class /PHPExcel/Cell/DefaultValueBinder.php.

So you can create your own value binder, as described in the PHPExcel Documentation, that would set every value as a string datatype.

Something like:

class PHPExcel_Cell_MyColumnValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
{
    protected $stringColumns = [];

    public function __construct(array $stringColumnList = []) {
        // Accept a list of columns that will always be set as strings
        $this->stringColumns = $stringColumnList;
    }

    public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        // If the cell is one of our columns to set as a string...
        if (in_array($cell->getColumn(), $this->stringColumns)) {
            // ... then we cast it to a string and explicitly set it as a string
            $cell->setValueExplicit((string) $value, PHPExcel_Cell_DataType::TYPE_STRING);
            return true;
        }
        // Otherwise, use the default behaviour
        return parent::bindValue($cell, $value);
    }
}

// Instantiate our custom binder, with a list of columns, and tell PHPExcel to use it
PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_MyColumnValueBinder(['A', 'B', 'C', 'E', 'F']));

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->fromArray($dataArray,null,"A2");
Friday, May 28, 2021
 
Wilk
answered 5 Months ago
13

There's a lot been written about PHPExcel and memory use, and I'm not going to repeat it all here.

Try reading some of the threads on the PHPExcel discussion board discussing the issue, such as this one; or previous answers here on SO such as this one or this one

Tuesday, July 6, 2021
 
Slinky
answered 4 Months ago
32

You are occasionally overrunning $characters because of strlen($characters), which should be strlen($characters) - 1. Your random range should begin with zero, and you need to end with the length minus one. If the length of $characters is 10, then the last zero-based array index is nine, and that becomes your upper bound for random selection.

$string='';
$characters = "0123456789abcdef";
for ($p = 0; $p < $length ; $p++) {
    $string .= $characters[mt_rand(0, strlen($characters) - 1)]; 
}

Because you are choosing randomly, you wouldn't always receive the notice -- it would only occur if mt_rand() returned its maximum value at some point in your loop.

Friday, August 13, 2021
 
vuliad
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :