Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Create dynamic comma delimited list from table rows using PHP

New Here ,
Feb 05, 2009 Feb 05, 2009
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.

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?

The database for this is currently set up on two tables, one for the song and directory, and one for the artist. The MySQL expression is: "SELECT music.trackdirectory, artists.artistname, music.trackname FROM music, artists WHERE music.artistID = artists.artistID". There are currently two songs: If I Had Eyes by Jack Johnson and Alone by Jack Johnson.

I have tried so many different things that I have found through searches, but nothing seems to solve all my issues. I believe the track and artist names aren't showing up becuuse you can only have one repeat region for each recordset on each page. I tried separating out the three into separate recordsets, but then the artist name doesn't coincide with the songs.

Can anyone help me piece all these issues together, I can see to find one solid answer to get it right. Thank you!
TOPICS
Server side applications
924
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 05, 2009 Feb 05, 2009
.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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 05, 2009 Feb 05, 2009
That worked perfect! Thank you!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 06, 2009 Feb 06, 2009
LATEST
.oO(megs1328)

>That worked perfect! Thank you!

Nice. You're welcome.

Micha
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines