Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
I'm trying to display route data on a map using leaflet. I'm pulling the data from a MYSQL database with a php script. which is working. The data is stored in an array called vechiclelatlong. I use under the Javascript tag where is have my Leaflet script. I am getting the following error:
Uncaught ReferenceError: vehiclelatlong is not defined
See scripts below:
PHP script:
<php
function vData(){
include("dbconnecti.php");
$strtDate = strtotime('20150404');
$endDate = strtotime('20150405');
$query="SELECT ED.latitude, ED.longitude, ED.timestamp, ED.creationTime
FROM EventData ED
WHERE ED.latitude <>0 AND ED.longitude <>0 AND (ED.timestamp >= $strtDate) AND (ED.timestamp <= $endDate)";
$results = $dbconnect->query($query);if(!$results){
echo mysql_error();
die('There was an error excuting query statment: ');//. mysqli_error());}
$num_of_rows = $results -> num_rows;
$data = array();
echo "var vehiclelatlong = [";for($x =0; $x < $num_of_rows; $x++){
$data[]= $results -> fetch_assoc();
echo "[".$data[$x]['latitude'].",".$data[$x]['longitude']."]";if($x <= $num_of_rows -2){
echo ",";}}
echo "];";}
vData();?>
While your PHP looks to be creating an array, I'm not sure that you're going to have consistent luck getting JS to read it that way (an arrant space here or there, etc.). At least I've tried that in the past, only to realize that while it LOOKS like a good array, it isn't.
I think you're better off just returning your MySQL as a true array and then using json_encode($array) to get it in something that will consistently be read by JS.
EDIT: Lets stick with your array (although JSON ~ GEOJSON and there are good PHP GEO tools out there). I got it to work doing the following. 1. made sure both files had a *.php extension 2. removed **" on either side of my include statement in my main page 3. made sure my included file was correctly referenced in my main page 4. made sure my DB query was returning a valid result. I made some changes (including just trying it using SQLite) but you can see the same result.
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that
you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice
sessions to improve your knowledge and scores.
manpreet
Best Answer
2 years ago
I'm trying to display route data on a map using leaflet. I'm pulling the data from a MYSQL database with a php script. which is working. The data is stored in an array called vechiclelatlong. I use
under the Javascript tag where is have my Leaflet script. I am getting the following error:
See scripts below:
PHP script:
HTML script