View array value in php

Asked

Viewed 1,389 times

2

I’m putting values in a array and I need to display these values on the side of the titles in th of a table.

I don’t want to use a loop to traverse the array, I want to access the value through the Indice.

It follows my function that feeds the array and sends to the 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);
}

Follows a passage from view with the table where 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>

mine array $novas has it:

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

Error: Message: Array to string conversion

  • What mistake does the ?

  • Message: Array to string conversion

  • 1

    Give a dd() in your array and see its contents

  • The variable $novas is getting an even integer amount? Check if an array is not coming.

  • truth.. is coming an array in $novas

3 answers

3


With its description for the $novas array, the value can be obtained as follows, because there is the "Novas" object within the 0 position of this vector:

$novas[0]->Novas

  • Undefined index: Novas

  • which of the 3 attempts you used?

  • the first attempt.. in the second the same problem

  • try trading for $query->result()[0]['New']; and $query->result()[0]->New;

  • didn’t work out... edited the question

  • edited my answer here too

Show 1 more comment

0

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

If your model returns an array:

Change that passage:

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

For that:

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

If your model returns an object:

Change that passage:

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

For that:

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

  • I edited the question and put the array $novas... tried but gave the following error: Undefined index: Novas

0

If your model returns only one record should use result_row() in place of result() that 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;

Browser other questions tagged

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