doushi9780 2012-02-11 02:03
浏览 35
已采纳

数据库查询显示在视图中而不是$ data

The below code seems to be showing in the "view" and not via $data as it should be (I have not echoed the $data['companyName'] into my view yet but $data['pageTitle'] works fine).

Issue:

enter image description here

Model:

function companyName()
    {
        $companyName = $this->db->query("SELECT company_name FROM core");

        if ($companyName->num_rows() > 0)
        {
            foreach ($companyName->result() as $row)
            {
                echo $row->company_name;
            }
        }

Controller:

public function index()
{
    $data['companyName'] = $this->core_model->companyName();
    $data['pageTitle'] = "Admin Login";
    $this->load->view('admin/assets/header', $data);
    $this->load->view('admin/login.php');
    $this->load->view('admin/assets/footer');
}
  • 写回答

1条回答 默认 最新

  • dqp21271 2012-02-11 02:07
    关注

    By echoing something in the model, that echo happens when the model is processed, which is way before the view is even a twinkle in its controller's eye.

    Instead, you should do something like this in your model:

    function companyName()
    {
        $companyName = $this->db->query("SELECT company_name FROM core");
    
        if ($companyName->num_rows() > 0)
        {
            $company_names = '';
            foreach ($companyName->result() as $row)
            {
                $company_names .= $row->company_name;
            }
        }
        return $company_names;
    }
    

    Then you can pass $data into the view and the string will be ready to echo out like you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?