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>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入