I've got a loop nested in a loop. All of the data displays fine until I get to the second loop and the returned data keeps repeated. It gets to the point where songs of the items from the second loop gets repeated over 15 times.
item -subitems
item -subitems -subitems
item -subitems -subitems -subitems
item -subitems -subitems -subitems -subitems
I was told to clear my loop but I'm sure how to do that. Any ideas? here's a live example if need: view-source:http://mixtapemonkey.com/json
<?php
include ("connection.php");
foreach($db->query("SELECT * FROM mixtapes WHERE year='2014' OR year = '2015' ORDER BY id DESC LIMIT 24") as $info) {
echo "mixtape-".$info['id']," {
";
echo "\t\"id\" : \"".$info['id']."\",
";
echo "\t\"title\" : \"".$info['title']."\",
";
echo "\t\"name\" : \"".$info['name']."\",
";
echo "\t\"genre\" : \"".$info['genre']."\",
";
echo "\t\"year\" : \"".$info['year']."\",
";
echo "\t\"downloads\" : \"".$info['downloads']." Downloads\",
";
echo "\t\"thumb\" : \"http://mixtapemonkey.com/".$info['thumb']."\",
";
echo "\t\"songs\" : {
";
if ($handle = opendir("mixtapes/zip/".$info['id'])) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "mixtapes/zip/".$info['id'] && $entry != "mixtapes/zip/".$info['id']) {
if ($newhandle = opendir("mixtapes/zip/".$info['id']."/".$entry)) {
while (false !== ($newentry = readdir($newhandle))) {
if ($newentry != "mixtapes/zip/".$info['id']."/".$entry && $newentry != "mixtapes/zip/".$info['id']."/".$entry) {
$ext = substr($newentry, strrpos($newentry, '.') + 1);
if ($ext == "mp3" || $ext == "m4a")
{
$item = "mixtapes/zip/".$info['id']."/".$entry."/";
$musicarray[] = $newentry;
sort($musicarray);
}
}
}
closedir($newhandle);
}
}
}
closedir($handle);
}
foreach ($musicarray as $song) {
echo "\t\t\"track\" : \"http://mixtapemonkey.com/$item$song\",
";
}
echo "\t}
";
echo "},
";
}?>
I have a same problem also and I solved it to used php break after loop. So you can also try it to.
break;