我真的希望有人可以帮助我!
我正在尝试创建一个pdf文件,其中包含我从查询中获得的一些数据,pdf创建者位于控制器中 .
这是我的模型:
function assistiti()
{
$user = $this->ion_auth->user()->row();
$userId = $user->id;
$query = $this->db->get_where('assistiti', array('assistito_da' => $userId));
return $query->result();
}
我从控制器打来的电话:
$data['assistiti'] = $this->assistiti_m->assistiti();
在同一个控制器中,我使用foreach循环数据创建pdf文件:
foreach($data as $post){
$nome = $post->nome;
$cognome = $post->cognome;
$tribunale = $post->tribunale;
$tbl .= '<tr><td style="border:1px solid #000;text-align:center">'.$nome.'</td>';
$tbl .= '<td style="border:1px solid #000;text-align:center">'.$cognome.'</td>';
$tbl .= '<td style="border:1px solid #000;text-align:center">'.$tribunale.'</td> </tr>';
;}
$pdf->writeHTML($tbl_header.$tbl.$tbl_footer , true, false, false, false, '');
显然它不起作用......它给我一个经典的 Trying to get property of non-object
,我发现返回一些数据的唯一方法是将数组索引放入:
$nome = $post[0]nome;
$cognome = $post[0]->cognome;
$tribunale = $post[0]->tribunale;
但显然它只返回第一行 .
这是 $data
的print_r:
Array ( [assistiti] => Array ( [0] => stdClass Object ( [id_assistiti] => 1 [nome] => Matte [cognome] => Dama [luogo_nascita] => Milano [data_nascita] => 1986-10-01 [residenza] => Milano [dimora] => Milano [telefono] => 545431453143 [tribunale] => Milano) [1] => stdClass Object ( [id_assistiti] => 2 [nome] => dario [cognome] => vozzi [luogo_nascita] => 0 [data_nascita] => 0000-00-00 [residenza] => pesaro [dimora] => [telefono] => [tribunale] => ancona ) ) )
我怎样才能得到所有条目?
先感谢您!
2 回答
你有
Array of stdClass Objects
. 您可以访问它们:您将模型查询结果保存在数组$ data中的特定键(assistiti)中,然后在foreach循环中获取整个数组 . 这样,你必须遍历数组$ data中的特定数组: