【发布时间】:2017-02-16 21:36:08
【问题描述】:
我正在尝试根据放入数组中的 url、高度、宽度等来格式化 iframe。这是我到目前为止的代码。
<div class="row"
<div class="col-md-6">
<div class="info-box twitter-bg">
<div>
<?php
if ($cfg_array['grafana'] == 'true') {
include 'dashlets.php';
foreach ($dl as $element) {
$url = $element["url"];
$height = $element["height"];
$width = $element["width"];
echo "<iframe src=\"" . $url . " \"" . "height=\"" . $height . " \"" . "width=\"" . $width . " \"" . "frameborder=\"0\">" . " " . "<\iframe>" . " ";
}
}
?>
</div>
</div>
</div>
这是来自 dashlets.php 的数组。
<?php
$dl = array(
'dashlet1' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=6',
'height' => '200',
'width' => '450'
),
'dashlet2' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=7',
'height' => '200',
'width' => '450'
),
'dashlet3' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=11',
'height' => '200',
'width' => '450'
),
'dashlet4' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=4',
'height' => '200',
'width' => '350'
),
'dashlet5' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=8',
'height' => '200',
'width' => '450'
),
'dashlet6' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=10',
'height' => '200',
'width' => '450'
),
'dashlet7' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=3',
'height' => '200',
'width' => '450'
),
'dashlet8' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=5',
'height' => '200',
'width' => '350'
)
);
我的问题是,循环只在循环内部时从数组中提取第一个值,如果它位于循环括号之外,则只提取最后一个值,如下所示。 我正在尝试从数组项中格式化 多个 url,并将它们放置在彼此水平的 iframe 中。 是不是我做错了什么?
已编辑以添加完整代码,包括 html div。
按照建议将所有内容移至 dashlets.php。我仍然只从第一个数组项中获得返回结果。
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
$dl = array(
'dashlet1' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=6',
'height' => '200',
'width' => '450'
),
'dashlet2' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=7',
'height' => '200',
'width' => '450'
),
'dashlet3' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=11',
'height' => '200',
'width' => '450'
),
'dashlet4' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=4',
'height' => '200',
'width' => '350'
),
'dashlet5' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=8',
'height' => '200',
'width' => '450'
),
'dashlet6' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=10',
'height' => '200',
'width' => '450'
),
'dashlet7' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=3',
'height' => '200',
'width' => '450'
),
'dashlet8' => array(
'url' => 'http://192.168.86.105:3000/dashboard-solo/db/home?panelId=5',
'height' => '200',
'width' => '350'
)
);
foreach ($dl as $element) {
$url = $element["url"];
$height = $element["height"];
$width = $element["width"];
echo "<iframe src=\"" . $url . "\"" . " height=\"" . $height . "\" " . "width=\"" . $width . "\"" . " frameborder=\"0\">" . " " . "<\iframe>" . "\n";
}
在我的 main.php 中,可以从我的 index.php 视口查看。
<div class="row"
<div class="col-md-6">
<div class="info-box twitter-bg">
<div>
<?php
if ($cfg_array['grafana'] == 'true') {
include 'dashlets.php';
}
?>
</div>
</div>
</div>
【问题讨论】:
-
您希望 PHP 代码的结果是什么?
标签: php multidimensional-array foreach