【问题标题】:How to flush output using Codeigniter 3?如何使用 Codeigniter 3 刷新输出?
【发布时间】:2015-11-23 10:11:21
【问题描述】:

我正在写一个CodeIgniter 3 应用程序。我的目标是有一个视图,它不断地(随着代码的进行)随着输出内容刷新。我在 stackoverflow 中阅读了一些答案,但我不确定该怎么做。

我有一个控制器,它在更新方法中呈现视图。

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

?>

这是“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);
        }
    }
}

?>

然后我有一个视图,它输出内容。

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

这很好用。到目前为止。

现在,我尝试实现以下目标:

  • 我点击“Ausführen”(执行)按钮,将表单提交到与此页面相同的 url
  • 然后我想执行一个 php 脚本,它不断地向页面输出内容。不是一次所有内容,而是在输出到页面后添加输出。

我希望我说清楚了。

关于如何做到这一点的任何建议或教程?

【问题讨论】:

  • 尝试添加一些你到现在为止写的代码,因为我的英语很差,不清楚你的需要
  • 我投票结束这个问题作为题外话,因为建议看看Codeigniter Tutorial for Beginners
  • @Abdulla 感谢您提供教程链接。我很快浏览了这些视频,但我找不到我的问题的答案。也许我错过了,或者我一开始没有说清楚。

标签: php codeigniter codeigniter-3


【解决方案1】:

您需要通过在output class 上调用-&gt;_display 方法来强制输出

例如,对于 JSON 输出,您将执行以下操作:

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

加载视图时也是如此,如果您 100% 确定不需要其他任何东西,您可以手动调用 _display 来获取输出;之后一定要调用 exit 否则你会得到一个重复的输出(一个来自你的手动调用,另一个来自自动调用)。

查看输出类的用户指南:http://www.codeigniter.com/user_guide/libraries/output.html

【讨论】:

  • 当您说“输出类”时,您指的是什么类?视图还是控制器?我不明白。
  • 我想到了这样的东西,就在CI3中编程:stackoverflow.com/questions/10579116/…
  • 输出类是 CodeIgniter 使用的核心类,它负责向浏览器显示最终输出……我给你的代码应该适合你;另请参阅我提供的文档 URL。
  • 这不是 OP 想要的:which is constantly (as the code goes along) flushed with the output content.。在第一次调用后终止进程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
  • 2021-02-16
  • 2016-01-29
  • 2015-04-09
  • 1970-01-01
相关资源
最近更新 更多