You are here: Re: Scope of php variables outside <?php ?> blocks « PHP Programming Language « IT news, forums, messages
Re: Scope of php variables outside <?php ?> blocks

Posted by Jerry Stuckle on 08/22/06 00:35

shawn.ready@gmail.com wrote:
> Hi All
>
> I have a little page I am having issues with. What I am doing is using
> php to query a odbc database and create a Google Map with point
> information I get back from the db. But the UpdateMyMap() function that
> has some php in it is not storing the the $pidpoint variable contents
> between calls (this page is a mix of php and javascript)...
>
> ----------START CODE HERE----------
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml"
> xmlns:v="urn:schemas-microsoft-com:vml">
>
> <?php
> $connectionstring = odbc_connect('some','bits','here');
> if (!$connectionstring) {
> echo '<p>Not connected to the database server at this time.</p>';
> echo '<p>Please try again later.</p>';
> }else {
> echo '<p>Connected to the database server at this time.</p>';
> global $pidpoint;
> //This is the var that is giving me issues
> $pidpoint="unique_String_1234";
> $pidpointNext;
> $pidpointOld;
>
> // Now that I have the PID, I can get the Lat/Long/Speed/Angle
> $stmt = odbc_prepare($connectionstring, 'EXEC getPIDLocation ?;');
> $result = odbc_execute($stmt,array($pidpoint));
> $Latitude = odbc_result($stmt, "Latitude");
> $Longitude = odbc_result($stmt, "Longitude");
> $Speed = odbc_result($stmt, "Speed");
> $Angle = odbc_result($stmt, "Angle");
> }
> odbc_close($connectionstring);
> ?>
>
>
>
> <head>
> <meta http-equiv="content-type" content="text/html;
> charset=utf-8"/>
> <title>Me Google Map</title>
> <style type="text/css">
> v\:* {
> behavior:url(#default#VML);
> }
> </style>
> <script
> src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=MyKeyHere"
> type="text/javascript"></script>
> <script type="text/javascript">
>
> <!-- I need to have some var defined at a high level -->
> var Latitude=<?php echo $Latitude ?>;
> var Longitude=<?php echo $Longitude ?>;
> var Speed=<?php echo $Speed ?>;
> var Angle=<?php echo $Angle ?>;
> var Pid=<?php echo "\"" . $pidpoint . "\""?>;
> var timePeriod=10000;
> var map;
>
> <!-- Most of these functions will be moved into suitable files as the
> project progresses -->
>
> function createMarker(point, details) {
> var marker = new GMarker(point);
> GEvent.addListener(marker, "click", function() {
> marker.openInfoWindowHtml(details);
> });
> return marker;
> }
>
> function UpdateMyMap() {
> <?php
> // This is were I am having issues, first time it is uses
> // the $pidpoint from first php calls above,
> global $pidpoint;
> $connectionstring = odbc_connect('some','bits','here');
> $pidpointOld=$pidpoint;
> $stmNextPoint = odbc_prepare($connectionstring, 'EXEC getNextPoint
> ?;');
> $resultNextPoint = odbc_execute($stmNextPoint,array($pidpointOld));
>
> $pidpointNext = odbc_result($stmNextPoint, "PID");
> $stmt = odbc_prepare($connectionstring, 'EXEC getPIDLocation ?;');
> $result = odbc_execute($stmt,array($pidpointNext));
> $Latitude = odbc_result($stmt, "Latitude");
> $Longitude = odbc_result($stmt, "Longitude");
> $Speed = odbc_result($stmt, "Speed");
> $Angle = odbc_result($stmt, "Angle");
> $pidpoint=$pidpointNext; // THIS IS NOT STORE 2nd TIME AROUND!!! it
> will use the unique_String_1234 AGAIN, AND AGAIN,
> // NOT THE VAR I SET IT TO FROM
> $pidpointNext
> // I think this is a scope issue, I have
> tried global and static...but I am doing something wrong...
> odbc_close($connectionstring);
> ?>
> Latitude=<?php echo $Latitude ?>;
> Longitude=<?php echo $Longitude ?>;
> Speed=<?php echo $Speed ?>;
> Angle=<?php echo $Angle ?>;
> Pid=<?php echo "\"" . $pidpoint . "\""?>;
> PidOld=<?php echo "\"" . $pidpointOld . "\""?>;
> alert("Old Point: " +PidOld + " New Point: " + Pid);
>
> details = "Location: Latitude = " + Latitude + " Longitude = " +
> Longitude + " <br/>Speed: km=" + Speed + "<br/>Angle=" + Angle;
> var point = new GLatLng(Latitude, Longitude);
> map.addOverlay(createMarker(point,details));
> map.setCenter(new GLatLng(Latitude,Longitude), 15);
> }
>
> <!-- This is going to be our main map generation area -->
> function load() {
> map = new GMap2(document.getElementById("map"));
> map.addControl(new GSmallMapControl());
> map.addControl(new GMapTypeControl());
> map.addControl(new GOverviewMapControl());
> map.setCenter(new GLatLng(Latitude, Longitude), 15);
> var point = new GLatLng(Latitude, Longitude);
> details = "Location: Latitude = " + Latitude + " Longitude = " +
> Longitude + "<br/>Speed: km=" + Speed + "<br/>Angle=" + Angle;
> map.addOverlay(createMarker(point,details));
> map.setCenter(new GLatLng(Latitude,Longitude), 15);
> window.setInterval(UpdateMyMap, timePeriod);
> }
>
> </script>
> </head>
>
> <body onload="load()">
> <div id="map" style="width: 1000px; height: 800px"></div>
> </body>
> </html>
>
> ----------END CODE HERE----------
>
> Any pointers/help would be great.
>
> Thanks Shawn.
>

Shawn,

Sure it will. All of your PHP code will be executed on the server, then
the page is sent to the client, where your javascript is executed. When
the page is resubmitted, all of your PHP code will be executed on the
server again, and the page is sent to the browser, where your javascript
is executed.

And so on.

Each time you load the page you start fresh.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация