Jump to content

How to get rid of Undefined offset: 1 message


cancaseiro

Recommended Posts

Hi!

 

How can I avoid gettint this message: Undefined offset: 1 in D:\EasyPHP 2.0b1\www\harjutus\leht\get_data.php on line 37

 

Code is:

 

<?php

 

class books {

 

//public

 

public $title = array();

public $image = array();

public $author = array();

public $description = array();

public $year = array ();

public $price = array();

 

// private

 

private $filename = "data.txt";

 

//class constructor

 

function __construct()

{

 

//get the lines as an array

 

  $i=-1;

  $lines = file($this->filename);

 

// strip "\n" at the end of each array

// get each variable in an array

 

  foreach ( $lines as $line) {

 

if (strlen($line) > 2) {

 

$line = rtrim($line);

 

list($what, $content) = explode("=> ", $line); (here lies the problem)

 

if ($what == "Title") {

$i++;

$this->title[$i]=$content;

}

elseif ($what == "Image") {

$this->image[$i]=$content;

}

elseif ($what == "Author") {

$this->author[$i]=$content;

}

elseif ($what == "Description") {

$this->description[$i]=$content;

}

elseif ($what == "Year") {

$this->year[$i]=$content;

}

elseif ($what == "Price") {

$this->price[$i]=$content;

 

};

    };

};

 

} // end constructor

 

} // end GetData

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.