helraizer Posted January 20, 2008 Share Posted January 20, 2008 Hi folks, I have a chatbox I made for a forum signature, it looks like this: The comments are recorded in a .line file in the format line 1: CHATBOXTEXT line 2: 4 Those two are always there. then the comments line 1: Username line 2: color line 3: font line 4: comment In the file that reads the comments and orders them it has a function called 'reverseiterate' which writes the comments in reverse order, so the newest one goes on the top. function getReverseIterate() { // Returns an array storing data for the current line in reverse order $linedata = $this->curline_reverse * $this->metadatalines; for($i=0;$i<$this->metadatalines;$i++) { $curlinedata[] = $this->linesdata[$i + $linedata]; //line 75 } $this->curline_reverse--; return $curlinedata; } The code to make the image from it includes that file. It works perfectly well when there is a lot of comment data, but when there is only a few bits of data it comes up with the error Notice: Undefined offset: -4 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -3 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -2 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -1 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -8 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -7 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -6 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -5 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -12 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -11 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -10 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -9 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -16 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -15 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -14 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -13 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -20 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -19 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -18 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -17 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -24 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -23 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -22 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -21 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -28 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -27 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -26 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -25 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -32 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -31 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -30 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -29 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -36 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -35 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -34 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -33 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -40 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -39 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -38 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -37 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -44 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -43 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -42 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Notice: Undefined offset: -41 in /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5 on line 75 Warning: Cannot modify header information - headers already sent by (output started at /home/sites/helraizer.co.uk/public_html/mychatbox/linesfile.php5:75) in /home/sites/helraizer.co.uk/public_html/mychatbox/showimage.php5 on line 98 Why does it do this, can you imagine? If you need more code I shall supply it. Sam Quote Link to comment https://forums.phpfreaks.com/topic/86957-odd-undefined-offset-error-in-code/ Share on other sites More sharing options...
Barand Posted January 20, 2008 Share Posted January 20, 2008 It means you are trying to access an array using a key that doesn't exist in the array - probably because they are all negative Quote Link to comment https://forums.phpfreaks.com/topic/86957-odd-undefined-offset-error-in-code/#findComment-444674 Share on other sites More sharing options...
btherl Posted January 21, 2008 Share Posted January 21, 2008 We would need more code to help diagnose it .. as for what causes the error it's as Barand says, the code is trying to access an array using a key that isn't in the array. If you want to debug it yourself, I would look at how $linedata and $this->metadatalines are set, and see if there's any funny business going on with the variables that are used to set those ones. And I would print out all those values, looking for anything odd. Quote Link to comment https://forums.phpfreaks.com/topic/86957-odd-undefined-offset-error-in-code/#findComment-444681 Share on other sites More sharing options...
helraizer Posted January 21, 2008 Author Share Posted January 21, 2008 It should work though, or at least it used to work perfectly. It should count the number of comments and calculate accordingly, where it has 0 comments or 150 but it seems to have stopped doing so. If you want to have a scan through, here's the code I wrote for it. See if you can see why it's doing what it's doing. showimage.php5 <?php error_reporting(E_ALL); ini_set('display_errors', true); include("linesfile.php5"); $linesDataFile = new DataFile("data.line"); //$image = ImageCreate(660,240); // create the image canvas $image = ImageCreateFromPNG("../background.png"); $blue = ImageColorAllocate($image, 200, 200, 255); // prepare some blueness $black = ImageColorAllocate($image, 0, 0, 0); // ... and whiteness $cur_line_y = 63; // This stores how far down the image the current line will print $cur_line_x = 24; // This stores how far across the image the current line will print $pagecharwidth = 75; // this is the maximum length of the line before it wraps; $lineheight = 15; // This is how much to move down to print the next line $pagelinelimit = 12; // This is the maximum number lines of text that can be displayed ImageFill($image, 0, 0, $blue); // fill the canvas //ImageString($image, 3, 15, $cur_line_y, trim(stripslashes($wordwrapped[0])), $black); $numberOfLines = $pagelinelimit; for($i=0;$i<$numberOfLines;$i++) { $data = $linesDataFile->getReverseIterate(); if (count($data)==0) continue; $name = "[" . $data[0] . "] "; $color = $data[1]; $font = $data[2]; $line = $data[3]; if($name == "[] ") { $name = ""; $line = $name . $line; } Else { $line = $name . $line; } //ImageString($image, 2, $cur_line_x, $cur_line_y, trim($line), getColor($color)); imagettftext($image,10,0,$cur_line_x,$cur_line_y,getcolor($color),getfont($font),trim($line)); $cur_line_y += $lineheight; } function getColor($color) { global $image; switch($color) { case "white" : return ImageColorAllocate($image, 255, 255, 255); case "yellow" : return ImageColorAllocate($image, 255, 255, 0); case "green" : return ImageColorAllocate($image, 0, 255, 0); case "orange" : return ImageColorAllocate($image, 255, 127, 36); case "aqua" : return ImageColorAllocate($image, 0, 255, 255); default: return ImageColorAllocate($image, 255, 255, 0); } } function getfont($font) { global $image; global $font; switch($font) { case "fixedsys" : return "../fixedsys.ttf"; case "Courbd" : return "../courbd.ttf"; case "arial" : return "../arialbd.ttf"; case "timesnr" : return "../timesbd.ttf"; case "calibri" : return "../calibrib.ttf"; case "comicsans" : return "../comicsans.ttf"; case "palab" : return "../palab.ttf"; default: return "../palab.ttf"; } } header("Content-Type: image/gif"); // tell the browser what we're gonna give it ImageGif($image); // paint the image in browser ImageGif($image, "./chatbox.gif"); //export as png file ImageDestroy($image); // clean up resources ?> linesfile.php5 <?php /**************** * linesfile.php5 * Oct 8th. 2007 * Script by Samuel Boulton 2007 * this file loads up our data file storing the lines for the text box. **** File Format Definition: .line Line1: CHATBOXTEXT <- file descriptor Line2: the number of lines used for each line to appear in the chatbox Line3 onwards : data for the chatbox Line data format: Line1: the name of the user posting the data Line2: a color available from a specific list Line3: the text to appear in the chatbox */ class DataFile { var $filedata; // an array storing the entire file raw var $metadatalines; // the number of lines in the file that store one line of text int he chat pad var $linesdata; // this is the data of the lines themselves, without any header information fromt he file var $curline; // a pointer to the current line that will appear in the chatpad. var $curline_reverse; var $filename; // the name of the file function DataFile($filename) { // Object constructor: open the file, check the format, load the contents into "linesdata" variable $descriptor = "CHATBOXTEXT"; $this->filedata = file($filename); $this->filename = $filename; if (empty($this->filedata)) { $this->errorDataFile("No data in file!"); } if (trim($this->filedata[0]) != $descriptor) { $this->errorDataFile("Invalid data file format : " . $this->filename); } $this->metadatalines = $this->filedata[1]; for ($i=2;$i<count($this->filedata);$i++) { $this->linesdata[] = trim($this->filedata[$i]); } $this->curline_reverse = $this->fileSize() - 1; // for some gay reason it needs to be -1 of the total fielsize :S $this->curline = 0; } function fileSize() { // Returns the number of lines to appear int he chatbox return count($this->linesdata) / $this->metadatalines; } function getNextLine() { // Return an array storing the data for the current line to appear in the chatbox $linedata = $this->curline * $this->metadatalines; for($i=0;$i<$this->metadatalines;$i++) { $curlinedata[] = $this->linesdata[$i + $linedata]; } $this->curline++; return $curlinedata; } function getReverseIterate() { // Returns an array storing data for the current line in reverse order $linedata = $this->curline_reverse * $this->metadatalines; for($i=0;$i<$this->metadatalines;$i++) { $curlinedata[] = $this->linesdata[$i + $linedata]; } $this->curline_reverse--; return $curlinedata; } function writeNewLine($data) { if(count($data) != $this->metadatalines) return false; for($i=0;$i<$this->metadatalines;$i++) { if($i==$this->metadatalines-1) { file_put_contents($this->filename, $data[$i] ,FILE_APPEND); break; } file_put_contents($this->filename, $data[$i] . "\n" ,FILE_APPEND); } return true; } function checkFormat() { if(($this->fileSize() % 3) != 0) return false; return true; } function errorDataFile($message) { die($message); } } ?> index.php5 (the php from it) <?php include("./linesfile.php5"); $filename = "data.line"; set_magic_quotes_runtime(0); if ($_POST['submit']) { // grab the inputted text $text = htmlspecialchars(stripcslashes($_POST['input'] . "\n")); $username = htmlspecialchars(stripslashes($_POST['username'])); $color = $_POST['color']; $font = $_POST['font']; $ip = $_SERVER['REMOTE_ADDR'] . "\n"; $_SESSION['username'] = $username; $_SESSION['color'] = $color; $dirty = array(); //array of censor words here, I removed them to keep this thread clean. foreach($dirty AS $bad_word){ $text = preg_replace("/$bad_word/i","****", $text); } $data[] ="\n". htmlspecialchars_decode(substr($username,0,10)); $data[] =trim($color); $data[] =trim($font); $data[] =htmlspecialchars_decode(trim(substr($text,0,75))); $datafile = new DataFile($filename); if(!$datafile->writeNewLine($data)) die("Error writing to file"); } ?> I can't understand why the array is negative and why it only comes up with the error when there is little data in it. The index script works and still writes to the file, it just doesn't show up, until you have a lot of data in the file. Any thoughts? Sam Quote Link to comment https://forums.phpfreaks.com/topic/86957-odd-undefined-offset-error-in-code/#findComment-444684 Share on other sites More sharing options...
btherl Posted January 21, 2008 Share Posted January 21, 2008 It should work though, or at least it used to work perfectly. All code should work I can't understand why the array is negative and why it only comes up with the error when there is little data in it. The index script works and still writes to the file, it just doesn't show up, until you have a lot of data in the file. Any thoughts? Sam I can guess that one of your values that you think is positive is going negative. I would first look at $this->metadatalines and $this->currline_reverse. If you find that they are not what you expected, then you can go back and look at how they were set, and see where the error is coming in. Perhaps $this->fileSize() returns 0, leading to a -1 in $this->currline_reverse Quote Link to comment https://forums.phpfreaks.com/topic/86957-odd-undefined-offset-error-in-code/#findComment-444687 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.