theweirdone Posted June 30, 2006 Share Posted June 30, 2006 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:[code]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"; }}[/code]When I run the code though, all it displays is [quote]Resource id 4Notice: Undefined offset: 2 in c:\program files\easyphp1-8\www\in my pants\includes\functions.inc.php on line 41[/quote]Any Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/13279-undefined-offset-2-problem-with-function/ Share on other sites More sharing options...
Barand Posted June 30, 2006 Share Posted June 30, 2006 [quote]$song_count = count($songs); for ($i = 0; $i <= $song_count; $i += 1) {[/quote]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[hr][code=php:0]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>"; }}[/code][hr] Quote Link to comment https://forums.phpfreaks.com/topic/13279-undefined-offset-2-problem-with-function/#findComment-51135 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.