Display array value in php

2

I am putting values in a array and need to display those values on the side of the titles in the th of a table .

I do not want to use a loop to go through the array, I want to access the value by the index.

Here is my function that feeds array and sends to view

public function index() {

    $novas = $this->model->countSituacaoNova();
    $analise = $this->model->countSituacaoAnalise();
    $aprovada = $this->model->countSituacaoAprovada();
    $reprovada = $this->model->countSituacaoReprovada();
    $pendente = $this->model->countSituacaoPendente();
    $paga = $this->model->countSituacaoPaga();

    $data = array(
        'novas' => $novas,
        'analise' => $analise,
        'aprovada' => $aprovada,
        'reprovada' => $reprovada,
        'pendente' => $pendente,
        'paga' => $paga
        );

    $situacoes['situacao'] = $data;

    //var_dump($data);
    $this->load->view('propostas_view', $situacoes);
}

Here is an excerpt of view with table in which I try to use the value of array

<table class="table table-striped table-hover">
<thead>
    <tr>
        <th>Em análise (<?php echo $situacao['novas']; ?>)</th>
        <th>Aprovadas</th>
        <th>Reprovadas</th>
        <th>Pendentes</th>
        <th>Pagas</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</tbody>
</table>

My array $novas has this:

  array (size=1)
 0 => 
   object(stdClass)[22]
     public 'Novas' => string '1' (length=1)
  

Error: Message: Array to string conversion

    
asked by anonymous 19.10.2017 / 14:53

3 answers

2

With its description for the new $ array, the value can be obtained as follows, as there is the "New" Object within position 0 of that vector:

$novas[0]->Novas

    
19.10.2017 / 15:17
0

You are getting a array in the "new" index. That's why the error is being triggered.

If your model returns an array :

Change this section:

<?php echo $situacao['novas']; ?>

By this:

<?php echo $situacao['novas']['Novas']; ?>

If your model returns an object :

Change this section:

<?php echo $situacao['novas']; ?>

By this:

<?php echo $situacao['novas']->Novas; ?>

    
19.10.2017 / 15:16
0

If your model returns only one record it should use result_row() instead of result() which returns more than one and requires for / foreach.

To access the value correctly in your view do:

foreach($situacao['novas'] as $item){
   echo $item->Nova .'<br>';
}

Or for a record with result_row()

echo $stituacao['novas']->Novas;
    
19.10.2017 / 15:38