0

I try to understand how it work. At the beginning, I was using inside my html code a php array with db and after that I was extracting my array inside my playlist.

Here the example:

    <?php
    $fileinfo=array();
    $count=0;

    //SQL Query
    $query = "select track, artiste, album, emplacement, duration, poster from tempo where genre like '%%' ORDER BY no_track";

    $con=mysqli_connect("localhost","user","password","db_table");

    // Check connection
    if (mysqli_connect_errno($con))
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $resultat = mysqli_query($con,$query);

    while($row = mysqli_fetch_array($resultat))
    {
       $row['emplacement'] = str_replace("../", "../../", $row['emplacement']);
       $row['poster'] = str_replace("../", "../../", $row['poster']);
       $row['duration'] = str_replace("00:", "", $row['duration']);
       $info = '{artist:"'.$row['artiste'].'", title:"'.$row['track'].'",    album:"'.$row['album'].'", mp3:"'.$row['emplacement'].'", cover:"'.$row['poster'].'", duration:"'.$row['duration'].'"}';
       array_push($fileinfo, $info);
    }

    mysqli_close($con);
    ?>

    ...

    $('#player').ttwMusicPlayer(
    [
       <?php

       //for each file in directory
       $arrlength=count($fileinfo);
       for($x=0;$x<$arrlength;$x++)
       {
          if ($x < ($arrlength - 1))
          {
             echo $fileinfo[$x].",\n\t\t";
          }else
          {
             echo $fileinfo[$x]."\n\t\t";
          }
       }
       //the result look like this:
       //{artist:"Boy Sets Fire", title:"After the Eulogy", album:"After The Eulogy",
          mp3:"../../music/Punk/Hardcore_Punk/Boy_Sets_Fire_-_After_the_Eulogy-2000-
          JAH/01_After_the_Eulogy-JAH.mp3", 
          cover:"../../music/Punk/Hardcore_Punk/Boy_Sets_Fire_-_After_the_Eulogy-2000-
          JAH/Folder.jpg", duration:"03:31"},

       ?>
    ],

To use everything more dynamically, I try to use JSON with PHP inside my javascript

And my code look like this:

    var sourceplayer =
    {
        datatype: "json",
        datafields: [
          { name: 'artiste' },
          { name: 'title' },
          { name: 'album' },
          { name: 'mp3' },
          { name: 'cover' },
          { name: 'duration' }
        ],
        url: 'player.php'

    };

    $('#player').ttwMusicPlayer(
    [

    ],

So afert url: 'player.php', I don't know how to work with result. It's an array of data like this: "Rows":[{"no_track":"1","track":"Grandma Dynamite","artiste":"24-7 Spyz","album":"Harder Than You","genre":"Alternative","year":"1989","duration":"00:03:44"}

And I want to use it inside the bracket of $('#player').ttwMusicPlayer(

Please give me a cue or an simple example to help me with this. I'm not using pure jplayer but a similar one.

Thanks in advance

Regards,

Eric

1
  • Have you heard of json_encode? Commented Nov 27, 2013 at 0:49

1 Answer 1

0

PHP json_encode - https://www.php.net/json_encode

<?php
    $fileinfo=array();
    $count=0;

    //SQL Query
    $query = "select track, artiste, album, emplacement, duration, poster from tempo where genre like '%%' ORDER BY no_track";

    $con=mysqli_connect("localhost","user","password","db_table");

    // Check connection
    if (mysqli_connect_errno($con))
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $resultat = mysqli_query($con,$query);

    while($row = mysqli_fetch_array($resultat))
    {
       $fileinfo[] = array(
           'mp3' => str_replace("../", "../../", $row['emplacement']),
           'cover' => str_replace("../", "../../", $row['poster']),
           'duration' => str_replace("00:", "", $row['duration']),
           'artist' => $row['artiste'],
           'title' => $row['track'],
           'album' => $row['album']
       );
    }

    mysqli_close($con);
    ?>

    ...

    $('#player').ttwMusicPlayer(<?php echo json_encode($fileinfo); ?>);
2
  • Thanks for the answer, it was almost the of what I did at the beginning. What I want it's to call external php for futur utilization like with parameters. My php file "player.php" could receive no parameter or with a parameter like artist, album, song, etc. That's the reason why I would like to use json. Any idea ? Thanks Commented Nov 27, 2013 at 2:44
  • You could pass parameters with a $_GET and/or $_POST request with or without AJAX. It really depends on the user experience you're looking for along with the flow of your application. Don't forget to sanitize any parameters you'll pass to your MySQL query. Commented Nov 27, 2013 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.