doujing6436 2017-02-16 21:36
浏览 47

从关联数组格式化URL

I'm attempting to format an iframe from urls, heights, widths, etc put in an array. Here is the code I have so far.

               <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>

Here is my array from 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'
    )
);

My problem is that the loop is only pulling either the first value from the array when inside the loop and only the last value if its placed outside the loop brackets as shown below. I am attempting to format multiple urls from the array items and have them placed in an iframe horizontal to eachother. Is there something I'm doing wrong?

Edited to add full code, including html divs.

Just moved everything to dashlets.php as suggested. I'm still only getting a return result from the first array item.

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

and in my main.php, which is viewable from my index.php viewport.

            <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>
  • 写回答

1条回答 默认 最新

  • drhzc64482 2017-02-16 21:38
    关注

    I just try your code, just added into the 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'
                )
                );
        ?>
    

    And put the echo inside the loop:

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

    And got this:

    <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line