I need to create this list (in javascript):
var videoList = ['GACS1.mp4','GACS4.mp4'];
From a call to the DB. So within the DB I have a field that I am calling to to get those mp4's back.
Here is the function.php, it is really just an Ajax page that processes the request.
function assignedvideos()
{
global $db_name, $path;
$uid = $_GET['uid'];
$selectedvideos = ORM::for_table('movies1')->raw_query('SELECT B.ID AS bid, B.*, LX.* FROM movies1 LX JOIN movies B ON LX.movieid = B.id JOIN locations L ON LX.locationid = L.id JOIN tablets T ON LX.locationid = T.tabletlocation WHERE T.uniqueid =".uid." ORDER BY LX.order ASC')->find_many();
if($selectedvideos)
{
foreach ($selectedvideos as $row)
{
$row['movielocation'] = stripslashes($row['movielocation']);
$row['moviename'] = stripslashes($row['moviename']);
}
}
die();
}
And ofcourse here is the call to the ajax page to retrieve the function.php page to retrieve the values.
$something = $_GET['uid'];
jQuery.ajax({
url: 'function.php',
type: "POST",
data: 'action=assignedvideos&uid='+$something,
success: function (response) {
},
error: function (response) {
}
});
The question is does this get built in the AJAX and sent back as a string and if so how?
Or does the AJAX return the list of variables and I build the list within the success response of the AJAX call? If so, then how?
To make it easy on yourself, put whatever values you want to send back to the client into a variable, and then call
echo json_encode($somevar);
in your case, there is an additional problem,
foreach ($selectedvideos as $row)
{
$row['movielocation'] = stripslashes($row['movielocation']);
$row['moviename'] = stripslashes($row['moviename']);
}
$row
is replaced on each iteration of the loop with new data from the next row in $slectedvideos
, so your operation basically does nothing, after the foreach $row
will contain the last row of data from $selectedvideos
. (also, $row
will not modify $selectedvideos
). if you wanted to modify $selectedvideos
, there's a number of ways to do that, but without changing your code too much, you could do
foreach ($selectedvideos as $key => $row)
{
$selectedvideos[$key]['movielocation'] = stripslashes($row['movielocation']);
$selectedvideos[$key]['moviename'] = stripslashes($row['moviename']);
}
and then
echo json_encode($selectedvideos);
which you can then convert to a javascript object in the success call using JSON.parse()
Note that in this case, the javascript object won't look exactly how you want your data to look, instead it will be an array of objects such that array[0].movielocation
will be (if I'm not mistaken) the file location of the first movie etc. If you want it to be exactly an array of file paths, the foreach loop could look like this:
$output = array();
foreach ($selectedvideos as $row)
{
$output[] = stripslashes($row['movielocation']);
}
echo json_encode($output);
Note also that using stripslashes on the file location will force you to either put all the videos in the same folder on the server or use mod_rewrite in some shape or form.