0

Here is the actual file which i want to seperate the javascript from. How do i tell the javascript where to look for the php file, at the moment i am only able to execute the javascript from within the php file..

<?php
date_default_timezone_set('Europe/London');
require_once("DbConnect.php");

$sql = "SELECT `artist`, `title`, `label`, `albumyear`, `date_played`, `duration`,
`picture` FROM historylist  ORDER BY `date_played` DESC LIMIT 5 ";

$result = $db->query($sql);

$lastplayed = array();
$i = 1;
while ($row=$result->fetch_object()) {
$lastplayed[$i]['artist'] = $row->artist;
$lastplayed[$i]['title'] = $row->title;
$lastplayed[$i]['label'] = $row->label;
$lastplayed[$i]['albumyear'] = $row->albumyear;
$lastplayed[$i]['date_played'] = $row->date_played;
$lastplayed[$i]['duration'] = $row->duration;
$lastplayed[$i]['picture'] = $row->picture;
$i++;
}

$starttime = strtotime($lastplayed[1]['date_played']);
$curtime = time();
$timeleft = $starttime+round($lastplayed[1]['duration']/1000)-$curtime;
$secsremain = (round($lastplayed[1]['duration'] / 1000)-($curtime-$starttime));


$lastplayedjson = json_encode( $lastplayed );
?>

i want this part to be in a separate file

<script type="text/javascript">


var lastplayedjson = <?php echo $lastplayedjson ?>;


alert( lastplayedjson[1].title );
alert( lastplayedjson[2].title );
alert( lastplayedjson[3].title );
alert( lastplayedjson[4].title );
</script>
Justin
  • 135
  • 2
  • 12

2 Answers2

0

You could include the first php file (we'll call it fileA.php) which would allow fileB.php to access all of it's variables

File A

<?php 
$cats = 'topkek';

File B

<?php
include('/path/to/fileA.php');
echo $cats;

Should output

topkek

You can see here for the differences on include, require, include_once and require_once

Community
  • 1
  • 1
Jamie Taylor
  • 4,709
  • 5
  • 44
  • 66
0

in your php:

include_once('templates/template-name.php');
wpdaniel
  • 714
  • 8
  • 25