cancaseiro Posted April 4, 2007 Share Posted April 4, 2007 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 Link to comment https://forums.phpfreaks.com/topic/45527-how-to-get-rid-of-undefined-offset-1-message/ Share on other sites More sharing options...
monk.e.boy Posted April 4, 2007 Share Posted April 4, 2007 I don't think there is a '=>' in $line. If not, then it will only return one string, and the list() will raise a warning that $content is null. monk.e.boy Link to comment https://forums.phpfreaks.com/topic/45527-how-to-get-rid-of-undefined-offset-1-message/#findComment-221044 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.