数据未解析为使用模板渲染codeigniter查看

时间:2016-09-25 08:43:21

标签: php codeigniter

我在控制器中有一个来自查询的结果数组,例如:

[...] controller / Admin.php:

public function user_email()
{
    $this->db->select('user_email');
    $this->db->from('user');
    $query = $this->db->get();
    $data['email'] = $query->result_array();

    $this->template->render('admin/user_driver', $data, 'admin');
}

libraries / Template.php:

class Template {
protected $ci;
public $data;

public function __construct()
{
    $this->ci =& get_instance();
}

public function render($view, $viewdata = null, $template = 'frontend')
{
    $html = $this->ci->load->view($view, $viewdata, TRUE);
    $this->data['template'] = $this->parse_blocks($html);
    $this->data['_viewdata'] = $viewdata;
    $this->ci->load->view("template/{$template}", $this->data);
}
public function parse_blocks($html)
{
    $blocks = array();
if(empty($html)) $html = " ";
libxml_disable_entity_loader(false);
$dom = new DOMDocument;
libxml_use_internal_errors(true); 
$dom->loadHTML($html);
libxml_use_internal_errors(false);
    libxml_disable_entity_loader(true);
    foreach ($dom->getElementsByTagName('template') as $node) {
        $section = $node -> getAttribute('block');
        $blocks[$section] = $this->DOMinnerHTML($node);
    }
    return $blocks;
}
private function DOMinnerHTML(DOMNode $element) 
{ 
    $innerHTML = ""; 
    $children  = $element->childNodes;

    foreach ($children as $child) 
    { 
        $innerHTML .= $element->ownerDocument->saveHTML($child);
    }

    return $innerHTML; 
} 
public function block_exists($blockname = NULL)
{
    if(empty($blockname))
        return false;

    return (is_array($this->data) && is_array($this->data['template']) && array_key_exists($blockname,$this->data['template']));
}
}

var_dump($email)视图中的user_driver.php,但它会抛出Undefined variable: email。文件user_driver.php位于[...]views/admin/user_driver.php

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的数据库结果当前正作为多维数组结果返回。如果您只期望一个结果,请将其更改为$data['email'] = $query->row_array();

  

$this->db->select('user_email'); $this->db->from('user'); $query = $this->db->get(); $dbresult = $query->row_array(); $emailAddress = $dbresult["user_email"]; $data['email'] = $emailAddress;

这将为您提供从以下位置获取的单维数组:

{{1}}