dshakcsq64956
2014-07-29 18:38
浏览 77
已采纳

TCPDF - Codeigniter - 在控制器中获取数据

I really hope that someone can help me!

I'm trying to create a pdf file with some data that I get from a query, the pdf creator is in the controller.

Here is my model:

function assistiti()
{
    $user =  $this->ion_auth->user()->row();
    $userId = $user->id;
    $query = $this->db->get_where('assistiti', array('assistito_da' => $userId));
    return $query->result();
}

That I call from the controller:

$data['assistiti'] = $this->assistiti_m->assistiti();

In the same controller I create the pdf file with a foreach loop through data:

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, '');

and obviously it does't work...it returns to me a classic Trying to get property of non-object and the only way that I found to return some data is putting the array index like that:

$nome = $post[0]nome;
$cognome =  $post[0]->cognome;
$tribunale = $post[0]->tribunale;

but obviously it returns me only the first row.

Here is a print_r of $data:

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 ) ) )

How can I get all the entries???

Thank you in advance!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsag14654 2014-07-29 19:09
    已采纳

    You have an Array of stdClass Objects. You can access them as:

    foreach($data as $post)
    {
        foreach($post as $p)
        {
            $tbl .= '<tr><td style="border:1px solid #000;text-align:center">'.$p->nome.'</td>'; 
            $tbl .= '<td style="border:1px solid #000;text-align:center">'.$p->cognome.'</td>';
            $tbl .= '<td style="border:1px solid #000;text-align:center">'.$p->tribunale.'</td>    </tr>';
        }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongyan7988 2014-07-29 20:34

    You are saving your model query results in a specific key(assistiti) inside the array $data, and then you are fetching the whole array in the foreach loop. This way, you got to loop through the specific array, inside the array $data:

    foreach($data['assistiti'] 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, '');
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题