Create HTML table using PHP

Asked

Viewed 11,326 times

7

Can I create a table to separate this code? Because it’s the only way to align the data I have

if($exibe['Nome1'] != NULL) {
    echo '<p><b>Nome: </b>'.$exibe["Nome1"].' &nbsp;&nbsp;<b> Função : </b>'.$exibe["Funcao1"];
}'</p>

<p>';
if($exibe['Nome2'] != NULL) {
    echo '<p><b>Nome: </b>'.$exibe["Nome2"].' &nbsp;&nbsp; <b> Função : </b>'.$exibe["Funcao2"];
}'</p>

I want to put a table like this:

|Nome      | Função   |

|Nome1     | Função1  |

|Nome2     | Função2  |

2 answers

9


Just edit the html inside your php

<?php
echo '</table>
    <tr>
            <td>Nome</td>
            <td>Função</td>
    </tr>';
if($exibe['Nome1'] != NULL) {
    echo "<tr>
            <td>{$exibe["Nome1"]}</td>
            <td>{$exibe["Funcao1"]}</td>
    </tr>";
}
if($exibe['Nome2'] != NULL) {
    echo "<tr>
            <td>{$exibe["Nome2"]}</td>
            <td>{$exibe["Funcao2"]}</td>
    </tr>";
}
echo '</table>';

1

Buddy, if you have a loop, you can do it this way:

<?php
    echo ' 
        <table>
            <tr>
                <td><b>Nome</b></td>
                <td><b>Função</b></td>
            </tr>
    ';
            while (<< condição >>) {
                if ($exibe['Nome1'] != NULL) {
                    echo '
                        <tr>
                            <td>'.$exibe['Nome1'].'</td>
                            <td>'.$exibe['Funcao1'].'</td>
                        </tr>
                    ';
                }
                if ($exibe['Nome2'] != NULL) {
                    echo '
                        <tr>
                            <td>'.$exibe['Nome2'].'</td>
                            <td>'.$exibe['Funcao2'].'</td>
                        </tr>
                    ';
                }
            }
    echo '
        </table> 
    ';
?>

Browser other questions tagged

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