【问题标题】:CodeIgniter pass variable from controller to viewCodeIgniter 将变量从控制器传递到视图
【发布时间】:2015-03-24 09:01:03
【问题描述】:

抱歉,如果这个问题已经得到解答,但我找不到答案。 我想将一个变量从我的模型传递给控制器​​,最后在视图中显示。

我对数据库执行查询,然后计算我的结果(通过 num_rows)。然后我返回结果。

public function countHighRisk(){
    $this->db->select("ares_status");
    $this->db->from("tbl_alert_result");
    $this->db->join('tbl_status_standards', 'ares_status = sts_status_id');
    $this->db->where('sts_status_risk', 3);
    $query = $this->db->get();

    return $query->num_rows();
}

之后,我将结果放入一个数组并将该数组传递给我的视图。

public function index()
{
    $this->load->model('Alert_result_model');
    $data['high'] = $this->Alert_result_model->countHighRisk();

    $this->load->view('templates/head');
    $this->load->view('templates/menu');
    $this->load->view('pages/Dashboard', $data);
    $this->load->view('templates/footer');
}

当我尝试在视图中显示变量时出现错误。

<div class="col-xs-8 text-right">
     <span> High Risk </span>
     <h2 class="font-bold"><span class="count"><?= $high; ?></span></h2>
</div>

错误:http://imgur.com/iHkYHiI

提前致谢。

【问题讨论】:

  • 检查值是通过 print_r($data['high']);退出;
  • 你能检查$this-&gt;Alert_result_model-&gt;countHighRisk();是否返回正确的变量
  • 回声 $data['high'];在数据库调用之后...是打印任何值吗?
  • 您的查询是否返回任何内容?首先检查您的查询是否正确?在$query下面试试echo $this-&gt;db-&gt;last_query();exit;
  • 谢谢你们的快速响应。显然代码没有任何问题。我刚刚重新启动了我的本地主机,现在它可以工作了!

标签: php arrays codeigniter model-view-controller


【解决方案1】:

您在这里调用视图的整个想法是错误的。请按如下方式使用:

控制器:

public function index()
{
    $this->load->model('Alert_result_model');
    $data['high'] = $this->Alert_result_model->countHighRisk();

    $this->load->view('pages/Dashboard', $data);
}

查看:

<? $this->load->view('templates/head'); ?>
<? $this->load->view('templates/menu'); ?>
<div class="col-xs-8 text-right">
     <span> High Risk </span>
     <h2 class="font-bold"><span class="count"><?= $high; ?></span></h2>
</div>
<? $this->load->view('templates/footer'); ?>

还可以通过 print_r($this-&gt;Alert_result_model-&gt;countHighRisk();) 检查 $this-&gt;Alert_result_model-&gt;countHighRisk(); 实际返回的内容

【讨论】:

  • 谢谢,但问题已经解决。我的代码没有错误。我刚刚重新启动了本地主机。但是你是说我必须在一个视图中调用其他视图吗?在 codeigniter 的文档中,他们说您必须在控制器中调用多个视图。 ellislab.com/codeigniter/user-guide/general/views.html
  • 我不太喜欢代码点火器 documentation,但是,是的,当您在控制器中一遍又一遍地调用 3 个静态行时,它有点混乱,所以为什么不只是而是在视图中调用它们。记住文档不是word of God :)
  • 您分享的文档只是一个示例,在 90% 的情况下它在架构上是不正确的。
  • 谢谢,我同意它看起来有点乱。所以我会像你说的那样做:)
【解决方案2】:

您可以将视图加载到控制器中,但在下面这样做

class Dashboard extends CI_Controller {
 public function index() {

  $this->load->model('Alert_result_model');
  $data['high'] = $this->Alert_result_model->countHighRisk();

  // Choose only one header examples:
  $data['header'] = $this->load->view('header', Null, TRUE); // If No Data
  $data['header'] = $this->load->view('header', $data, TRUE); // If Data

  // Choose only one Footer examples:
  $data['footer'] = $this->load->view('footer', Null, TRUE); // If No Data
  $data['footer'] = $this->load->view('footer', $data, TRUE); // If Data

  $this->load->view('dashboard', $data);
 }
}

仪表板视图

<?php echo $header;?>

<?php foreach ($high as $low) { ?>

   <?php echo $low['something'];?>

<?php }?>

<?php echo $footer;?>

如果您有一个单独的页眉控制器和页脚控制器,如果您对用户指南感到困惑,那么您将需要 HMVC。CI3 中默认用户指南的右上角是切换到更清洁的版本。

【讨论】:

【解决方案3】:

请改一下

$this->load->view('pages/Dashboard', $data);

进入

$this->load->view('pages/Dashboard', $data, true);

现在它应该可以工作了。

【讨论】:

  • 谢谢,但这不是必需的,我刚刚重新启动了我的本地主机,现在它工作得很好。
猜你喜欢
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 2017-03-27
  • 2020-02-17
相关资源
最近更新 更多