duanpendan8067 2013-10-06 05:16
浏览 41
已采纳

模型在Codeigniter中的视图之前加载

I have a controller in codeigniter pages.php

class Pages extends CI_Controller {

    public function view($page = 'home')
    {
        $this->load->view('header');
        echo("<br>");
        $this->load->model("math");
        echo $this->math->add(1,2);
    }
}

The model: math.php

class math extends CI_Controller
{
    public function add($val1,$val2){
        return $val1+$val2;
    }
}

The view: header.php

<html>
<head>
    <title>fashion and style</title>
</head>
<body>
<?php
    echo("this is the header");
?>

As per the controller the code should output:

this is the header

number

but i get the output like:

number this is the header

Why?

  • 写回答

2条回答 默认 最新

  • dshdsh2016 2013-10-06 06:50
    关注

    If you will echo the string from the codeignitor controller directly, it will render the string before rendering the loaded views. If you want to do so, you can try like-

    $str = $this->math->add(1,2);
    $this->output->append_output($str);
    

    Then your controller will look like-

    class Pages extends CI_Controller {
    
        public function view($page = 'home')
        {
            $this->load->view('header');
            $this->load->model("math");
            $str = "<br>".$this->math->add(1,2);
            $this->output->append_output($str);
        }
    }
    

    Hope this will help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)