通过搜索自己,我找到了将HTML数据发送到PHP的解决方案,但是我已经完成了,我希望从PHP脚本访问的XML文件中获取数据,因为它的格式是这是我目前的尝试。这不起作用,我哪里错了?
<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>
示例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>
编辑: PHP正在服务器上托管的其他文件中以及通过我的帐户工作。但是我无法在如上所示的HTML文件中使用它。
答案 0 :(得分:0)
你有几个语法错误,我不认为这会运行。这对我有用:
$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>';
$xml=simplexml_load_string($xml);
$nodes = $xml->xpath("/february/finances[@date='". date("d-m-Y" . "']")); # fixed month name, added missing parenthesis
$node = $nodes[0]; # xpath returns an array of nodes, not just one
echo <<< HTML
Date: $node[date]<br/>
Projection: $node->projection,<br/>
Recommended Staff: $node->recommendedStaff,<br/>
Staff Wages: $node->staffWages,<br/>
Actual: $node->actual<br/>
HTML;
注意我已将heredoc syntax用于HTML块。
编辑:在您指出的评论中,您甚至无法运行PHP,这是一个完全不同的问题。但是一旦你解决了这个问题,这个代码应该适合你。
答案 1 :(得分:-1)
如果我正确理解您的问题,您希望从PHP文件输出数据,以便用户可以看到它。为此,您需要执行以下操作:
index.php
php -S localhost:8080
假设您的index.php
文件如下所示:
<!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>
渲染结果如下所示: