10

My website prints the user location by calling an API. Say I have the following code all in one file index.php on my website:

<?php
    $sIP = $_SERVER['REMOTE_ADDR'];
    $sURL = "http://ipinfo.io/" .$sIP . "/json";
    $sJSON = file_get_contents($sURL);
    $view = (object) array();
    $view->JSON = $sJSON;
?>

<!DOCTYPE html>
<html>
    <head>
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    </head>
    <body>
    <script>
        // global string of Data
        var gsData = <?= $view->JSON ?>

        $(document).ready(function(){
            var sRegion = gsData.region;
            $('#result').html(sRegion);
        });
    </script>
    <div>Region from data is:</div>
    <div id="result"></div>
</body>

Though, I need to keep the PHP script and the markup separated. I can't embed the PHP script in a script element. What can I do to have a PHP and a HTML file?

Cesare
  • 9,139
  • 16
  • 78
  • 130
  • 2
    you can use template engines, there are a lot of them – Iłya Bursov Dec 31 '15 at 17:16
  • @Lashane What's a template engine? Sorry for my ignorance if it's something simple. – Jonathan Lam Dec 31 '15 at 17:22
  • In general it is correct and important to keep logic and markup separated. However I see little sense in your given example, since there is no logic involved here. This is "just" a passive view. The fact that some small php snippets are used does not change that. – arkascha Dec 31 '15 at 17:23
  • @JonathanLam https://en.wikipedia.org/wiki/Web_template_system – Iłya Bursov Dec 31 '15 at 17:23

3 Answers3

16

EDIT: Use include_once() instead of include because you can use relative paths.

You can move the PHP into other files and then include_once it back into the main file.

The HTML file will still have to be a .php file, but you can include all of your PHP script (besides the include) in a different file for better maintainability / readability.

For example:

script.php

<?php
    $sIP = $_SERVER['REMOTE_ADDR'];
    $sURL = "http://ipinfo.io/" .$sIP . "/json";
    $sJSON = file_get_contents($sURL);
    $view = (object) array();
    $view->JSON = $sJSON;
?>

index.php

<?php include_once("script.php") ?>
<!-- ^ is the only PHP you will need to include a separate PHP file -->
<!DOCTYPE html>
<html>
    <head>
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    </head>
    <body>
    <script>
        // global string of Data
        var gsData = <?= $view->JSON ?>

        $(document).ready(function(){
            var sRegion = gsData.region;
            $('#result').html(sRegion);
        });
    </script>
    <div>Region from data is:</div>
    <div id="result"></div>
</body>
Community
  • 1
  • 1
Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
  • I made sure the extensions and the file names were the same but I'm getting this error on localhost: `Warning: include(script.php): failed to open stream: No such file or directory in - on line 1 Warning: include(): Failed opening 'script.php' for inclusion (include_path='.:') in - on line 1`. What would you do if you were me? It looks like PHP is unable to find the script.php file. – Cesare Dec 31 '15 at 17:28
  • Perhaps single-quotes are needed to import a PHP file, not double quotes. – Cesare Dec 31 '15 at 17:31
  • Thanks Jonathan. I'm getting the same issue; does it work on your end? – Cesare Dec 31 '15 at 17:39
  • @CeCeXX Yes, it does work for me... Maybe it's file permissions? – Jonathan Lam Dec 31 '15 at 17:42
  • Thanks. I wasn't able to get my local printed out, but I guess that's another issue. – Cesare Dec 31 '15 at 20:27
  • @JonathanLam what about when need to show unknown number of same components (e.g. `.image-container>(.image+.title+.description+.a>button)`) that read multiple rows of a database table for content? How do you get rid of that ` `? – s3c Feb 21 '20 at 08:10
3

You can also use php framework like Laravel, CodeIgniter or CakePHP, so that you can separate your php logic from html code and then send data to html page. It is very good practice for your future career.

sabbir
  • 2,020
  • 3
  • 26
  • 48
2

You can put the PHP script in a separated PHP file, and use AJAX to call it, then you use the Javascript to print the JSON data that generated by the PHP script.

DevManX
  • 476
  • 3
  • 14