result in 2 table columns

Asked

Viewed 60 times

1

Good afternoon friends, I’m having trouble assembling a table with result in 2 columns with database information.

<table style='width: 100%;' border='1' cellspacing='0' cellpadding='0'>

<thead>
    <tr style='background-color: #CCCCCC;'>

        <td style='text-align: center; width: 12.5%; height:20px'><h3>Equipamento locado</h3></td>
        <td style='text-align: center; width: 12.5%;'><h3>Patrimônio</h3></td>

    </tr>

</thead>
<tbody> 


if($cliente->equipamento){

    $array_equipamento = explode( "," , $cliente->equipamento ) ;

    foreach ($array_equipamento as $value) {

    $equipamento = $objeto_equipamento->busca_equipamento($value);


    <tr>

        <td style='text-align: center; width: 12,5%; height:20px'>$equipamento->nome</td>
        <td style='text-align: center; width: 12,5%;'>$equipamento->patrimonio</td>

    </tr>

    }

}


</tbody>
</table> 


    <table>

    <tr><td>coluna 1 </td> <td> Coluna 2 </td> </tr> 



    <tr><td> 1 </td> <td> 2 </td></tr>
    <tr><td> 3 </td> <td> 4 </td></tr>
    <tr><td> 5 </td> <td> 6 </td></tr>
    <tr><td> 7 </td> <td> 8 </td></tr>



    </table>

The numbers in the example represent the sequence of results for better understanding.

  • 2

    I don’t understand... what code you’re using?

  • 2

    sorry, I edited the html for better visualization !!!

  • We need to know where you get this information?

  • mysql database ... comes from query query !!!

  • $equipamento makes so of a var_dump($equipamento) and glue the result here!

  • It’s the following, I already receive and I list everything in the table friend, the problem I have is to assemble the result in two columns, only this .... an algorithm to divide into the table

  • 1

    Thiago, my dear Odson, if you do not pass as the columns of this table, it is difficult to know how to generate the same!

  • as I informed you, the important thing is not my data but how to present the result divided in 2 columns, if you get the algorithm I apply in my code !!

  • it’s complicated to give a var_dump?

  • @Thiagolopez Only with the data provided is it impossible to propose a solution.

  • I edited the code, if it is simple for you like this ! As you can see this listing in two columns with two information, however I want you to have two more columns on the right and list 2 records per row to take advantage of the sheet size.

Show 6 more comments

2 answers

0

Good,

Shows the structure of table sff.

I simulated the tables with utility classes (code below) and the result was shown below. I didn’t change anything else.

    <?php 

/*
*
* Classes utilitária para simular a estrutura da tabela
*/
class Equipamento{

    public $id;
    public $nome;
    public $patrimonio; 

    public function busca_equipamento($value){

        $testeEquipamentos = self::obterListaDeEquipamenetoTeste();

        foreach($testeEquipamentos as $k => $equipamento){
            if($equipamento->id == $value){
                return $equipamento;
            }
        }

        // um pequeno teste, caso não for encontado o respetivo id/value
        return new  Equipamento();
    }

    public function obterListaDeEquipamenetoTeste(){

        $eq1 = new Equipamento();
        $eq1->id = 1;
        $eq1->nome = "Equipamento 1";
        $eq1->patrimonio = "200€";

        $eq2 = new Equipamento();
        $eq2->id = 2;
        $eq2->nome = "Equipamento 2";
        $eq2->patrimonio = "300€";

        $eq3 = new Equipamento();
        $eq3->id = 3;
        $eq3->nome = "Equipamento 2";
        $eq3->patrimonio = "400€";  

        return [$eq1,$eq2,$eq3];

    }
}

class Cliente{
    public $id;
    public $nome;
    public $equipamento;
}

$cliente = new Cliente();
$cliente->id = '1111';
$cliente->nome = 'Clinet Teste';
$cliente->equipamento = '1,2,3';

?>




    <?php
    /*
    *
    * codigo original => nada alterado
    */
    <table style='width: 100%;' border='1' cellspacing='0' cellpadding='0'>

    <thead>
    <tr style='background-color: #CCCCCC;'>

        <td style='text-align: center; width: 12.5%; height:20px'><h3>Equipamento locado</h3></td>
        <td style='text-align: center; width: 12.5%;'><h3>Patrimônio</h3></td>

    </tr>

    </thead>
    <tbody> 

    <?php



    $objeto_equipamento = new Equipamento();

    if($cliente->equipamento){

        $array_equipamento = explode( "," , $cliente->equipamento ) ;

        foreach ($array_equipamento as $value) {

        $equipamento = $objeto_equipamento->busca_equipamento($value);


        echo "<tr>

            <td style='text-align: center; width: 12,5%; height:20px'>$equipamento->nome</td>
            <td style='text-align: center; width: 12,5%;'>$equipamento->patrimonio</td>

        </tr>";

        }

    }

    ?>

    </tbody>
    </table> 

E o resultado

  • very good friend, now what remains is to do what I really need to list the result in the way I described in the question !! 2 records per row in the table.

  • You can post an image of the diagram of the database. I suppose you have two tables "Customer" and "Equipment". Where each customer can have 0 or more equipment.

0

Good afternoon everyone, thanks for all your help but I already solved my problem ... I followed the source code where I separate the results of the query in two columns independent of the records Qtd, grateful.

if($cliente->equipamento){

    // quantidade de virgulas faz distinção de numero de registros - 1
    // $reg é uma string separada por ',' na coluna $cliente->equipamento na minha BD, para entendimento de todos
    $reg = substr_count( $cliente->equipamento , ',' ) ; 

    $array_equipamento = explode( "," , $cliente->equipamento ) ;

    $colunas = 2;

    $html .= '<tr>';

    $i = 0;

    foreach ($array_equipamento as $value) 
    {

        $equipamento = $objeto_equipamento->busca_equipamento($value);

        if( $i == $colunas)
        {
            $html .= '</tr><tr>';
            $i=0;
        }

        $html .= '<td>'.$equipamento->nome.'</td>';
        $i++;
    }

    $html .= '</tr>';
}
  • anyone can accept my answer to finalize the top, please !!!

Browser other questions tagged

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