0
\$\begingroup\$
<html>
<head>
  <title>GPS Tracker</title>
  <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="view/style.css">
  <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">/script>
  <script type="text/javascript">
    function show_maps()
      {
      var div_peta = document.getElementById('kanvas');
      var tengah = new google.maps.LatLng(-8.801502,115.174794);
      var options = 
      {
        center : tengah,
        zoom : 14,
        mapTypeId : google.maps.MapTypeId.ROADMAP
      }
      //make map object
      var google_map = new google.maps.Map(div_peta,options);
   }
 </script>
</head>
<body OnLoad="show_maps();">
 <div class="container">
 <div class="row">
   <div class="col-md-4">
     <div class="panel panel-default panel-body">
       <p><b><h4>Tracking</h4></b>
       <hr>
       <?php
         include"controller/connection.php";
         $query=mysql_query("SELECT id_history FROM tb_history");
         $count=mysql_num_rows($query);
       ?>
       <form method="POST" action="<?=$_SERVER['PHP_SELF']?>">
         <input type="hidden" name="count" value="<?php echo $count ?>">
         <input type="submit" name="submit" class="btn btn-primary" value="Get Location" style="width:280px"> </button></a></p>
       </form>
     </div>
   </div>
   <div class="col-md-8">
     <div class="panel panel-default panel-body" style="height:490px">
       <div id="kanvas"> kanvas peta</div>
     </div>
   </div>
 </div>
<?php
 if(isset($_POST['submit']))
   {
     include "controller/connection.php";
     $count1 = $_POST['count']; 
     $count2 = $count1+1;
     $sql = mysql_query("SELECT id_history FROM tb_history");
     $count3 = mysql_num_rows($sql);
     if($count3==$count1)
     {
       echo"<script>setTimeout(show_maps,1000)</script>";  
     }
     else if
     {
       echo"<script type='text/javascript'>
       alert('Data has been added!');
       </script>";
     }
   }
 ?>
</body>
</html>

The code that I wrote above will show a button and a Google map.

Once the button has been clicked, the code will send the amount of data that exist in the tb_history.

The PHP code at the bottom will always check if there's any new data added or not. If not, then it will refresh the Google map until new data has been added to the table tb_history.

Can anyone suggest a better algorithm for this?

\$\endgroup\$
0

3 Answers 3

1
\$\begingroup\$

In my opinion the best way to achieve what you want is to work with Event Listeners.

So basically, breaking it down to software design, you would fire a event if something is written to the database, so everytime you insert into the database. Maybe also when you update or delete something if you want to trigger these too.

Then you need an Event Listener which will run php code which runs and does what you want when new Data is added only when new Data is added.

Then you could send frequently requests to the PHP file, which gives you the message back if data is updated or not, but that would be counterproductive behavior because your already working event driven.

So you fire a Response to the JavaScript from the PHP file when the Event got fired. So your whole code will only run if it must, which depends only on if there's new data or not. Without continious requests whatsoever.

This would be the best attempt, you'll need some things for that, like the before mentioned Event Listeners, jQuery Ajax Calls or Plain JavaScript Ajax Calls.

Also some thougts to your SQL code

$count1 = $_POST['count']; //NEVER use data from Requests directly! 

You're vulnerable to SQL Injection! Always escape the input and use prepared statements

$count2 = $count1+1; //Attempting to the above code there's no need for this line

And at least you can mix these 2 lines togther to one line:

$sql = mysql_query("SELECT id_history FROM tb_history");
$count3 = mysql_num_rows($sql);

Would be

$count3 = mysql_query("SELECT count(id_history) FROM tb_history");
\$\endgroup\$
1
\$\begingroup\$

I don't do , but I presume the last else if block should read else if($count3==$count2), or just else... as it stands I would think it's broken code you've got here.

include "controller/connection.php";
$count1 = $_POST['count']; 
$count2 = $count1+1;
$sql = mysql_query("SELECT id_history FROM tb_history");
$count3 = mysql_num_rows($sql);
if($count3==$count1)
{
  echo"<script>setTimeout(show_maps,1000)</script>";  
}
else if <!-- here -->
{
  echo"<script type='text/javascript'>
  alert('Data has been added!');
  </script>";
}

This snippet could use much more meaningful names.

$count1 -> $postCount
$count2 -> $expectedCount;
$count3 -> $actualCount

This would make your condition much easier to parse:

if($actualCount==$postCount)
{
   ...
}
else if($actualCount==$expectedCount) <!-- assuming intent -->
{
   ...
}

It looks like the condition could be simplified, putting the "happy path" at the top:

if($actualCount==$expectedCount)
{
    echo"<script type='text/javascript'>
    alert('Data has been added!');
    </script>";
}
else
{
    echo"<script>setTimeout(show_maps,1000)</script>";  
}

Otherwise it looks like your code is defaulting to success!

\$\endgroup\$
0
\$\begingroup\$

You need 3 php files. One to render the map and fire a javascript function which returns the second php page containing the map data.

Another javascript function runs every few seconds and compares a checksum of the map data against a checksum of a fresh database query returned by the third php page via an ajax request.

If the checksums are different, call a further javascript function to reload the data.

\$\endgroup\$
2
  • \$\begingroup\$ If you don't mind, would you like to write it down in algorithm so it makes me easier to understand... Thank you.. \$\endgroup\$
    – user3581186
    Commented Jun 6, 2014 at 8:33
  • \$\begingroup\$ Load the page, add the data & button and a javascript variable showing a checksum of the dataset used. The button click handler then starts a javascript interval, making an ajax request to databasechecksum.php which returns the checksum of the server side dataset. If the two checksums don't match then call another javascript function to make an ajax request to getnewdata.php which will return the json/xml/soap/whatever to repopulate the map. Does this make more sense? \$\endgroup\$
    – sanepete
    Commented Jun 6, 2014 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.