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.
In PHP, you can make an array, and fill it with arrays of those attributes.
Something like:
Now to put it into the JavaScript, you’d do this: