CodeIgniter不会在页面之间发送数据

时间:2016-12-14 08:08:49

标签: php codeigniter

模型Mod_about

 public function getContent()
{
    $query=$this->db->get('about');
    return $query->result();
}
public function getMainContent()
{ 
    $this->db->get('about');
    $this->db->where('about_id', '1'); 
}

控制器

    public function index(){
    $this->load->model('Mod_about');        

    $contents['content']=$this->Mod_about->getContent();        
    $maincontent=$this->Mod_about->getMainContent();

    $this->load->view('_header');
    $this->load->view('_signin');
    $this->load->view('_header2');
    $this->load->view('about',$contents,$maincontent);
    $this->load->view('_footer');
}

查看about.php

<h3><?php 
        echo $maincontent."<br>"  ?>
</h3>
<h2>
       <?php 
        foreach($content as $cont){

        echo $cont->ceo_name."<br>";} ?>

  </h2>

您好, getContent()函数工作正常,但它总是在getMainContent()函数中出现错误,该函数显示“Undefined variable:maincontent”

1 个答案:

答案 0 :(得分:1)

希望以下代码可以帮助您..

模型

<?php

public function getContent()
{
    $query=$this->db->get('about');
    return $query->result();
}

public function getMainContent()
{ 
    $this->db->where('about_id', '1'); 
    $query=$this->db->get('about');
    return $query->row();
}

控制器

public function index(){
    $this->load->model('Mod_about');        

    $data['content']=$this->Mod_about->getContent();        
    $data['maincontent']=$this->Mod_about->getMainContent();

    $this->load->view('_header');
    $this->load->view('_signin');
    $this->load->view('_header2');
    $this->load->view('about',$data);
    $this->load->view('_footer');
}

查看

<h3>

<?php 

echo $maincontent->attribute1."<br>"; 

// attribute1 is one the attribute in maincontent row..(see print_r($maincontent);)

?>

</h3>

<h2>
  <?php 
    foreach($content as $cont){
      echo $cont->ceo_name."<br>";
    } 
  ?>
</h2>