2017-02-16 131 views
0

我試圖格式化從網址,高度,寬度等放入數組中的iframe。這是我迄今爲止的代碼。從關聯數組中格式化URL

   <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' 
    ) 
); 

我的問題是環路只從陣列環路和僅最後值內部時拉動所述第一值,如果其放置在環路括號外,如下所示。 我試圖格式化多個從陣列項目的網址,並將它們放置在一個iframe水平對彼此。 有什麼我做錯了嗎?

編輯添加完整的代碼,包括html divs。

剛纔將所有內容都移到了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> 
+0

您想成爲PHP代碼的結果? –

回答

1

我只是想你的代碼,只需加入到dashlets.php:

<?php 
     $dl = array(
      'dashlet1' => array(
       'url' => 'http://1.1.1.1:3333/stuff/stuff?36', 
       'height' => '200', 
       'width' => '450' 
      ), 
      'dashlet2' => array(
       'url' => 'http://1.1.1.1:3333/stuff/stuff?37', 
       'height' => '200', 
       'width' => '450' 
       ...... 
       ...... 
       'width' => '450' 
      ), 
      'dashlet8' =>array(
      'url' => 'http://1.1.1.1:3333/stuff/stuff?35', 
      'height' => '200', 
      'width' => '350' 
      ) 
      ); 
    ?> 

並把回聲內循環:

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>" . "\n"; 
    } 

,並得到這樣的:

<iframe src="http://1.1.1.1:3333/stuff/stuff?36" height="200" width="450" frameborder="0"> <\iframe> 
<iframe src="http://1.1.1.1:3333/stuff/stuff?37" height="200" width="450" frameborder="0"> <\iframe> 
<iframe src="http://1.1.1.1:3333/stuff/stuff?311" height="200" width="450" frameborder="0"> <\iframe> 
<iframe src="http://1.1.1.1:3333/stuff/stuff?34" height="200" width="350" frameborder="0"> <\iframe> 
<iframe src="http://1.1.1.1:3333/stuff/stuff?38" height="200" width="450" frameborder="0"> <\iframe> 
<iframe src="http://1.1.1.1:3333/stuff/stuff?310" height="200" width="450" frameborder="0"> <\iframe> 
<iframe src="http://1.1.1.1:3333/stuff/stuff?33" height="200" width="450" frameborder="0"> <\iframe> 
<iframe src="http://1.1.1.1:3333/stuff/stuff?35" height="200" width="350" frameborder="0"> <\iframe> 
+0

正如我所說,我試過了,它產生的只不過是陣列中列出的第一個值... –

+0

在你的Dashlets.php中,你有沒有? –

+0

是的,我在開頭有<?php,我沒有用?>關閉文件,因爲只有它列出的數組。我更新了原始帖子以反映該div的所有內容。爲了確保沒有混淆,我還將回聲聲明移至循環中。 –