gpt4 book ai didi

php - Codeigniter 将查询加载到数组中

转载 作者:行者123 更新时间:2023-11-29 00:11:19 25 4
gpt4 key购买 nike

我一直在尝试弄清楚如何将查询中的数据加载到数组中。

$query->row() //only brings back a single row of data when there are more entries in the database. 

如果我只使用 foreach 循环并在下面的模型代码中回显,数据只会显示在屏幕上。它不在变量或数组中。它只是屏幕上挤在一起的文本。

我真的很难找到一个代码示例来告诉我如何使用

$this->db->get_where('table' array('column' => $var);

我终于找到了它,但是 codeigniters 网站上的示例只是将查询回显到屏幕上。

http://ellislab.com/codeigniter/user-guide/database/results.html

这对生产没有用。我的 Controller 代码是:

    public function record(){ 
/*
* Here the id is being passed to the record
* function to retieve the parent and childrens data
*
*/
$getid['id'] = $this->uri->segment(3);
$accld = $getid['id'];

$data = array();

$this->load->model('account');
$account = new Account();
$account->load($getid['id']);
$data['account'] = $account;

$this->load->model('children');
$children = new Children();
$children->accld($getid['id']);
$data['children'] = $children;

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

}


}

我的模型代码是这样的:

public function accld($id) 
{
$query = $this->db->get_where($this::DB_TABLE, array('accId' => $id));

$c_data = array();
foreach ($query->result() as $row){
$c_data[] = $row ;
}
return $c_data;

/*
* Note:
* I need to figure out how to load to an array to pass back to the
* controller to pass to the display
* I can echo to the screen the results but that is uncontrolled.
*
*/
}

如果我这样做:

public function accld($id) 
{
$query = $this->db->get_where($this::DB_TABLE, array('accId' => $id));

foreach ($query->result() as $row){
echo $row->id ;
// and all the other fields below here
}

}

我的行被回显到屏幕上。但是没有控制。因此,我们将不胜感激任何有助于控制我的数据的帮助。

回答

这是最终返回所有结果而不仅仅是一行的原因。

/**
* Populate from an array or standard class.
* @param mixed $row
*/
public function populate($row) {
foreach ($row as $key => $value) {
$this->$key = $value;
}
}

public function accld($id) {

$query = $this->db->get_where($this::DB_TABLE, array('accId' => $id));

$this->populate($query->result());
}

最佳答案

就这样

$query = $this->db->get_where($this::DB_TABLE, array('accId' => $id));
$_array = $query->result_array();

用 $_array 做任何事。

关于php - Codeigniter 将查询加载到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24897311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com