1

I want to transform this PHP function.. that should return JSON data.

<?php
$query = 'SELECT * FROM `' . mix_player::table() . '` a';
if (isset($_GET['cat']) || isset($_GET['order'])) 
    if (isset($_GET['cat'])) {
        $query .= ' INNER JOIN `' . mix_player::table_cat_rel() . '` b '
            . "ON (a.`id` = b.`idtrack`) WHERE `idcat` = '" . $wpdb->escape($_GET['cat']) . "'";

        $random = $wpdb->get_var('SELECT `random`, `order` FROM `' . mix_player::table_categories() . "` WHERE `id` = '"
            . $wpdb->escape($_GET['cat']) . "'");

        if (!$random)
            $order = $wpdb->get_var(NULL, 1);
    }

    if (isset($_GET['order']))
        $order = $_GET['order'];

    if ($order != '') {
        if (isset($_GET['cat']))
            $query .= ' AND ';
        else
            $query .= ' WHERE ';

        $tracks = mix_player::order_list($query, $order);
    }

} else {
    $random = '0';
}

$query .= ' ORDER BY `id` ASC';

if (isset($tracks) || ($tracks = $wpdb->get_results($query, ARRAY_A))) {
    // option "shuffle = true" not always working into mix. Do it our own way...
    if ($random == 1) { // shuffle tracks?
        list($usec, $sec) = explode(' ', microtime());
        mt_srand((float) $sec + ((float) $usec * 100000));
        $nrows = count($tracks);
        for ($i = 0; $i < $nrows; $i++) {
            $j = mt_rand(0, $nrows - 1);  // pick j at random
            $row = $tracks[$i]; // swap i, j
            $tracks[$i] = $tracks[$j];
            $tracks[$j] = $row;
        }
    }

    foreach ($tracks as $row) {


        $artist = (mix_player::entities($row['artist']));
        echo ($artist);


        $title = (mix_player::entities($row['title']));

        echo ($title);

        $url =(xspf_player::entities($row['url']));
        echo ($url);        

}
}
?>

to display like this json file :

{"title":"title", "artist":"artist","media":"url media.mp3","color":"#56B0E8" },

Can you help me?

Thanks in advance.

1
  • 2
    just fill an array and the json_encode it Commented Feb 22, 2016 at 11:32

2 Answers 2

1

You can simply create an array and populate it with your desired values, then return it as JSON:

function tracks2json( $tracks )
{
    $retval = array();
    foreach( $tracks as $row )
    {
        $array = array();
        $array['artist'] = mix_player::entities($row['artist']);
        $array['title']  = mix_player::entities($row['title']);
        $array['media']  = 'url '.xspf_player::entities($row['url']);
        $array['color']  = '#56B0E8';
        $retval[]        = $array;
    }
    return json_encode( $retval );
}

if( isset($tracks) || ($tracks = $wpdb->get_results($query, ARRAY_A)) )
{
    // Your MySQL routine here

    $json = tracks2json( $tracks );

}
Sign up to request clarification or add additional context in comments.

3 Comments

Works well thank you very much But there is a problem ,it Show 3 SLASHES .. when i use-------- echo json_encode($json); ............ ,\"media\":\"url http:\\\/\\\/
@Paradiva if you have copied exactly my code, returning value is already json encoded!
Is it possible to assist in modifying myplaylist.php file to become a JSON file , such as found in jsonplaylist but dynamic --- URL mediafire.com/download/jh14xe6kr4t9he9/playlist_file.rar –
0
echo json_encode(array("title"=>$title,"artist"=>$artist,"url"=>$url));

1 Comment

thank you .. but there is a probleme of SLASHES (( http:\/\/ ... {\"title\":"" ))

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.