Vinod VT
Vinod VT

Reputation: 7159

How to load a codeigniter view in iframe

I try to load a codeigniter view in iframe as, <iframe src="<?php $this->load->view('lists');?/>"></iframe> But the page could not load. With out i frame its working. How do i load this view in iframe ?

Upvotes: 0

Views: 19283

Answers (4)

Gaius
Gaius

Reputation: 11

I wanted to tile a site with several views, each in their own iframe, this is what I did:

The controller, book.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Book extends CI_Controller {


    public function index(){
        $this->shell();
    }


    public function shell(){

        $data['title'] = "Home";
        $data['frames'] = array(
            "book/events"
            ,"book/sales"
            ,"book/purchases"
            ,"book/cashflows"
        );

        $this->load->view("shell", $data);

    }


    public function events(){
        $this->load->view("events");
    }

    public function sales(){
        $this->load->view("sales");
    }

    public function purchases(){
        $this->load->view("purchases");
    }

    public function cashflows(){
        $this->load->view("cashflows");
    }


}

The view shell.php contains a foreach statement letting me pass in tiles dynamically. Note how the url's to each site are written in the controller above ^^ (eg. "book/events").

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title><?php echo $title ?></title>     
    </head>
    <body>

        <?php foreach ($frames as $frame):?>
            <iframe src="<?php echo $frame?>"></iframe>
        <?php endforeach;?>

        <div class="footer">
            Page rendered in <strong>{elapsed_time}</strong> seconds</p>
        </div>
    </body>
</html>

Then each tile is simply it's own site, like so:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Events</title>       
    </head>
    <body>
        <div class="header">Events</div>
        <div class="content"> Bla bla bla you have no money</div>
        <div class="footer">
            Page rendered in <strong>{elapsed_time}</strong> seconds</p>
        </div>
    </body>
</html>

Upvotes: 1

edwin
edwin

Reputation: 101

create a function in your controller

//project.php

function view_list()
{
$this->load->view('lists');
}

and call in the view page like this

<iframe src="<?php echo site_url('project/view_list');?>">>    </iframe>

Upvotes: 7

Hasan
Hasan

Reputation: 23

You can't assign plain html to iframe elements src attribute. You should have a controller that render your html and then set src attribute to that controller.

<iframe src="<?php echo base_url( 'controller_that_render_list_html' ) ?>"></iframe>

Upvotes: 1

Kees Sonnema
Kees Sonnema

Reputation: 5784

This is because the iframe is on another page? you haven't specified the controller function on that page so it doesn't recognize the view. i think you have to put the controller date from your view into the function for the view you have your iframe in, so lik this.

function list_view()
{
//functions for your listview
}

function viewforiframe()
{
//function for you view where the iframe is located
+
//functions for your listview
}

NOTE: the answer Deon gave is also a thing to look at. without a good link you could never see the view.

Upvotes: 0

Related Questions