HTML table inside another table in a foreach in PHP

Asked

Viewed 60 times

0

I wonder if it is possible to Add a Table HTML within another table in a foreach.

Explanation why I need this: I have the budgets in the database and each budget can have many composition items. I would like to list the composition items below their respective budgets.

I’m trying but the layout is breaking.

<table border="1">
  <thead>
    <tr>
      <th>Item</th>
      <th>Descricao</th>
      <th>Quantidade</th>
      <th>Valor unidade</th>
      <th>Valor total</th>
      <th>Cliente</th>
    </tr>
  </thead>
  <tbody>
    <?php $itemCount = 1 ?>
    <?php foreach ($orcamentos->findAll() as $orcamento) : ?>
      <tr>
        <td>Item <?= $itemCount ?></td>
        <td><?= $orcamento['descricao'] ?></td>
        <td><?= $orcamento['quantidade'] ?></td>
        <td><?= $orcamento['valor_unidade'] ?></td>
        <td><?= $orcamento['valor_total'] ?></td>
        <td><?= $clientes->where('id', $orcamento['cliente_id'])->first()['nome_cliente'] ?></td>

        <td>
          <table border="2">
            <thead>
              <tr>
                <th>Item</th>
                <th>Descricao</th>
                <th>Quantidade</th>
                <th>Valor unidade</th>
                <th>Valor total</th>
                <th>Fornecedor</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>A</td>
                <td>A</td>
                <td>A</td>
                <td>A</td>
                <td>A</td>
                <td>A</td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>
      <?php $itemCount += 1 ?>
    <?php endforeach; ?>
  </tbody>
</table>
  • 2

    yes it is possible, but the way it is in your code, to nest a <table> within the <tbody> no, it can be nestled inside a <td>. Create a <td> and insert the table there, use colspan if necessary

  • He sided with each other.... @Ricardopunctual

  • can you put the code in the question? ai da para entender melhor

  • @Ricardopunctual, I edited the question with the updated code...

  • 1

    there will be next to it, would be better to do in a line below, see this example I put together, if you want put in a reply: https://jsfiddle.net/4hrcz01w/1/

  • @Ricardopunctual, worked, puts as an answer for me to score aushuash <3

Show 1 more comment

1 answer

3


The element <table> can be inserted into another table, within an element <td>, so I could do something like this:

<table border="1">
  <thead>
    <tr>
      <th>Item</th>
      <th>Descricao</th>
      <th>Quantidade</th>
      <th>Valor unidade</th>
      <th>Valor total</th>
      <th>Cliente</th>
    </tr>
  </thead>
  <tbody>
      <tr>
        <td>Item 1</td>
        <td>descricao</td>
        <td>2</td>
        <td>10</td>
        <td>20</td>
        <td>Fulano</td>
      </tr>
      <tr>
        <td>Outra tabela</td>
        <td colspan="5" style="background-color: yellow">
          <table border="1">
        <thead>
          <tr>
            <th>Item</th>
            <th>Descricao</th>
            <th>Quantidade</th>
            <th>Valor unidade</th>
            <th>Valor total</th>
            <th>Fornecedor</th>
          </tr>
          <tr>
            <td>Item 1</td>
            <td>descricao</td>
            <td>1</td>
            <td>10</td>
            <td>10</td>
            <td>Fulano</td>
          </tr>
        </thead>
      </table>
        </td>
      </tr>
  </tbody>
</table>

Note that, as the second table is inserted in the structure of the first, I used the colspan to merge cells and maintain alignment and structure

  • Is it possible to create a ul inside a table too? Amazingly enough, the need has arisen and the same thing happens, it breaks

  • 1

    yes, within the <td> can :)

Browser other questions tagged

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