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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀