CodeIgniter在视图开始时附加它应该';不要


CodeIgniter appending to the beginning of the view when it shouldn't be

当我调用regularDashboard()时,它会附加到视图的开头。在我看来,我是在格式化样式内部调用$reg。因此,它不应该在视图的开头出现。。。有什么关于为什么会发生这种情况的想法吗?

public function dcr() {
        // pass all dashboard accesses through this function
        $username = $this->session->userdata("username");
        $query = $this->db->get_where('users', array('username' => $username));
        $userType = $this->session->userdata('userType');
        if ($userType == 'regular') {
                foreach ($query->result() as $row) {
                    $data = array('reg' => $this->regularDashboard(), 'firstname' => $row->firstname);
                    $this->load->view('dashboard', $data);
} public function regularDashboard () {
            $userid = $this->session->userdata('userid');
            $results = $this->db->query("SELECT * FROM users");
            foreach ($results->result() as $row) {
                if($userid != $row->userid) {
                    echo $row->firstname . " " . $row->lastname;
                    echo "<form method='GET' action='processing/lib/process-send-friend-request.php?'>";
                    echo '<input name="accepted" type="submit" value="Send User Request" /><br />';
                    echo '<input name="AddedMessage" placeholder="Add a message?" type="textbox" />';
                    echo '<br>Select Friend Type: ' . '<br />Full: ';
                    echo '<input name="full_friend" type="checkbox"';
                    echo '<input type="hidden" name="id" value="' . $row->idusers . '" />';
                    echo '</form>';
                    echo "<br /><hr />";
                } elseif ($userid == $row->userid) {
                    echo $row->firstname . " " . $row->lastname;
                    echo "<br />";
                    echo "You all are currently friends";
                }
       }
}

视图是缓冲的。当您直接在控制器中回显某个内容时,它会在刷新缓冲区之前发送(因此,在将包含视图的输出发送到浏览器之前),这就是为什么它会出现在任何内容之前。

你不应该这样做(发送直接输出/回显视图之外的内容),一旦你使用了与标头相关的任何东西(重定向、cookie、CI会话…),你就有陷入麻烦的风险

更新:

要修复它,只需将所有这些字符串分配给一个变量(如jeff所示),并将其发送到视图:

$data['form'] = $row->firstname . " " . $row->lastname;
$data['form'] .= "<form method='GET' action='processing/lib/process-send-friend-request.php?'>";
$this->load->view('formview',$data);

在那里,您只需回显$form,就可以正确输出所有字符串。

编辑:如果你在一个控制器里,以上都是。如果你在一个模型中,只需将所有内容分配给一个变量并将其返回给控制器:

function regularDashboard()
{
  $form = $row->firstname . " " . $row->lastname;
  $form .= "<form method='GET' action='processing/lib/process-send-friend-request.php?'>";
  return $form;
}

在控制器中:

$data['form'] = $this->model->regularDashboard();
$this->load->view('formview',$data);

如果你允许的话,我建议直接将表单写入视图,而不需要创建应该是";演示文稿";视图的外侧。

您的问题似乎是在regularDashboard()中使用echo。尝试设置一个包含form标记的变量并返回它,而不是使用echo

这里有一个例子:

function regularDashboard()
{
    $html  = "";
    $html .= "<form>";
    //Append the rest of the form markup here
    return $html;
}