Philipp Braun
Philipp Braun

Reputation: 1573

Inserting a javascript code into php

I am searching for possibility to include a javascript function into a php code.

The code should get the results from the search php file and then print them out in form of a Javascript Playlist.

This is the javascript code:

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

var description = ''; 
var myPlaylist = [ {

    mp3:'./../sounds/mysql-upload',
    title:'mysql-title',
    artist:'mysql-artist',
    subcategory:'mysql-subcategory',
    date:'mysql-date',
    rating:'mysql-rating',
    },

    /* var myPlaylist has to repeat */

]; 

$('#main').ttwMusicPlayer(myPlaylist, { 
autoPlay:false, 
description:description, }
);

}); 
</script>

And here is the php code:

 <?php
    if (!empty($_POST['search'])) {

    /* Connect to database */
    $hostname = '';
    $database = '';
    $username = '';
    $password = '';
    if (!($mysql_link = mysql_connect($hostname, $username, $password))) {
    die('Could not connect');
    }

    /* Select databse */
    if (!($db_selected = mysql_select_db($database, $mysql_link))) {
        die('Could not find database');
    }

    /* Send mysql command */
    $sql_cmd = "SELECT * FROM sounds WHERE `keywords` LIKE '"
    . $_POST['search']."%'";
    if (!($res = mysql_query($sql_cmd))) {
        die('Invalid MySQL query');
    }

    /* Show results */
    while ($dsatz = mysql_fetch_assoc($res)) {

    $upload = $dsatz["upload"];
    $title = $dsatz["title"];
    $artist = $dsatz["artist"];
    $subcategory = $dsatz["subcategory"];
    $date = $dsatz["date"];
    $rating = $dsatz["rating"];

    /* Here should be the Javascript code */

    }

    /* Close database connection */
    mysql_close($mysql_link);
    }
?>

Please note that I don't want to include the full Javascript function in the results part of the php code but the playlist variable which should repeat.

Upvotes: 0

Views: 196

Answers (3)

yuvrajm
yuvrajm

Reputation: 318

this is fairly possible, but how abt using json instead. JSON encoded string can be generated in php via json_encode($array) You can then, possibly using AJAX, load the json into the current page.

The other option is, generate javascript arrays from the php code, and then include the javascript array on to the web page.

Upvotes: 0

DanRedux
DanRedux

Reputation: 9359

In PHP, you can make an array, and fill it with arrays of those attributes. Something like:

$results = array();
while ($dsatz = mysql_fetch_assoc($res)) {
    $results[]=$dsatz; }
$printout = json_encode($results);

Now to put it into the JavaScript, you'd do this:

var myPlaylist = <?php echo $printout; ?>;

Upvotes: 2

BudwiseЯ
BudwiseЯ

Reputation: 1826

If I understood you correctly, you'll need AJAX for doing that. Here's some places to start with:

W3Schools AJAX Tutorial
Tizag's AJAX Tutorial

Also, as you're already using JQuery, it can make your life with AJAX much easier.

Take a glance to JQuery AJAX API at http://api.jquery.com/category/ajax/

Upvotes: 1

Related Questions