0

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

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
  • What would you like to be the result of the PHP code? Commented Feb 16, 2017 at 22:07

1 Answer 1

1

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

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>
Sign up to request clarification or add additional context in comments.

7 Comments

As I have stated, I tried that and it produced nothing more than the first value listed in the array...
In your Dashlets.php, do you have the <?php ?> ?
Yes, I have <?php at the beginning and I do not close the file with ?> since there is only the array listed in it. I updated the orignal post to reflect everything for that div. I also moved my echo statement to be inside the loop just to ensure there is no confusion.
I changed a couple of \" too, and added a \n at the end of the echo, but that might not be a problem.
Unsure why I'm getting a different result. I just tried moving the loop to my dashlets.php and I'm still only showing the first of the array items.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.