adding the foreach field of the controller to the view

0

Hello! I have the following controller function.

	public function lancamento_listar()
	{
		$lista = $this->lancamento->obter_dados();
		$data = array();
		$no = $_POST['start'];
		foreach ($lista as $lancamento) {
			$no++;
			$row = array();
			$row[] = $lancamento->id;            
			$row[] = $lancamento->tipo == 1 ? '<span class="label label-success">'.'Receita'.'</span>' : '<span class="label label-danger">'.'Despesa'.'</span>';
			$row[] = date('d/m/Y H:i',strtotime($lancamento->dt_lancamento));
			$row[] = $lancamento->descricao;			
			$row[] = date('d/m/Y',strtotime($lancamento->dt_vencimento));			
			$row[] = 'R$ ' .number_format($lancamento->valor, 2, ',', '0');					
			$row[] = $lancamento->recebido == 1 ? '<span class="label label-success">'.'Pago'.'</span>' : '<span class="label label-warnning">'.'Pendente'.'</span>';
			
			//add html para a ação
			$row[] = '<a class="btn btn-flat btn-primary" href="javascript:void(0)" title="Editar" onclick="editar_lancamento('."'".$lancamento->id."'".')"><i class="fa fa-pencil"></i></a>
				      <a class="btn btn-flat btn-danger" href="javascript:void(0)" title="Excluir" onclick="excluir_lancamento('."'".$lancamento->id."'".')"><i class="fa fa-trash"></i></a>';
		
			$data[] = $row;
		}

		$saida = array(
						"draw" => $_POST['draw'],
						"recordsTotal" => $this->lancamento->lancamento_tudo(),
						"recordsFiltered" => $this->lancamento->lancamento_filtrado(),
						"data" => $data,
				);
		//Saída para o formato json
		echo json_encode($saida);
	}

And through the DateTable plugin, I'm returning the data from the database.

My question is, I want to add the postings and display them in a <tfoot> . For example:

$totalReceita = 0;

<tfoot>
    <tr>
        <td colspan="5" style="text-align: right; color: green"> <strong>Total Receitas:</strong></td>
    	<td colspan="2" style="text-align: left; color: green"><strong>R$ <?php echo number_format($totalReceita,2,',','.') ?></strong></td>
    </tr>    	
</tfoot>

This $totalReceita must be the sum of the values in (controller)
$row[] = 'R$ ' .number_format($lancamento->valor, 2, ',', '0');

How to do this calculation since the data is coming from ajax?

I can do this sum when the foreach is in the view itself, but I can not do this using the plugin DataTable and Ajax .

    
asked by anonymous 06.06.2017 / 18:53

1 answer

1

As André commented:

  

I would do a sum storage:    $total_receita += $lancamento->valor; within the loop, then display at the end $ total_receite. I would do it that way.

To add to the view, you add this variable to the array $saida :

$saida = array( "draw" => $_POST['draw'], "recordsTotal" => $this->lancamento->lancamento_tudo(), "recordsFiltered" => $this->lancamento->lancamento_filtrado(), "data" => $data, "soma" => $total_receita, );

And in the view you print the variable normally: $soma

    
08.06.2017 / 15:10