【问题标题】:Codeigniter Fetch dataCodeigniter 获取数据
【发布时间】:2018-09-18 23:17:45
【问题描述】:

我正在尝试从我的数据库中获取数据并将其显示在我的视图中。 但我似乎没有做对。我使用 codeIgniter 做了一些工作,我知道您可以在视图中以如下方式显示您的数据:

{data}{dataname}{/data}

这会显示数据名的每条记录。

这是我的模型:

public function __construct()
{
    $this->load->database();
}   

public function Get_Pedras()
{
    $query=$this->db->query("SELECT * FROM pedras;");
    $row = $query->row_array();
    return $row;
}

这是我的控制器

public function __construct()
    {
            parent::__construct();
            $this->load->model('Pedras_Model');
            $this->load->helper('url_helper');
    }
public function index (){


    $this->load->view('Main/Formulario_Email');
}

public function GetStone(){
    $this->load->library('parser');
    $name = $this->Pedras_Model->Get_Pedras();

    $data = array(
        'title' => 'IdPedra',
        'heading' => 'NomePedra'
    );
    $this->load->view('Main/Formulario_Email', $data);
}

至少我的观点是:

<div class="col-sm-6 col-sm-offset-1">
                                    {data}
                                    {title}
                                    {/data}
                                    </div>

我想我可能遗漏了一些非常重要的东西,但我只在 CI 方面做了一点工作,前段时间,你能帮我吗?提前致谢!

【问题讨论】:

标签: php codeigniter


【解决方案1】:

在您的 Model 中不要使用 row_array,因为您正在查询表中的所有可用数据。请改用result_array

像这样……

public function Get_Pedras(){
$query=$this->db->query("SELECT * FROM pedras;");
return $query->result_array();

}

在你的控制器中试试这个...

public function GetStone(){
$this->load->library('parser');
$data['pedras'] = $this->Pedras_Model->Get_Pedras();

$this->load->view('Main/Formulario_Email', $data); //pass data here!

}

那么在您看来,为了显示数据这样做...

foreach($pedras as $p){
 echo $p['data']; //rename data with you query_name.

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2016-10-29
    相关资源
    最近更新 更多