0

Through searching myself I've found solutions to send HTML data to PHP which I have already done however, I want to get data from an XML file that is accessed by a PHP script into a HTML file due to the format it is in. This is my current attempt. This does not work, where am I going wrong?

  <div class="w3-container w3-red w3-cell">
    <h4>Todays Projections</h4>
    <p>#OUTPUT THE CURRENT DATE + PROJECTION</p>
    <?php 
    $xml=simplexml_load_file("XMLtest.xml") or die("Error: Cannot create object");
    $nodes = $xml->xpath("/february/finances[@date='". date("d-m-Y") . "']");
    echo "Date: ";
      $attr = $nodes->attributes();
      echo $attr['date'];
      echo "<br> Projection: ";
      echo $nodes->projection . ",<br>Recommended Staff: ";
    echo $nodes->recommendedStaff . ",<br>Staff Wages: ";
    echo $nodes->staffWages . ",<br>Actual: ";
    echo $nodes->actual . "<br>";
    ?>
  </div>

Sample XML:

<february>
  <finances id="48" date="17-02-2017">
        <projection>101</projection>
        <recommendedStaff>10</recommendedStaff>
        <staffWages>101</staffWages>
        <actual>101</actual>
    </finances>
    <finances id="49" date="18-02-2017">
        <projection>101</projection>
        <recommendedStaff>10</recommendedStaff>
        <staffWages>101</staffWages>
        <actual>101</actual>
    </finances>
    <finances id="50" date="19-02-2017">
        <projection>101</projection>
        <recommendedStaff>10</recommendedStaff>
        <staffWages>101</staffWages>
        <actual>101</actual>
    </finances>
</february>

Edit: PHP is working within other files hosted on the server and through my account. However I can't get it work in a HTML file like shown above.

  • 2
    That is the PRIME function of PHP – RiggsFolly Feb 18 '17 at 23:02
  • @RiggsFolly Could you expand on your point as I am looking for guidance and information rather than a statement? – Paragon Jenko Feb 18 '17 at 23:03
  • 1
    So what is the issue with this code? – RiggsFolly Feb 18 '17 at 23:04
  • To expand on what @RiggsFolly said, PHP is a templating language (e.g. to create HTML, but not limited to that). All you need to do is change your file extension to `.php` and write your PHP code inside of this file alongside HTML. – djthoms Feb 18 '17 at 23:04
  • It would probably be a good idea if you show us at least a relevant sample of the XML file you are processing – RiggsFolly Feb 18 '17 at 23:06
  • @AlexJenkinson what is the error that you are getting. Saying it does not work, does not help. We need to know what is not working, what is erring out. – Ray Hunter Feb 18 '17 at 23:06
  • @AlexJenkinson post the XML structure, also i noticed that in function date you are missing the ')' closing parenthesis – knetsi Feb 18 '17 at 23:07
  • Forgive me all I pasted the old code that was used in the PHP file itself. I will post a sample of the XML I am looking to access. – Paragon Jenko Feb 18 '17 at 23:07
  • @djthoms Therefore would I need to embed it such as done in this HTML file? As I was looking to use CSS to format it and my logical idea was to just embed it or find a way to output the actual output I have such as the GET function I used to send data? – Paragon Jenko Feb 18 '17 at 23:10
  • @RiggsFolly The issue is that when using the PHP shown above, it simply just displays the code not the values I want to show – Paragon Jenko Feb 18 '17 at 23:12
  • What does the URL in the browser address bar look like? Does it start `http://` or `file:///` – RiggsFolly Feb 18 '17 at 23:17
  • @RiggsFolly This is hosted externally through my Academy's own server therefore http:// – Paragon Jenko Feb 18 '17 at 23:22

1 Answers1

-1

If I understand your question properly, you want to output data from a PHP file so that it's visible to a user. To do this you need to do the following:

  1. Create a php file index.php
  2. Add your HTML and PHP code
  3. Ensure your web server is running
    • php -S localhost:8080
  4. Open the URL in your browser http://localhost:8080

Let's say your index.php file looks like this:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Demo</title>
</head>
<body>
    <ul>
        <li>Foo</li>
        <?php

            $source = array(1,2,3,4,5);

            for ($i = 0; $i < count($source); $i++) {
                echo '<li>' . $i . '</li>';
            }

        ?>
    </ul>
</body>
</html>

The rendered result looks like this:

  • Foo
  • 0
  • 1
  • 2
  • 3
  • 4
djthoms
  • 3,026
  • 2
  • 31
  • 56
  • When I change my current HTML document to a .php file it doesn't run at all? – Paragon Jenko Feb 18 '17 at 23:27
  • @AlexJenkinson see miken32's answer regarding syntax errors. Also, ensure you have a php server (or some kind of server running). Check any errors in the logs/console window. – djthoms Feb 18 '17 at 23:29