Page 1 of 1

[SOLVED] Undefined Offset Error

Posted: Sat Aug 14, 2004 9:21 am
by Getran
Sometimes when i refresh my page i get this error:

Notice: Undefined offset: 9 in *hidden*\random.php on line 9

this is the line it is referring to:
$RandomRotator1 = ereg_replace("\n","",$file[$randcount]);

This is the whole of random.php

Code: Select all

<?php
$RandomRotator1 = "";
$filename = "random.txt";
$file = file($filename);
srand((double)microtime()*1000000);
while ($RandomRotator1 == "") {
if(!isset($sp)){
$randcount = rand(0,count($file));
$RandomRotator1 = ereg_replace("\n","",$file[$randcount]);
}else{
$RandomRotator1 = ereg_replace("\n","",$file[$sp]);
}
}
print "$RandomRotator1";

?>
where i have it appearing i just use include("random.php");

As you can probably see it is supposed to take a random line from a file called random.txt and display it. It does work (it displays a random advertisement) but i get the error on top of it...

Can anybody help please ?

Posted: Sat Aug 14, 2004 9:37 am
by markl999
It's $randcount = rand(0,count($file)); that's causing it.
You probably want $randcount = rand(0,count($file)-1);

Posted: Sat Aug 14, 2004 9:42 am
by Getran
ah, i think that did the trick..thx man