Variable call another

0

I need to declare a variable and call the name of an object by code and also call the type. I tried to nest foreach but it did not work. I tried declaring the variable $tipo out of the function but it did not work either. If I just call it, it gives an error

  

Undefined index: type

Follows part of the code. This code refers to a table that displays the name, type, and two buttons to delete and edit. It's all working, I just do not know how to call the type to correctly display the database information in the table.

    echo '<table style="width:100%;border:2px solid #000000">';
    //echo '<th style="border:1px solid #000000; background-color:#3b8ccc">Código</th>';
    echo '<th style="border:1px solid #000000; background-color:#3b8ccc; width:55%">Local</th>';
    echo '<th style="border:1px solid #000000; background-color:#3b8ccc">Tipo</th>';
    echo '<th style="border:1px solid #000000; background-color:#3b8ccc">Ação</th>';


   // $tipo=  [];
    foreach ($repositorio as $codigo => $nome['nome'])
    {   
                echo '<tr>';
                //echo '<td style="border:1px solid #000000">'.$codigo.'</td>';
                echo '<td style="border:1px solid #000000">'.$nome['nome'].'</td>';
                echo '<td style="border:1px solid #000000">'.$tipo['tipo'].'</td>';

                echo '<td style="border:1px solid #000000">';

                echo '<a href="index.php?r=a2d/excluiRepositorio&codobj='.$codigo.'"><i class="icon-remove" ></i> Excluir </a>';
                echo '<i class="icon-null" ></i>'; //icon-null para espaçar os botoes


                $this->widget('ext.popup.JPopupWindow');
                echo '<a href="index.php?r=a2d/editarRepositorio&codobj='.$codigo.'" title="editarRepositorios" class="editarRepositorios"><i class="icon-edit" ></i> Editar </a>';
                echo '<script type="text/javascript">';
                            echo '$(".editarRepositorios").popupWindow({ 
                                  height:800, 
                                  width:1300, 
                                  top:50, 
                                  left:50 
                                  });'; 
                            echo '</script>';

                echo '</td>';

                echo '</tr>';        
    }
    echo '<tr>';
    echo '<td style="border:1px solid #000000">';
    echo '<textarea style="height:20px; width:50%; resize:none" name="NovoRepositorio"></textarea>';
    echo '</td>';

    echo '<td style="border:1px solid #000000;vertical-align:text-bottom;">'; 
    $this->widget('bootstrap.widgets.TbButton',
                    array(
                        'buttonType' => 'submit',
                        'type' => 'success',
                        'icon' => 'icon-plus-sign',
                        'label' => 'Adicionar',                        
                        )
                  );
    echo '</td>';

    echo '</tr>';

    echo '</table>';
    
asked by anonymous 16.01.2015 / 17:28

2 answers

1

The assembly of your code is confusing ... as already pointed out, it is not correct to use the foreach structure the way you are doing it. I do not know exactly what the structure of the $ repository object is, but I will assume something like

$repositorio = [
    ['nome' => 'Nome 1', 'tipo' => 'Tipo 1'],
    ['nome' => 'Nome 2', 'tipo' => 'Tipo 2']
];

If so, your foreach should be

<?php foreach($repositorio as $item): ?>
<tr>
    <td><?php echo $item['nome'] ?></td>
    <td><?php echo $item['tipo'] ?></td>
</tr>
<?php endforeach; ?>

If your $ repository is of type

$repositorio = [
   'Nome 1' => ['tipo' => 'Tipo 1'],
   'Nome 2' => ['tipo' => 'Tipo 2']
];

You could put it like this:

<?php foreach($repositorio as $nome => $item): ?>
<tr>
    <td><?php echo $nome ?></td>
    <td><?php echo $item['tipo'] ?></td>
</tr>
<?php endforeach; ?>

If it's not, explain it better than I try to help. Hugs.

    
16.01.2015 / 22:17
0

to display something in the variables $ name ['name'] and $ type ['type'] declared inside the foreach look at the block below:

<?php
$tipo           = array("tipo" => "TIPO NO ARRAY");
$repositorio    = array("chave" => array("nome" => "NOME NO ARRAY"));
foreach ($repositorio as $codigo => $nome['nome'])
{
    echo 'NOME: '.$nome['nome']['nome'];    // imprime "NOME NO ARRAY"
    echo '<br/>';
    echo 'TIPO: '.$tipo['tipo'];            // imprime "TIPO NO ARRAY"
}
?>

"Correcting" the excerpt in foreach:

<?php
$tipo           = array("tipo" => "TIPO NO ARRAY");
$repositorio    = array("chave" => array("nome" => "NOME NO ARRAY"));
// foreach ($repositorio as $codigo => $nome['nome'])   // $nome['nome'] não parece correto :/
foreach ($repositorio as $codigo => $nome)
{
    // echo 'NOME: '.$nome['nome']['nome']; // imprime "NOME NO ARRAY", mas, observe abaixo :)
    echo 'NOME: '.$nome['nome'];    // imprime "NOME NO ARRAY"
    echo '<br/>';
    echo 'TIPO: '.$tipo['tipo'];    // imprime "TIPO NO ARRAY"
}
?>

I'm worse than bad to explain, so I hope you know how to analyze the code and understand where you're fooling yourself, anything, comment here that I'm trying to explain to you ...

    
17.01.2015 / 07:23