adding controller foreach field to view

Asked

Viewed 378 times

0

Hello!
I have the following function of the controller.

	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 am returning the data from the database.

My point is, I want to add the releases 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 by ajax ?

I can do this sum when the foreach is in the view itself, but I don’t know how to do this using the plugin DataTable and Ajax.inserir a descrição da imagem aqui

  • I would do a sum storage: $total_revenue += $lancamento->value; inside the loop, then display at the end $total_revenue. I would do so.

  • And what would you call it in the view ?

1 answer

1

As André commented on:

I would make a sum storage: $total_receita += $lancamento->valor; inside the loop, then display at the end $total_recipe. I would do so.

To add this 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.