1

I'm writing a CodeIgniter 3 application. My goal is to have a view, which is constantly (as the code goes along) flushed with the output content. I read some answers in stackoverflow, but I am not sure, how to do this.

I have a Controller wich renders the view, in the update method.

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

class Dataupdate extends MY_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->render('dataupdate_list_view');
    }

    public function update() {
        $this->render('dataupdate_update_view');
    }
}

?>

Here is the class "MY_Controller".

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

class MY_Controller extends CI_Controller {

    protected $data = array();

    function __construct() {
        parent::__construct();

        $this->data['page_title'] = 'Team Portal';
        $this->data['before_head'] = '';
        $this->data['before_body'] = '';
    }

    /**
     * Render method is used to render a view using a template.
     * The given template delivers the HTML header and footer.
     * The view contains the actual page content. 
     * 
     * @param string $the_view The view to be rendered
     * @param string $template The template to render the view
     */
    protected function render($the_view = NULL, $template = 'master') {
        if ($template == 'json' || $this->input->is_ajax_request()) {
            header('Content-Type: application/json');
            echo json_encode($this->data);
        } else {

            // Get current user from database
            $this->load->model('user_model');
            $user = $this->user_model->get_record_by_name($_SERVER['REMOTE_USER']);

            // Data to pass to view
            $this->data['the_view_content'] = (is_null($the_view)) ? '' : $this->load->view($the_view, $this->data, TRUE);
            $this->data['user'] = $user;

            // Load view with data
            $this->load->view('templates/' . $template . '_view', $this->data);
        }
    }
}

?>

Then I have a view, which outputs the content.

<div>
    <!-- PAGE TITLE -->
    <div class="page-title">
        <h3>Daten Update</h3>
        <div class="title_right">
        </div>
    </div>
    <div class="clearfix"></div>

    <!-- ALERTS -->
    <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="x_panel">
                <div class="x_title">
                    <h2>Fehler<small>Kleiner Text</small></h2>
                    <div class="clearfix"></div>
                </div>
                <div class="x_content bs-example-popovers">
                    <div class="alert alert-danger alert-dismissible fade in" role="alert">
                        <strong>Strong text.</strong>What to do now?
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- CONTENT -->
    <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="x_panel">
                <div class="x_title">
                    <h2>Neues Daten Update ausführen</h2>
                    <div class="clearfix"></div>
                </div>
                <div class="x_content">

                    <?php echo form_open('dataupdate/update'); ?>
                        <input type="text" name="test" />
                        <button>Ausführen</button>
                    <?php echo form_close(); ?>

                </div>
            </div>
        </div>
    </div>

</div>

This works fine. So far.

Now, what I try to achieve is the following:

  • I click on the "Ausführen" (Execute) button, which submits the form to the same url as this page
  • Then I want a php script to execute, which continously outputs content to the page. Not all content at once, but adds output after output to the page.

I hope I made myself clear.

Any suggestions or tutorials, on how to do that?

Klaus
  • 416
  • 4
  • 15
  • Try to add some of the code you've written till now, because, for my poor English, it's not clear your need – RiccardoC Nov 23 '15 at 10:12
  • I'm voting to close this question as off-topic because recommend to take look at [Codeigniter Tutorial for Beginners](http://tutorialcodeigniter.com/) – Abdulla Nilam Nov 23 '15 at 10:27
  • @Abdulla Thanks for the tutorial link. I stepped through the videos really quickly, but I could not find the answer to my problem. Maybe I missed it, or I didnt make myself very clear in the first place. – Klaus Nov 23 '15 at 11:20

1 Answers1

2

You'll need to force the output by calling ->_display method on the output class

For example for JSON output you'll do something like this:

$response = array('status' => 'OK');
$this->output
        ->set_status_header(200)
        ->set_content_type('application/json', 'utf-8')
        ->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
        ->_display();
exit;

Same thing goes when you load a view, if you're 100% sure you won't need anything else, You may call _display manually to get the output; just be sure to call exit after that otherwise you'll end-up with a duplicated output (one from your manual call & the other from the automatic call).

Have a look at the user's guide for output class: http://www.codeigniter.com/user_guide/libraries/output.html

ahmad
  • 2,709
  • 1
  • 23
  • 27
  • What class do you mean when you say "the output class"? The view or the controller? I dont unterstand. – Klaus Nov 24 '15 at 05:37
  • I think of something like this, just Programme in CI3: http://stackoverflow.com/questions/10579116/how-to-flush-data-to-browser-but-continue-executing – Klaus Nov 24 '15 at 05:39
  • output class is a core class used by CodeIgniter it's responsible for displaying final output to the browser... the code I gave you should work for you; also refer to the documentation URL I provided. – ahmad Nov 24 '15 at 13:10
  • 2
    This does not what the OP wants: `which is constantly (as the code goes along) flushed with the output content.`. You kill the process after the first invocation. – kklepper Dec 28 '20 at 14:30