0

I am trying to include a google map in my php file. I have it set up and when I run it, the map works perfectly with no errors, but when I include it in my php file, nothing comes up, no errors, just blank.

I have tried using require, include and include_once, and even just putting all the contents of my map file into my php file rather than including it, none of these work. I am getting no errors so I am unsure of why this is happening.

 <?php include_once '../google_map.php';?>

I expect my google map to display along with other contents in my php file. I didnt post the code for my map as it works fine on its own, just not in an include, please let me know if I should post it.

New: This is the google_map.php

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 50%;
         width: 40%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
   <script> 
   var map; 
 function initMap() 
{ 
map = new google.maps.Map(document.getElementById('map'), 
{
 center: {lat: -34.397, lng: 150.644}, zoom: 8 }); } initMap(); </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap"
    async defer></script>
  </body>

</html>

and this is how I am including it:

  <?php 
               if ((include '../google_map.php') == TRUE) {
    echo 'OK';

}
         ?>

When google_map.php is run itself, it works and shows the map. When i use include in a different php file, it does not work, and I only get 'OK' in which OK comes from the code above

1 Answers1

0

including a file sometimes is 'tricky' - from the PHP manual, https://www.php.net/manual/en/function.include.php Example #6, try this....

$string = get_include_contents('../google_map.php');

// Now, you can just 'echo' it and see the results
echo $string;

function get_include_contents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        return ob_get_clean();
    }
    return false;
}

I'm working on a very similar 'include' and this is the one that worked for me. It brings in variables too.

Apps-n-Add-Ons
  • 2,026
  • 1
  • 17
  • 28
  • I tried this and it still returned nothing, no errors or anything – Nicola O'Donoghue May 13 '19 at 13:02
  • what does this give you, now that you fixed the other errors? (I still believe this will be the right way to do this, though Google has a lot of 'protection' around using their maps and such - why do you need to do an include instead of just adding the map to the php file? – Apps-n-Add-Ons May 13 '19 at 15:45
  • I dont particularly need it in a separate file. But the map works in a file on its own and not in the php file, and when i add the map code to the php file instead of including it, it still does not work. I get no errors, just blank, with ' OK ' meaning the include did work – Nicola O'Donoghue May 13 '19 at 15:47
  • Really confusing on what you are doing and trying..... - I asked for the results using what I gave you and you tell me something about 'OK' (which is neither in your code example nor in my added answer....), so it is very unclear what your code is doing.... If you want help, you need to be sure you are following the 'helper' and showing code changes, etc. (I know, when things don't work, you sort of 'hack' at anything, but we can't help a moving target either....). Update your code to show the php file with Google in it that doesn't work - that sounds like your fastest way to get this done. – Apps-n-Add-Ons May 13 '19 at 15:52
  • @CFTSupport I am trying as clearly as I can to explain. I changed the post. – Nicola O'Donoghue May 13 '19 at 16:27