php - 在 HTML 中显示结果
问题描述
我正在编写代码以获取数据以在 HTML 中输出它们。我在 PHP 之外的 html 源中显示数据时遇到问题,因为我收到了一个错误:
警告:在第 150 行的 /home/username/public_html/check_score.php 中为 foreach() 提供的参数无效
这是第 150 行:
foreach($score->rules AS $rule) {
echo '<div name="test-row" style="margin-top:-1px">
<div name="description" style="float: left; width:470px;">' . $rule->description . '</div>';
echo '<div name="scores" style="float: right">' . $rule->score . '</div>';
echo '</div><br><hr>';
}
这是完整的代码:
<?php
if(isset($_POST['btn1']))
{
sleep(1);
$ch = curl_init('http://example.com/check_score');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$message = "hey chris";
$header = "Delivered-To: example@gmail.com
Received: by 2002:a0c:938a:0:0:0:0:0 with SMTP id f10csp5715121qvf;
Sun, 2 Dec 2018 06:07:45 -0800 (PST)
the long list of the header goes here...
Hey rob,
How you doing?";
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('email' => $header, 'options'=>'long')));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$response = curl_exec($ch);
$score = json_decode($response);
?>
<script type="text/javascript">
$(document).ready(function() {
$('#myModal').modal('show');
});
</script>
<?php
}
?>
<form action="" method="post">
<div class="container">
<div class="form-group">
<button name="btn1" id="btn1" type="submit" class="btn btn-primary btn-block button-size">Check for score
</button>
</div>
</div>
</form>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content" style="height:600px;">
<div class="modal-header" style="margin-bottom: 10px;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">SpamScore</h3>
</div>
<div class="modal-body" style="height: 77%;">
<?php
echo '<p><b>SpamScore points: <span id="bbb"></span>' . $score->score . '</b></p><hr>';
foreach($score->rules AS $rule) {
echo '<div name="test-row" style="margin-top:-1px">
<div name="description" style="float: left; width:470px;">' . $rule->description . '</div>';
echo '<div name="scores" style="float: right">' . $rule->score . '</div>';
echo '</div><br><hr>';
}
?>
</div><br>
<div class="modal-footer">
</div>
</div>
</div>
</div>
我检查了它显示的 PHP 中 $score 的结果:
stdClass Object ( [success] => 1 [score] => 0.2 [rules] => Array ( [0] =>
stdClass Object ( [score] => 0.1 [description] => Message has a DKIM or DK
signature, not necessarily valid ) [1] => stdClass Object ( [score] => 0.1
[description] => DKIM or DK signature exists, but is not valid ) ) [report]
=> pts rule description ---- ---------------------- ------------------------
-------------------------- 0.1 DKIM_SIGNED Message has a DKIM or DK
signature, not necessarily valid 0.1 DKIM_INVALID DKIM or DK signature
exists, but is not valid )
当我将 $score 放入 HTML 时,结果将显示为空。
它昨天工作正常,但今天在这里它不工作。$score
当我能够毫无问题地在 PHP 中获取数据时,我真的不明白为什么变量在 HTML 中显示为空。我试图在谷歌上找到答案,但我在任何地方都找不到。
您能否帮助我了解如何使用变量在 HTML 中显示数据$score
?
谢谢你。
解决方案
我刚刚注意到这里的一些东西。我不确定它是否会修复错误,但这可能是一个起点。循环中的第一个回显行没有尾随分号:
foreach($score->rules AS $rule) {
echo '<div name="test-row" style="margin-top:-1px">
<div name="description" style="float: left; width:470px;">' . $rule->description . '</div>';
echo '<div name="scores" style="float: right">' . $rule->score . '</div>';
echo '</div><br><hr>';
}
试一试:
foreach($score->rules AS $rule) {
echo '<div name="test-row" style="margin-top:-1px">;
<div name="description" style="float: left; width:470px;">' . $rule->description . '</div>';
echo '<div name="scores" style="float: right">' . $rule->score . '</div>';
echo '</div><br><hr>';
}
推荐阅读
- c# - 是否可以使用 OpenId Connect 对 Rest API 进行身份验证并从客户端传递凭据
- c# - 如何从列表中删除 ID 不在 ID 列表中的项目
- android - Android App返回的设备IP地址与手机不同
- c# - 在测试配置设置中找不到 MS 单元测试数据源
- html - 在 HTML5 项目编译期间插入日期
- python - 将文件复制到 Windows 桌面
- kubernetes - “错误:转发端口:需要升级请求” Kubernetes 集群的 helm 错误
- javascript - 迁移到 vuetify 2.0 beta (.5) 时遇到问题
- sql - 在 HANA 数据库中使用 NEWUID() 函数
- android - 如何根据另一个小部件中的操作更新 UI 或更改小部件中的状态?