Jump to content

Problem with variable...


Rootstonian

Recommended Posts

I'm coding a google map script in php/javascript, and I'm having a hell of a time with the address.  It seems to not like any spaces for what I can tell (and I'm no php programmer).

/* Create an array of address values */
addresses = [
<?php    
   for($i=0; $i < count($dataArray); $i++) {
      $p = $dataArray[$i];
      $p = explode(":", $p);
      $addr = $p[4]; 
      echo "$addr,\n";
   }
?>                       
];

Here is data file (just one line for testing):

1315414069:41.100908:-81.442699:33:150 North Ave Tallmadge OH:1

 

Here is "view source" from browser:

      /* Create an array of address values */

      addresses = [

              150 North Ave Tallmadge OH,

                     

];   

 

I'm stumped :(  The map will not display with this.  If I change the $addr to another variable in the array, it works fine.

Trying to put the address in an info window on the map.

 

TIA,

 

Roots

Link to comment
Share on other sites

well the code produced by that script adds an extra comma to the end (in your case, after the first entry in the javascript array) which makes the produced javascript invalid. You need to remove that trailing comma and newline. The following would probably be better

 

 

<?php  
$str = "";//start with empty string  
   for($i=0; $i < count($dataArray); $i++) {
      $p = $dataArray[$i];
      $p = explode(":", $p);
      $addr = $p[4]; 
      $str .= "$addr,\n";//concatenate instead of echoing
   }
//now we can echo, but before we need to take off the ending 2 characters
$str = substr($str, 0, strlen($str)-2);
echo $str;
?>

Link to comment
Share on other sites

TO be honest though, there is a function that can translate a php object into json notation. its called json_encode. you can use it like so

$arr = array();//empty array
for($i=0; $i < count($dataArray); $i++) {
      $p = $dataArray[$i];
      $p = explode(":", $p);
      $addr = $p[4]; 
      $arr[] =$addr;//push each address onto the array
   }

echo json_encode($arr);

Link to comment
Share on other sites

I cleaned-up my code with ALL of your suggestions...to wit: <from "view source" of web page>

 

    /* The addresses array */

    addresses = ['150 North Ave Tallmadge OH','33 Main Tallmadge OH','107 SouthEast Tallmadge OH'];

 

Much cleaner now and the code is shorter.  I am a programmer, just not PHP....yet.

 

This is just a small part of an Android project I'm working on...I'm sure I'll be back with more questions! LOL

Just wanted to thank everyone for the quick responses.

 

Roots

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.