0

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.

3
  • Watch out for SQL-injections. Your "SELECT * FROM sounds WHERE 'keywords' LIKE '". $_POST['search']."%'" is open to it. Have a look at mysql_real_escape_string(). Commented Mar 11, 2012 at 10:22
  • Because it's a loop the variable will get a different value each time , do you mean that you want to summon the js function? Commented Mar 11, 2012 at 10:22
  • No I just need the js function to inlude the results of the database in a music player. Commented Mar 11, 2012 at 10:43

3 Answers 3

2

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; ?>;
Sign up to request clarification or add additional context in comments.

4 Comments

But this solution doesn't work for more than one result. Is ajax really the only possibility?
It does. If you noticed, $results is an array of arrays, where each array inside it is a "row" of the DB. When I convert it to JSON, it gets converted into the same thing- an array of arrays identical to the one from the PHP. Try it out and see.
You don't. json_encodes turns the $results array of arrays into a string, which JavaScript can read and turn into an array of arrays.
Can you please give me an example how this should look like? Because I am not quite sure how I would the results array.
1

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/

Comments

0

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.

Comments

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.