.oO(megs1328)
>I am trying to create a dynamic comma delimited list to
insert into a
>javascript music player. I've included the script below,
as well as my attempt.
>My problem is, it finds the sound files fine, but then
the track and artists
>names don't show. You can see it here:
>
http://www.every-scene.com/artists2.php?recordID=1.
In the source code, it just
>shows a comma for the track and artists names.
Your code tries to run three times through the same record
set from the
DB. This is not going to work (at least not the way you tried
it) and
actually not really necessary at all.
> My second problem here is that when I concatenated the
comma, one shows up at
>the end, which adds another "phantom" item. Is there a
way to remove the last
>comma, or only show it only on the first items?
Collect all data in arrays first, before you print it. Then
you can
easily use PHP's implode() function to print a
comma-separated list.
> Can anyone help me piece all these issues together, I
can see to find one
>solid answer to get it right. Thank you!
>
> // The music player
> <script type="text/javascript">
> AudioPlayer.embed("audioplayer_1", {
> soundFile: "track1, track2",
> titles: "title1, title2",
> artists: "artist1, artist2",
> autostart: "yes",
> loop: "yes"
> });
>
> // My attempt
> <script type="text/javascript">
> AudioPlayer.embed("audioplayer_1", {
> soundFile: "<?php do { ?>/includes/audio/<?php
echo
>$row_music['trackdirectory'] . ','; ?><?php } while
($row_music =
>mysql_fetch_assoc($music)); ?>",
> titles: "<?php do { ?><?php echo
$row_music['trackname'] .
>','; ?><?php } while ($row_music =
mysql_fetch_assoc($music)); ?>",
> artists: "<?php do { ?><?php echo
$row_music['artistname'] .
>','; ?><?php } while ($row_music =
mysql_fetch_assoc($music)); ?>",
> autostart: "yes",
> loop: "yes"
> });
> </script>
Such spaghetti code is hard to read, hard to maintain and
almost
impossible to debug. Try something like the following to get
the data
first, before you print it out - step by step:
<?php
// query the DB, you have to adjust this part!
$music = mysql_query('...');
// initialize data structures to keep results from the DB
$path = '/includes/audio/';
$data = array(
'soundFile' => array(),
'titles' => array(),
'artists' => array()
);
// loop through DB results and store all data in our array
while ($row_music = mysql_fetch_assoc($music)) {
$data['soundFile'][] = $path.$row_music['trackdirectory'];
$data['titles'][] = $row_music['trackname'];
$data['artists'][] = $row_music['artistname'];
}
?>
Now you should have all the data, so you can easily print it
out as
JavaScript for the audio player, e.g.
<script type="text/javascript">
AudioPlayer.embed("audioplayer_1", {
soundFile: "<?php print implode(', ',
$data['soundFile']);?>",
titles: "<?php print implode(', ',
$data['titles']);?>",
artists: "<?php print implode(', ',
$data['artists']);?>",
autostart: "yes",
loop: "yes"
});
</script>
Or try this, which should give the same result:
<script type="text/javascript">
AudioPlayer.embed("audioplayer_1", {
<?php
foreach ($data as $type => $items) {
printf(" %s: '%s',\n", $type, implode(', ', $items));
}
?>
autostart: "yes",
loop: "yes"
});
</script>
Be aware that
* this code is completely untested - all I had was syntax
highlighting
and a PHP parser to find and fix syntax errors, but this
doesn't mean
that it will work as expected
* this snippet is incompatible with DW's behaviors, you at
least have to
adjust the part where the DB is queried
But hopefully it gives you an idea of how such things can be
done.
Micha