使用JSON检索PHP数组,并使用javascript中的数组来填充播放列表


Retrieve PHP Array with JSON and use the array in javascript to populate a playlist

我试图了解它是如何工作的。一开始,我在我的html代码中使用了一个带有db的php数组,之后我在播放列表中提取了我的数组。

这里的例子:

    <?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"},
       ?>
    ],

为了更动态地使用一切,我尝试在javascript 中使用JSON和PHP

我的代码是这样的:

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

所以在一个新的网址:"player.php"之后,我不知道如何处理结果。这是一个数据数组,如下所示:"行":[{"no_track":"1","track":"奶奶炸药","艺人":"24-7间谍","专辑":"比你更难","流派":"另类","年份":"1989","持续时间":"00:03:44"}

我想在$('#player')的括号内使用。ttwMusicPlayer(

请给我一个提示或一个简单的例子来帮助我。我使用的不是纯jplayer,而是类似的。

提前感谢

问候,

Eric

PHP json_encode-http://us2.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); ?>);