...

View Full Version : How do I get javascript to run a php function



ArnMan
10-01-2011, 05:09 PM
Hello,
I have a php function

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


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.

nomanic
10-01-2011, 08:59 PM
do you want to call this after the page has loaded, while users are viewing the page? or setup during page load?

ArnMan
10-01-2011, 09:33 PM
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.

nomanic
10-01-2011, 09:36 PM
this is php just inserted into javascript like this-



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

ArnMan
10-01-2011, 09:43 PM
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?

nomanic
10-01-2011, 09:48 PM
change this-



while($row = $db->fetch_row($npcl))
{
$nloc[]=$row;
}
foreach($nloc as $n)


into this-


$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!

ArnMan
10-01-2011, 09:53 PM
well that line was in another part of the script I was working, so I used it in the instance.


<?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.

nomanic
10-01-2011, 09:56 PM
i edited my post, reread it now.

ArnMan
10-01-2011, 10:01 PM
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 [ ]

Old Pedant
10-02-2011, 09:10 AM
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:


<?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.

Old Pedant
10-02-2011, 09:12 AM
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.

ArnMan
10-02-2011, 02:04 PM
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

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.

ArnMan
10-03-2011, 06:06 AM
:thumbsup: 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
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
// 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:

include "getnpcloc.php";
so it gets processed right off the bat.

and in the google maps javascript function:

function addPoints() {

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


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 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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum