Jump to content


Photo

Undefined offset: 2 (Problem With Function)


  • Please log in to reply
1 reply to this topic

#1 theweirdone

theweirdone
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 30 June 2006 - 09:07 AM

So I have another problem.
What I'm pretty much trying to do is to make a program that displays text. So I've made a function to do this, because it extracts the text from the file "songs.inc". The songs are formatted in the file in this way: "Song Title#", (using the # to split the songs up). Here's the function that's meant to display the songs:
function display_songs($file_path) {
	$all_songs = fopen($file_path, "r");
	
	$songs = explode("#", $all_songs);
	
	$songs = str_replace("#", "<br />", $songs);
	
	$song_count = count($songs);
	
	for ($i = 0; $i <= $song_count; $i += 1) {
		echo "$songs[$i] In My Pants";
	}

}
When I run the code though, all it displays is

Resource id 4
Notice: Undefined offset: 2 in c:\program files\easyphp1-8\www\in my pants\includes\functions.inc.php on line 41


Any Ideas?
¯|¯µ€W€¦®ÐØÑ€

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 June 2006 - 10:03 AM

$song_count = count($songs);

for ($i = 0; $i <= $song_count; $i += 1) {


if you 20 songs, $i needs to go from 0 to 19. When you reach 20 in your code you get the undefined offset.

Try
 
function display_songs($file_path) {
	$all_songs = file_get_contents($file_path);
	
	$songs = explode("#", $all_songs);
		
	foreach ($songs as $song) {
		echo "$song In My Pants<br>";
	}
}
 
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users