Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 来自查询的Codeigniter自定义输出数组_Database_Arrays_Codeigniter_Loops_Model - Fatal编程技术网

Database 来自查询的Codeigniter自定义输出数组

Database 来自查询的Codeigniter自定义输出数组,database,arrays,codeigniter,loops,model,Database,Arrays,Codeigniter,Loops,Model,我的模型中有一个函数,用于从数据库中选择项并将其作为数组返回。我的问题是,如何手动创建数组,以便更改日期格式 以下是我所拥有的: $sql = 'SELECT * FROM prayer_requests ORDER BY id DESC'; $query = $this->db->query($sql); if($query->num_rows() > 0) return $query->result(); els

我的模型中有一个函数,用于从数据库中选择项并将其作为数组返回。我的问题是,如何手动创建数组,以便更改日期格式

以下是我所拥有的:

$sql = 'SELECT * FROM prayer_requests ORDER BY id DESC';
        $query = $this->db->query($sql);

        if($query->num_rows() > 0) return $query->result();
        else return FALSE;

我猜我需要某种环?我知道它一定很简单,但我不知道怎么做。

如果你想得到一个数组的结果,你可以使用
result\u array()
而不是
result()
。 您可以在此处阅读有关使用CodeIgniter生成结果的更多信息:

例:

编辑: 如果要在将数据放入数组之前修改数据。您需要一个循环,在循环中,使用任何必要的函数来更改所需的数据。然后将其分配回循环变量,并将其附加到数组(在循环之前已经初始化)


第一件事:您的模型将返回结果,控制器将完成工作,例如:

//Picking data
$this->data['resultados']           = $this->outros_detalhes_model->consulta();
$rows = array();

//Running all data, modifying whatever you need
foreach($this->data['resultados'] as $row){
   $row->CT_TIPO = $this->funcoes->mostra_tipo_detalhe($row->CT_TIPO);
   $rows[] = $row;
}

//Sending data to view
$this->data['resultados'] = $rows;
$this->load->view('includes/estrutura',$this->data);

我意识到了这一点,但如何在将数据放入数组之前修改数据?看起来不错,但有一些问题。我做错什么了吗?foreach($query->result_array()as$row){$row['date']=standard_date($row['date');$rows[]=$row;}return$row;@Adam我觉得你的代码很好。但是你能解释一下有什么问题吗?它没有返回任何东西?或者可能有错误消息?还有,
$row['date]的原始内容是什么
?记住,
standard_date()
的第二个参数必须是Unix时间戳。我只是得到一个空白的白色屏幕,这意味着有些东西坏了。我用以下内容替换了日期格式:$row['content']=“hey”;我仍然得到一个空白屏幕:/Fixed!!我不得不用$row->date替换$row['date'],再次感谢!
$rows = array();
foreach($query->result_array() as $row)
{
    $row['some_key'] = change_data($row['some_key');
    $rows[] = $row;
}
//Picking data
$this->data['resultados']           = $this->outros_detalhes_model->consulta();
$rows = array();

//Running all data, modifying whatever you need
foreach($this->data['resultados'] as $row){
   $row->CT_TIPO = $this->funcoes->mostra_tipo_detalhe($row->CT_TIPO);
   $rows[] = $row;
}

//Sending data to view
$this->data['resultados'] = $rows;
$this->load->view('includes/estrutura',$this->data);