Skip to main content
added 3451 characters in body
Source Link
Rob
  • 1.4k
  • 13
  • 41
  • 68

Here's what's in the geocode.php file:

<?php 

require("database.php");
// Opens a connection to a MySQL server
$con = mysql_connect("localhost", $username, $password);

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("medicom_wp", $con);


    $company = get_field('company_name');
    $address = get_field('address');
    $city = get_field('city');
    $post_code = get_field('post_code');

    $sql = sprintf("select count('x') as cnt from markers where `name` = '%s'", mysql_real_escape_string($company));
    $row_dup = mysql_fetch_assoc(mysql_query($sql,$con));
    if ($row_dup['cnt'] == 0) {
        mysql_query("INSERT INTO markers (`name`, `address`, `lat`, `lng`, `type`) VALUES ('".$company."', '".$address.", ".$city.", ".$post_code."', '0.0', '0.0', '')");
}
wp_reset_query();


define("MAPS_HOST", "maps.google.com");
define("KEY", "");

// Opens a connection to a MySQL server
$connection = mysql_connect("localhost", $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die("Can\'t use db : " . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;

// Iterate through the rows, geocoding each address
while ($row = @mysql_fetch_assoc($result)) {
  $geocode_pending = true;

  while ($geocode_pending) {
    $address = $row["address"];
    $id = $row["id"];
    $request_url = $base_url . "&q=" . urlencode($address);
    $xml = simplexml_load_file($request_url) or die("url not loading");

    $status = $xml->Response->Status->code;
    if (strcmp($status, "200") == 0) {
      // Successful geocode
      $geocode_pending = false;
      $coordinates = $xml->Response->Placemark->Point->coordinates;
      $coordinatesSplit = split(",", $coordinates);
      // Format: Longitude, Latitude, Altitude
      $lat = $coordinatesSplit[1];
      $lng = $coordinatesSplit[0];

      $query = sprintf("UPDATE markers " .
             " SET lat = '%s', lng = '%s' " .
             " WHERE id = '%s' LIMIT 1;",
             mysql_real_escape_string($lat),
             mysql_real_escape_string($lng),
             mysql_real_escape_string($id));
      $update_result = mysql_query($query);
      if (!$update_result) {
        die("Invalid query: " . mysql_error());
      }
    } else if (strcmp($status, "620") == 0) {
      // sent geocodes too fast
      $delay += 1000;
    } else {
      // failure to geocode
      $geocode_pending = false;
      echo "Address " . $address . " failed to geocoded. ";
      echo "Received status " . $status . "
\n";
    }
    usleep($delay);
  }
}


?>

Here's what's in the geocode.php file:

<?php 

require("database.php");
// Opens a connection to a MySQL server
$con = mysql_connect("localhost", $username, $password);

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("medicom_wp", $con);


    $company = get_field('company_name');
    $address = get_field('address');
    $city = get_field('city');
    $post_code = get_field('post_code');

    $sql = sprintf("select count('x') as cnt from markers where `name` = '%s'", mysql_real_escape_string($company));
    $row_dup = mysql_fetch_assoc(mysql_query($sql,$con));
    if ($row_dup['cnt'] == 0) {
        mysql_query("INSERT INTO markers (`name`, `address`, `lat`, `lng`, `type`) VALUES ('".$company."', '".$address.", ".$city.", ".$post_code."', '0.0', '0.0', '')");
}
wp_reset_query();


define("MAPS_HOST", "maps.google.com");
define("KEY", "");

// Opens a connection to a MySQL server
$connection = mysql_connect("localhost", $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die("Can\'t use db : " . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;

// Iterate through the rows, geocoding each address
while ($row = @mysql_fetch_assoc($result)) {
  $geocode_pending = true;

  while ($geocode_pending) {
    $address = $row["address"];
    $id = $row["id"];
    $request_url = $base_url . "&q=" . urlencode($address);
    $xml = simplexml_load_file($request_url) or die("url not loading");

    $status = $xml->Response->Status->code;
    if (strcmp($status, "200") == 0) {
      // Successful geocode
      $geocode_pending = false;
      $coordinates = $xml->Response->Placemark->Point->coordinates;
      $coordinatesSplit = split(",", $coordinates);
      // Format: Longitude, Latitude, Altitude
      $lat = $coordinatesSplit[1];
      $lng = $coordinatesSplit[0];

      $query = sprintf("UPDATE markers " .
             " SET lat = '%s', lng = '%s' " .
             " WHERE id = '%s' LIMIT 1;",
             mysql_real_escape_string($lat),
             mysql_real_escape_string($lng),
             mysql_real_escape_string($id));
      $update_result = mysql_query($query);
      if (!$update_result) {
        die("Invalid query: " . mysql_error());
      }
    } else if (strcmp($status, "620") == 0) {
      // sent geocodes too fast
      $delay += 1000;
    } else {
      // failure to geocode
      $geocode_pending = false;
      echo "Address " . $address . " failed to geocoded. ";
      echo "Received status " . $status . "
\n";
    }
    usleep($delay);
  }
}


?>
deleted 243 characters in body
Source Link
Rob
  • 1.4k
  • 13
  • 41
  • 68

Is there a way to only do a section of code on a template if the post has just been published or updated?

So for instance, I only want to do this ONCE:

$company = get_field('company_name');
$address = get_field('address');
$city = get_field('city');
$post_code = get_field('post_code');

When a post is published or updated. What do I need to wrap around that code?

UPDATE:

I need to check if any of my custom-post types called "members" have been saved. The template I'm using to display the posts is single-members.php.

If the post has been saved, then on my single-members.php template, I need to call get_template_part('geocode'); - but only if the post has been saved.

I DON'T WANT THIS TO HAPPEN IN THE ADMIN AREA, THIS NEEDS TO HAPPEN FRONT-END OF THE WEBSITE. OBVIOUSLY THE ONLY THING I WANT TO DO IN THE ADMIN AREA IS TO CLICK PUBLISH/UPDATE.

Is there a way to only do a section of code on a template if the post has just been published or updated?

So for instance, I only want to do this ONCE:

$company = get_field('company_name');
$address = get_field('address');
$city = get_field('city');
$post_code = get_field('post_code');

When a post is published or updated. What do I need to wrap around that code?

UPDATE:

I need to check if any of my custom-post types called "members" have been saved. The template I'm using to display the posts is single-members.php.

If the post has been saved, then on my single-members.php template, I need to call get_template_part('geocode'); - but only if the post has been saved.

I DON'T WANT THIS TO HAPPEN IN THE ADMIN AREA, THIS NEEDS TO HAPPEN FRONT-END OF THE WEBSITE. OBVIOUSLY THE ONLY THING I WANT TO DO IN THE ADMIN AREA IS TO CLICK PUBLISH/UPDATE.

Is there a way to only do a section of code on a template if the post has just been published or updated?

What do I need to wrap around that code?

UPDATE:

I need to check if any of my custom-post types called "members" have been saved. The template I'm using to display the posts is single-members.php.

If the post has been saved, then on my single-members.php template, I need to call get_template_part('geocode'); - but only if the post has been saved.

I DON'T WANT THIS TO HAPPEN IN THE ADMIN AREA, THIS NEEDS TO HAPPEN FRONT-END OF THE WEBSITE. OBVIOUSLY THE ONLY THING I WANT TO DO IN THE ADMIN AREA IS TO CLICK PUBLISH/UPDATE.

added 113 characters in body
Source Link
Rob
  • 1.4k
  • 13
  • 41
  • 68

Is there a way to only do a section of code on a template if the post has just been published or updated?

So for instance, I only want to do this ONCE:

$company = get_field('company_name');
$address = get_field('address');
$city = get_field('city');
$post_code = get_field('post_code');

When a post is published or updated. What do I need to wrap around that code?

UPDATE:

In my functions file I need to addcheck if any of my custom-post types called "members" have been saved. The template I'm using to display the hook:posts is single-members.php.

function my_data_update () {
 $company = get_template_part('geocode');
}
add_action('publish_post', 'my_data_update');
add_action('save_post', 'my_data_update');

OnIf the post has been saved, then on my single-members.php template I need to check whether the post was saved, if it was I then need to show the "geocode" part of the templatecall get_template_part('geocode'); - but only if the post has been saved.

I DON'T WANT THIS TO HAPPEN IN THE ADMIN AREA, THIS NEEDS TO HAPPEN FRONT-END OF THE WEBSITE. OBVIOUSLY THE ONLY THING I WANT TO DO IN THE ADMIN AREA IS TO CLICK PUBLISH/UPDATE.

Is there a way to only do a section of code on a template if the post has just been published or updated?

So for instance, I only want to do this ONCE:

$company = get_field('company_name');
$address = get_field('address');
$city = get_field('city');
$post_code = get_field('post_code');

When a post is published or updated. What do I need to wrap around that code?

UPDATE:

In my functions file I need to add the hook:

function my_data_update () {
 $company = get_template_part('geocode');
}
add_action('publish_post', 'my_data_update');
add_action('save_post', 'my_data_update');

On my single-members.php template I need to check whether the post was saved, if it was I then need to show the "geocode" part of the template.

Is there a way to only do a section of code on a template if the post has just been published or updated?

So for instance, I only want to do this ONCE:

$company = get_field('company_name');
$address = get_field('address');
$city = get_field('city');
$post_code = get_field('post_code');

When a post is published or updated. What do I need to wrap around that code?

UPDATE:

I need to check if any of my custom-post types called "members" have been saved. The template I'm using to display the posts is single-members.php.

If the post has been saved, then on my single-members.php template, I need to call get_template_part('geocode'); - but only if the post has been saved.

I DON'T WANT THIS TO HAPPEN IN THE ADMIN AREA, THIS NEEDS TO HAPPEN FRONT-END OF THE WEBSITE. OBVIOUSLY THE ONLY THING I WANT TO DO IN THE ADMIN AREA IS TO CLICK PUBLISH/UPDATE.

added 119 characters in body
Source Link
Rob
  • 1.4k
  • 13
  • 41
  • 68
Loading
added 280 characters in body
Source Link
Rob
  • 1.4k
  • 13
  • 41
  • 68
Loading
Source Link
Rob
  • 1.4k
  • 13
  • 41
  • 68
Loading