CodeIgniter不应该附加到视图的开头

时间:2012-02-15 18:19:39

标签: php codeigniter

当我调用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";
                }
       }
}

2 个答案:

答案 0 :(得分:1)

您的问题似乎是在echo内使用regularDashboard()。尝试设置包含form标记的变量,然后将其返回,而不是使用echo

以下是一个例子:

function regularDashboard()
{
    $html  = "";
    $html .= "<form>";

    //Append the rest of the form markup here

    return $html;
}

答案 1 :(得分:1)

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

您不应该这样(发送直接输出/回显视图之外的东西),一旦您使用与标题相关的任何内容(重定向,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,你就可以正确输出所有字符串。

编辑: 以上所有内容如果您在控制器内部。如果您在模型中,只需将所有内容分配给变量并将其返回给Controller:

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

如果你允许我,我建议将表单直接写入视图,而不会产生一些麻烦(和结构错误),这些东西应该是视图的“演示”。