Variable call another

Asked

Viewed 193 times

0

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

Undefined index: type

Follows a 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 don’t know how to call the guy to properly 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>';
  • What exactly is/should be in the variable $tipo?

  • 2

    Could you give us an example of the structure of the variable $repositorio? I believe that your foreach should have this structure foreach ($repositorio as $codigo => $rep) and the use of nome and tipo in this way $rep['nome'] and $rep['tipo'], but this depends on the structure of the variable $repositorio.

  • A word. Example: In the table I have the information "Antenna" type "Laboratory". This type should be shown in a box for choice, but I will do so later. The main thing is to be able to call the information in the database.

  • public $repositorio = array();

2 answers

1

The assembly of your code is confused... as has already been pointed out, it is not correct to use the foreach structure the way you are doing. I don’t know exactly what the structure of the $repositorio object is, but I’m going to 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; ?>

Already if your $repository is of the type

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

You could ride 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 nothing like that, explain better that I try to help. Hugs.

  • I tried to do it this way but the items in the table are not displayed correctly. It even displays some information in the "type" but only letters appear instead of the name q should appear. foreach ($repositorio as $codigo => $item)&#xA; { &#xA; echo '<tr>';&#xA; //echo '<td style="border:1px solid #000000">'.$codigo.'</td>';&#xA; echo '<td style="border:1px solid #000000">'.$item['nome'].'</td>';&#xA; echo '<td style="border:1px solid #000000">'.$item['tipo'].'</td>';

  • ps.: sorry not being formatted properly, still don’t know how to do it.

0

to display something in the $name['name'] and $type['type'] variables declared within foreach note 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 stretch in the 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 lousy at explaining, so I hope you know how to analyze the code and figure out where you’re fooling yourself, anything, comment here that I’m trying to explain to you...

  • although it seems incorrect, if I take the ['nome'] of foreach ($repositorio as $codigo => $nome['nome']), the table is not displayed correctly. This needs to be part of the code. This code is the standard of the company project I’m doing internship, so I can’t change much =/

  • It is not to change, it is to understand the functioning... the "what I am accessing" of the story..

Browser other questions tagged

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