error on php/javascript script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

error on php/javascript script

Post by garry27 »

i'm tring to create a javascript data structure which contains data pulled out of a database via php. the data is reformated and i'm attempting to echo the data structure back out to the browser. the problem is i keep getting an error stating: "undefined variable: markers in /.../map_data.php on line 34 var markers "

please somone take a look and advice me on how i can proceed as i've hit a brick wall. thank you.

Code: Select all

<?php
  function getPubMarkers() {

    db_connect();
    $sql = mysql_query ( " select Longitude, Latitude, pubName, pubStreetNo, pubStreet, pubCity, pubCounty, 
                         pubPostcode, pubPhone   from Pubs  " ) or die( 'Invalid query: ' . mysql_error() ); 
      
    while( $elements = mysql_fetch_array($sql) ) {        
     $markers[] =      '{'  
                             ." 'lng': " .$elements[0] .','
	                         ." 'lat': " .$elements[1] .','
	                         ." 'name': " .$elements[2] .','
	                         ." 'address': " .$elements[3]  .'<br>'  .$elements[4] .'<br>' .$elements[5]    
	                           .'<br>'  .$elements[6] .'<br>' .$$elements[7]  .','
	                         ." 'phone': " .$element[8]
				      .'},';
    }
	
     //convert array into a single string variable
     $markers  = implode( ' ', $markers );
     //remove the end comma in the string				
     $markers = substr_replace($markers,"",-1);				
     
  }
 
 
 
  echo 'var markers
        markers =' 
       .'['      
       .$markers                                   // line 34
       .'];'; 
  
  
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If the while loop doesn't run, $markers will not exist when it's imploded.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

You also have the echoed $markers outside the function.

You can do a couple things, move the echo inside the function, or make markers global.
Just call this in the begging of the function..

Code: Select all

global $markers;
-Zoxive
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

Post by garry27 »

ok i've removed the function out of the script and included the db function and i now get the following errors on my debugger:
<br />
<b>Notice</b>: Undefined variable: 2 in <b>/home/unn_p921847/public_html/test/map_data.php</b> on line <b>19</b><br />
<br />
<b>Notice</b>: Undefined variable: DL3 6JH in <b>/home/unn_p921847/public_html/test/map_data.php</b> on line <b>19</b><br />
<br />
<b>Notice</b>: Undefined variable: DL1 5NQ in <b>/home/unn_p921847/public_html/test/map_data.php</b> on line <b>19</b><br />
<br />
<b>Notice</b>: Undefined variable: DL3 7LX in <b>/home/unn_p921847/public_html/test/map_data.php</b> on line <b>19</b><br />
<br />
<b>Notice</b>: Undefined variable: DL1 5HW in <b>/home/unn_p921847/public_html/test/map_data.php</b> on line <b>19</b><br />
<br />
<b>Notice</b>: Undefined variable: DL3 7JG in <b>/home/unn_p921847/public_html/test/map_data.php</b> on line <b>19</b><br />
<br />
<b>Notice</b>: Undefined variable: DL3 7LL in <b>/home/unn_p921847/public_html/test/map_data.php</b> on line <b>19</b><br />
<br />
<b>Notice</b>: Undefined variable: DL1 5QD in <b>/home/unn_p921847/public_html/test/map_data.php</b> on line <b>19</b><br />
<br />
<b>Notice</b>: Undefined variable: DL3 7AQ in <b>/home/unn_p921847/public_html/test/map_data.php</b> on line <b>19</b><br />
var markers
markers =[{ 'lng': -1.55736, 'lat': 54.5269, 'name': 2, 'address': 2<br>2<br>2<br>2<br>, 'phone': 2}, { 'lng': -1.55736, 'lat': 54.5269, 'name': Aruba, 'address': 12<br>King St<br>Darlington<br>County Durham<br>, 'phone': 1325},

{ 'lng': -1.55736, 'lat': 54.5234, 'name': Berlins, 'address': 0<br>Grange Rd<br>Darlington<br>County Durham<br>, 'phone': 1325}, { 'lng': -1.55736, 'lat': 54.5239, 'name': Flares, 'address': 82<br>Skinnergate<br>Darlington<br>County Durham<br>, 'phone': 1325},

{ 'lng': -1.55736, 'lat': 54.5236, 'name': Humphrys, 'address': 0<br>Blackwellgate<br>Darlington<br>County Durham<br>, 'phone': 1325}, { 'lng': -1.55736, 'lat': 54.5263, 'name': The Turks Head, 'address': 21<br>Bondgate<br>Darlington<br>County Durham<br>, 'phone': 1325},

{ 'lng': -1.55736, 'lat': 54.5254, 'name': Tanners Hall, 'address': 63<br>Skinnergate<br>Darlington<br>County Durham<br>, 'phone': 0}, { 'lng': -1.55355, 'lat': 54.5246, 'name': The Boot And Shoe, 'address': 0<br>Church Row<br>Darlington<br>County Durham<br>, 'phone': 1325},

{ 'lng': -1.56079, 'lat': 54.5256, 'name': Atlantic, 'address': 23<br>Duke Street<br>Darlington<br>Co. Durham<br>, 'phone': 1325}];

are these notices superficial because at first glance, the outputted data is spot on? if so, then how do i remove these notices gracefully so they don't interfere with the debugger?

much thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

They are not superficial. They point to underlying errors. Check that the variable is there before attempting to use it.

isset() may be of interest.
Post Reply