Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question How do I get javascript to run a php function

    Hello,
    I have a php function
    PHP Code:
    function npclocs()
    {
        
    $npcl=$db->query("SELECT * FROM npclocation WHERE npclong < {$ir['userlongitute']}+5 AND npclong > {$ir['userlongitute']}-5 AND npclat > {$ir['userlatitude']}-5 AND npclat < {$ir['userlatitude']}+5 ");
        
    /*$npcarray = mysql_fetch_array($npcl);*/
        
    $rows =0;
        while(
    $row $db->fetch_row($npcl))
      {
          
    $nloc[]=$row;
    }
    foreach(
    $nloc as $n)
    {
       echo 
    "newpoints[" $rows "] = new Array(".$n['npclong'].", ".$n['npclat'].", ".$n['npciicon'].", ".$n['npclname'].", ".$n['npclname'].");
       "
    ;
       
       
    $rows++;
      }

    and I have a javascript function that I would like the results of this function to insert its results in the javascript here is the function in javascript

    Code:
    function addPoints() {
    
    
     
    //newpoints[0] = new Array(-73, 46, icon0, 'monroe', 'monroepopup'); 
    //newpoints[1] = new Array(-69, 52, icon0, 'jackson', 'jacksonpopup'); 
    //newpoints[2] = new Array(-45, 39, icon0, 'Benton', 'bentonpopup');
        
    	 
    
    	for(var i = 0; i < newpoints.length; i++) {
    		var point = new GPoint(newpoints[i][1],newpoints[i][0]);
    		var popuphtml = newpoints[i][4] ;
    		var marker = createMarker(point,newpoints[i][2],popuphtml);
    		map.addOverlay(marker);
    	}
    }
    The commented out //newpoints is the old js array. my php function queries mysql and uses whiles and foreach to create the same output, as the old js array.
    I would like that php function result to insert its result into the js function.

    Is this possible?

    Thank you in advance.

  • #2
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    do you want to call this after the page has loaded, while users are viewing the page? or setup during page load?

  • Users who have thanked nomanic for this post:

    ArnMan (10-03-2011)

  • #3
    New Coder
    Join Date
    Jun 2011
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Well, I am guessing page load. What the javascript is, is actually google maps api, and what it does is puts balloons on the map in a short radius around the current user.

    I have noticed that when the page loads, the map is the last thing that pops up.

  • #4
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    this is php just inserted into javascript like this-

    Code:
    function addPoints() {
     
    <?php
    function npclocs() 
    { 
        $npcl=$db->query("SELECT * FROM npclocation WHERE npclong < {$ir['userlongitute']}+5 AND npclong > {$ir['userlongitute']}-5 AND npclat > {$ir['userlatitude']}-5 AND npclat < {$ir['userlatitude']}+5 "); 
        /*$npcarray = mysql_fetch_array($npcl);*/ 
        $rows =0; 
        while($row = $db->fetch_row($npcl)) 
      { 
          $nloc[]=$row; 
    } 
    foreach($nloc as $n) 
    { 
       echo "newpoints[" . $rows . "] = new Array(".$n['npclong'].", ".$n['npclat'].", ".$n['npciicon'].", ".$n['npclname'].", ".$n['npclname']."); 
       "; 
        
       $rows++; 
      } 
    }  
    
    npclocs();
    ?>
    	for(var i = 0; i < newpoints.length; i++) {
    		var point = new GPoint(newpoints[i][1],newpoints[i][0]);
    		var popuphtml = newpoints[i][4] ;
    		var marker = createMarker(point,newpoints[i][2],popuphtml);
    		map.addOverlay(marker);
    	}
    }
    and just have the php inline on the page
    Last edited by nomanic; 10-01-2011 at 08:38 PM.

  • Users who have thanked nomanic for this post:

    ArnMan (10-03-2011)

  • #5
    New Coder
    Join Date
    Jun 2011
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts
    That looks simple enough
    but when i opened dreamweaver and copied and pasted the change and it gave me a error on

    $nloc[]=$row;

    which I have noticed at other times, when I have something like this similiar when workin in js i get an error. how do I fix this?

  • #6
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    change this-

    Code:
        while($row = $db->fetch_row($npcl)) 
      { 
          $nloc[]=$row; 
    } 
    foreach($nloc as $n)
    into this-
    Code:
        $nloc=array();
        while($nloc[] = $db->fetch_row($npcl)) {} 
        foreach($nloc as $n)
    this is a javascript forum lol, not php, if this fixes it thank me!
    Last edited by nomanic; 10-01-2011 at 08:52 PM.

  • Users who have thanked nomanic for this post:

    ArnMan (10-01-2011)

  • #7
    New Coder
    Join Date
    Jun 2011
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts
    well that line was in another part of the script I was working, so I used it in the instance.

    PHP Code:
       <?php
    function npclocs() 

        
    $npcl=$db->query("SELECT * FROM npclocation WHERE npclong < {$ir['userlongitute']}+5 AND npclong > {$ir['userlongitute']}-5 AND npclat > {$ir['userlatitude']}-5 AND npclat < {$ir['userlatitude']}+5 "); 
        
    /*$npcarray = mysql_fetch_array($npcl);*/ 
        
    $rows =0
        while(
    $row $db->fetch_row($npcl)) 
      { 
          
    foreach(
    $row as $n

       echo 
    "newpoints[" $rows "] = new Array(".$n['npclong'].", ".$n['npclat'].", ".$n['npciicon'].", ".$n['npclname'].", ".$n['npclname']."); 
       "

        
       
    $rows++; 
      } 
    }  

    npclocs();
    ?>
    this is what I have now I changed the foreach to $row instead of $nloc
    and it is giving me a syntax error on the echo line now.

  • #8
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    i edited my post, reread it now.

  • Users who have thanked nomanic for this post:

    ArnMan (10-03-2011)

  • #9
    New Coder
    Join Date
    Jun 2011
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I do appreciate your help and you will get a huge thank you, even if you didnt get to help me.
    I noticed that those [ ] cause the problem if I remove them the error goes away.
    The php works fine when I am using just php, but messes up in js
    I still get a syntax error wherever there are [ ]

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Quote Originally Posted by ArnMan View Post
    That looks simple enough
    but when i opened dreamweaver and copied and pasted the change and it gave me a error on

    $nloc[]=$row;

    which I have noticed at other times, when I have something like this similiar when workin in js i get an error. how do I fix this?
    That code is *NOT* "in js". It's in PHP.

    If DumbWhacker thinks it is a JavaScript error, then it's a typical DuckWalker mess up.

    On the other hand, if DW says it's a PHP error, it might really be.

    I am *NOT* a PHP person, and properly this question belongs in the PHP forum, but...

    I *THINK* this works:
    Code:
    <?php
    function npclocs() 
    { 
        $sql = "SELECT * FROM npclocation "
             . " WHERE npclong < {$ir['userlongitute']}+5 "
             . " AND npclong > {$ir['userlongitute']}-5 "
             . " AND npclat > {$ir['userlatitude']}-5 "
             . " AND npclat < {$ir['userlatitude']}+5 ";
       
        $npcl=$db->query($sql) or die("bad SQL query");
        $rows = 0;
        while($row = $db->fetch_row($npcl)) 
        { 
             echo "newpoints[" . $rows . "] = ["
                     . $n['npclong'] . ", " 
                     . $n['npclat'] . ",'"
                     . $n['npciicon'] . "','"
                     . $n['npclname'] . "','"
                     . $n['npclname'] . "'];\n" ;
        
            $rows++; 
        } 
    }  
    npclocs();
    ?>
    Among other things, I'm assuming that the fields npciicon and npclname (why is that latter repeated?) are *strings*. So in the JS array, they need "..." or '...' around them. Look carefully at my code and you will see the apostrophes in there, beside the appropriate commas, etc.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    ArnMan (10-03-2011)

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Ahhh...looks like maybe you don't need/want apostrophes around the icon designator. Is that a JavaScript variable in your code?

    Well, whatever. Easy to remove them, now that they are there.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    ArnMan (10-03-2011)

  • #12
    New Coder
    Join Date
    Jun 2011
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Ok
    here is what i have done to get to this point. added google maps js code to my site to show the current location for the user.
    That works, great. It is a php file with js in it, and two single php variables inserted into the googles maps for the users longitude and latitude.
    Now I want to show in a small radius around the user, npc's or non player characters, whose longitude and latitude are also in database.

    so I found this site http://mapmaker.donkeymagic.co.uk/

    and the site was able to take information and provide me with another javascript script.
    Then after tinkering I was able to integrate the google maps javascript and the donkeymagic generated script.

    the commented out //newpoints wasn't mine, that whole function was generated for me, if I uncomment those 3 //newpoints lines, 3 balloons show up on my map around the center icon.
    Since later on im going to be having dynamic npc long's and lat's, and quite a long list of them, having 100s of newpoints[] arrays, would take forever for the page to download.
    so I created a php function that outputs the exact same format as the
    Code:
    newpoints[0] = new Array(-73, 40, icon0, 'monroe1', 'monroepopup1');
    with npc information
    the second npclname in the while loop just puts the name inside the infowindow when you click on a balloon.

    I have depending on the case of how I need to do it. both a function and a php file with the same code.
    I just want the output or result of that php that works, inserted in javascript that also works, and act as part of the code, so javascript can read it and process it.

    Now I appreciate everyone's help, i'll try the code above. im sorry this is long winded.
    I was thinking this was a javascript question since I am trying to insert either php code, that works in php, or somehow get the result of the php code, into javascript.

    I'll let you know how the new code works out from above.

    Thank ya'll for being so patient and helpful.

  • #13
    New Coder
    Join Date
    Jun 2011
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Thumbs up I figured it out, finally......

    I have tried the above code and it still didnt work, so after taking a break, did some chores, had supper, i had a thought.
    I wondered if I could take the result of the function and turn it into a string. and then use it as a variable inside the js script
    so here is what I did:
    I opened my getnpcloc.php file
    PHP Code:
    <?php
    include "globals.php";

    $npcl=$db->query("SELECT * FROM npclocation WHERE npclong < {$ir['userlongitute']}+5 AND npclong > {$ir['userlongitute']}-5 AND npclat > {$ir['userlatitude']}-5 AND npclat < {$ir['userlatitude']}+5 ");
        
    /*$npcarray = mysql_fetch_array($npcl);*/
        
    $rows =0;
        while(
    $row $db->fetch_row($npcl))
      {
          
    $nloc[]=$row;
    }
    foreach(
    $nloc as $n)
    {
       
    $newpoints "newpoints[" $rows "] = new Array(".$n['npclong'].", ".$n['npclat'].", ".$n['npciicon'].", ".$n['npclname'].", ".$n['npclname'].");
       "
    ;
       
      
         
     
    $rows++;
      }

    ?>
    and changed it to this:
    PHP Code:
    <?php
    // I deleted the include   <-----------------------

    $npcl=$db->query("SELECT * FROM npclocation WHERE npclong < {$ir['userlongitute']}+10 AND npclong > {$ir['userlongitute']}-10 AND npclat > {$ir['userlatitude']}-10 AND npclat < {$ir['userlatitude']}+10 ");
        
    /*$npcarray = mysql_fetch_array($npcl);*/
        
    $rows =0;
        while(
    $row $db->fetch_row($npcl))
      {
          
    $nloc[]=$row;
    }
    ob_start();// I added this  <-----------------------
    foreach($nloc as $n)
    {
       echo 
    "newpoints[" $rows "] = new Array(".$n['npclong'].", ".$n['npclat'].", ".$n['npciicon'].", '".$n['npclname']."', '".$n['npclname']."');
       "
    ;// I changed this  <-----------------------
       
      
         
     
    $rows++;
      }
    $myStr ob_get_contents();// I added this  <-----------------------
    ob_end_clean();// I added this  <-----------------------

    ?>
    I then opened my map page and I added at the top:
    PHP Code:
    include "getnpcloc.php"
    so it gets processed right off the bat.

    and in the google maps javascript function:
    PHP Code:
    function addPoints() {

     var 
    mystr $myStr;// I added this <---------------------


    for(var 0newpoints.lengthi++) {
            var 
    point = new GPoint(newpoints[i][1],newpoints[i][0]);
            var 
    popuphtml newpoints[i][4] ;
            var 
    marker createMarker(point,newpoints[i][2],popuphtml);
            
    map.addOverlay(marker);
        }

    And to my surprise, I now extra balloons on my map.
    So for everyone that has put an effort to figure this out, I give a huge thanks for helping me..

    Thank you very much


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •