Echo php results in javascript code

I am trying to replicate some result in php in javascript but it doesn't work, obfuscating the javascript player interface

that's the complete javascript:

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){

    new jPlayerPlaylist({


        jPlayer: "#jquery_jplayer_1",
            cssSelectorAncestor: "#jp_container_1"
        }, [
            {
                title:"Name",
                mp3:"audio.mp3",
            },      

        ], {
            swfPath: "js",
            supplied: "oga, mp3",
            wmode: "window"
        });
    });
    //]]>
    </script>

      

I want to replace this:

            {
                title:"Name",
                mp3:"audio.mp3",
            },      

      

with this:

    while(
    $row = mysql_fetch_assoc($result)) { 
    $sender = $row['sender'];
    $sender_name_query = mysql_query("SELECT fullname FROM users WHERE id = '$sender'");
    $sender_name = mysql_fetch_object($sender_name_query);
    $sender_fullname = $sender_name->fullname;
    echo '{<br/>title:"' . $sender_fullname . '",<br/>mp3:"link",<br/>},';  
}   

      

I need a while loop to get all the results

can anyone help on how to replace it? thank

+3


source to share


4 answers


one more solution. You can do:



<?php
    $playlist = array();

    while($row = mysql_fetch_assoc($result)) { 
        $sender = $row['sender'];
        $sender_name_query = mysql_query("SELECT fullname FROM users WHERE id = '$sender'");
        $sender_name = mysql_fetch_object($sender_name_query);
        $sender_fullname = $sender_name->fullname;
        $playlist[] = (object) array(
            'title' => $sender_fullname,
            'mp3' => 'audio.mp3'
        );  
    }   
?>

<script type="text/javascript">
    //<![CDATA[
    $(document).ready(function(){
        new jPlayerPlaylist({
            jPlayer: "#jquery_jplayer_1",
            cssSelectorAncestor: "#jp_container_1"
        }, 
        <?php echo(json_encode($playlist));?>,
        {
            swfPath: "js",
            supplied: "oga, mp3",
            wmode: "window"
        });
    });
    //]]>
</script>

      

+2


source


<br/>

not valid in javascript. Try:



echo '{\ntitle:"' . $sender_fullname . '",\nmp3:"link",\n},';  

      

+2


source


Don't use tags <br>

, use \n

instead to add new lines (if you really need to, the script will run without line breaks).

You cannot use HTML tags in javascript

+1


source


{
    title: "<?php echo json_encode($sender_fullname);?>",
    mp3: "audio.mp3",
},

      

+1


source







All Articles