/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Several PHP Undefined offset errors

I am getting the following errors to a random quote script I have:

[CODE]PHP Notice: Undefined offset: 3 in /home1/shamaror/includesem/lgrquote.php on line 58
PHP Notice: Undefined offset: 2 in /home1/shamaror/includesem/lgrquote.php on line 58
PHP Notice: Undefined offset: 1 in /home1/shamaror/includesem/lgrquote.php on line 58
PHP Notice: Undefined offset: 3 in /home1/shamaror/includesem/lgrquote.php on line 58
PHP Notice: Undefined offset: 2 in /home1/shamaror/includesem/lgrquote.php on line 58
PHP Notice: Undefined offset: 1 in /home1/shamaror/includesem/lgrquote.php on line 58
[/CODE]

I don’t know too much about PHP, especially this type of problem. Any suggestions?

[CODE]$quotetitle=””;
$quotefile=”quotes.txt”;

$dir=$_SERVER[‘DOCUMENT_ROOT’];

//No need to change anything below here.
if (file_exists($dir.”/”.$quotefile)) {
$quote=file($dir.”/”.$quotefile);
$rand_key=array_rand($quote, 1);
list($qtext, $qauthor, $qlink, $qlinktext) = explode(“|”, trim($quote[$rand_key]));

if (isset($qtext) && $qtext!=””) { echo $qtext; }

}

?>
[/CODE]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 27.2014 — My guess is that you are retrieving empty lines from the file. Maybe try this:
[code=php]
if (file_exists($dir."/".$quotefile)) {
$quote=file($dir."/".$quotefile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // added 2 flags
$rand_key=array_rand($quote, 1);
[/code]

Though it would still probably be a good idea to put some defensive code in after that to make sure that any given random line you then grab actually explode()'s into the required number of fields before you try to access them.
Copy linkTweet thisAlerts:
@LeeUauthorOct 27.2014 — Thanks I tried your code but it keeps throwing the errors, this time it looks like it's on the following line:

[CODE]list($qtext, $qauthor, $qlink, $qlinktext) = explode("|", trim($quote[$rand_key]));[/CODE]

As for your other suggestion. I don't really have the faintest idea how to go about doing it.
Copy linkTweet thisAlerts:
@NogDogOct 27.2014 — So it's likely the lines you are reading in from the file (or at least some of them) do not have 4 pipe-delimited fields in them (since you are explode()-ing on "|"). If that is possible, then you need to decide how to handle it: output an error, terminate this part of the processing and just don't output it, throw an exception, whatever. You might do something like:
[code=php]
if (file_exists($dir."/".$quotefile)) {
$quote=file($dir."/".$quotefile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // added 2 flags
$rand_key=array_rand($quote, 1);
$data = explode("|", trim($quote[$rand_key]));
if(count($data) < 4) {
list($qtext, $qauthor, $qlink, $qlinktext) = $data;
}
else {
// here's where you handle the error condition
}
}
[/code]
Copy linkTweet thisAlerts:
@LeeUauthorOct 27.2014 — Thanks NogDog for the help. However, I went back took another look at how to set-up the quotes.txt file and it was a problem with the layout, not the code. Where I had only one line for the quote and author, it is looking for foour separate fields:

[CODE]Quote|Quote Author|Link|Link Text[/CODE]

It was your comment So it's likely the lines you are reading in from the file (or at least some of them) do not have 4 pipe-delimited fields in them (since you are explode()-ing on "|").[/QUOTE]

Now I don't have any errors ("duh!"). Thanks for the quick help, I appreciate it. I really need to get back to this forumk. I miss it.
Copy linkTweet thisAlerts:
@LeeUauthorOct 27.2014 — Ooops. I spoke too soon. In changing the layout, I had two empty pips at the end. The script says that "If you leave one section blank it will not display that line." So I am using

[CODE]Quote|Quote Author||[/CODE]

but now the Quote Author does not display. Any ideas?
Copy linkTweet thisAlerts:
@NogDogOct 27.2014 — Nope, might be additional logic elsewhere in the code?
Copy linkTweet thisAlerts:
@NogDogOct 27.2014 — PS: Yet another reason I prefer databases over text files. ?
Copy linkTweet thisAlerts:
@LeeUauthorOct 27.2014 — Thanks anyways. I'll msark this thread solved since it did soolve the first part.
×

Success!

Help @LeeU spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...