TonalDev
TonalDev

Reputation: 583

Draw stats from php file

I have a jQuery player with a stats bar on top that draw information from a remote xml file.

I'm trying to modify it to read stats from a php file.

That php file draw the stats from a xspf file in the remote server.

How do i go about doing that with the following code:

Updated Code:

The php file:

<?php
header('Content-Type:text/xml');

$xml = simplexml_load_file("http://**.**.**.**:8000/live.xspf"); 

echo '<?xml version="1.0" encoding="UTF-8"?><tracks>';
foreach ($xml->trackList->track as $data) {
    $radio = $data->location;
    $song  = $data->title;  
    $info = $data->listeners;
    echo '<track>
            <title>'.$song.'</title>
            <listeners>'.$info.'</listeners>
            <location>'.$radio.'</location>
          </track>';
}
echo '</tracks>';
?>

The jQuery code:

    function getCurrentTrack(){
$.get('/player/readerPlayer.php'), function (){
    $track = $(data).find('track');
    $track.each(function (){
        $this = $(this);
        var title = $this.find('title').text();
        var listenersn = $this.find('listeners').text();
        $('.listeners span').html(listenersn);
        setTimeout('getCurrentTrack()', 65000);
    });
}, 'xml'};

Upvotes: 0

Views: 212

Answers (1)

Alex
Alex

Reputation: 219

You simply need to output XML from your PHP file.

header('Content-Type:text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?><tracks>';
foreach ($xml->trackList->track as $data) {
    $radio = $data->location;
    $song  = $data->title;  
    $info = $data->listeners;
    echo '<track>
            <title>'.$song.'</title>
            <listeners>'.$info.'</listeners>
            <location>'.$radio.'</location>
          </track>';
}
echo '</tracks>';

You'll want to adjust the js a little bit to get the track objects first though.

function getCurrentTrack(){
    $.get('/player/readerPlayer.php'), function (){
        $track = $(data).find('track');
        $track.each(function (){
            $this = $(this);
            var title = $this.find('title').text();
            var listenersn = $this.find('listeners').text();
            $('.listeners span').html(listenersn);
            setTimeout('getCurrentTrack()', 65000);
        });
    }, 'xml');
}

Upvotes: 2

Related Questions