Jump to content

Recommended Posts

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 4
Notice: Undefined offset: 2 in c:\program files\easyphp1-8\www\in my pants\includes\functions.inc.php on line 41[/quote]

Any Ideas?
Link to comment
https://forums.phpfreaks.com/topic/13279-undefined-offset-2-problem-with-function/
Share on other sites

[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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.