PHP html table generator

Asked

Viewed 76 times

-1

the purpose of this table is simple, when I put the information in the textarea it should appear and when I do not fill the cells of this table also should not appear, well until then everything is occurring right, however this table has q be zebrad and when I stop putting one of the information the colors repeat and do not get zebrad (this is because I will not always put all the information of the text area), as in the images below Com todas as informações preenchidas

com algumas das informações preenchidas

<?php

if(isset($_POST)){
$t1a = $_POST['txt1a'];
$t1b = str_replace("\r\n",'<br />',$_POST['txt1b']);
$t2a = $_POST['txt2a'];
$t2b = str_replace("\r\n",'<br />',$_POST['txt2b']);
$t3a = $_POST['txt3a'];
$t3b = str_replace("\r\n",'<br />',$_POST['txt3b']);
$t4a = $_POST['txt4a'];
$t4b = str_replace("\r\n",'<br />',$_POST['txt4b']);
$t5a = $_POST['txt5a'];
$t5b = str_replace("\r\n",'<br />',$_POST['txt5b']);
$t6a = $_POST['txt6a'];
$t6b = str_replace("\r\n",'<br />',$_POST['txt6b']);
$t7a = $_POST['txt7a'];
$t7b = str_replace("\r\n",'<br />',$_POST['txt7b']);
$t8a = $_POST['txt8a'];
$t8b = str_replace("\r\n",'<br />',$_POST['txt8b']);
$t9a = $_POST['txt9a'];
$t9b = str_replace("\r\n",'<br />',$_POST['txt9b']);
$t10a = $_POST['txt10a'];
$t10b = str_replace("\r\n",'<br />',$_POST['txt10b']);
$t11a = $_POST['txt11a'];
$t11b = str_replace("\r\n",'<br />',$_POST['txt11b']);
$t12a = $_POST['txt12a'];
$t12b = str_replace("\r\n",'<br />',$_POST['txt12b']);
$t13a = $_POST['txt13a'];
$t13b = str_replace("\r\n",'<br />',$_POST['txt13b']);
$t14a = $_POST['txt14a'];
$t14b = str_replace("\r\n",'<br />',$_POST['txt14b']);
$t15a = $_POST['txt15a'];
$t15b = str_replace("\r\n",'<br />',$_POST['txt15b']);

if(empty($t1a)){
    echo '<script>alert(" \n\Oops,\nO título do artigo é essencial!\n\nPreencha-o e tente novamente. ");</script>';
    exit;
}

$tab = '<!-- Título -->\n'; 
$tab .= '<div style=\"font-size:16pt; font-weight:bold; padding:10px; font-family: Roboto;\">'.$t1a.'</div>\n';

if(!empty($t1a) && !empty($t1b)){ 
    $tab .= '<!-- Descrição -->\n';
    $tab .= '<div style=\"font-size:12pt; padding:10px; font-family: Roboto;\">'.$t1b.'</div>\n\n';
}
$tab .= '<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"font-family:Roboto, sans-serif;\">\n';
$tab .= '<tbody>\n\n';                  

if(!empty($t2a) && !empty($t2b)) {
    $tab .= '<!-- Linha 1 -->\n';
    $tab .= '<tr style=\"background-color:#D9E2F3;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; padding:5px; font-size:10pt; font-weight:800;\">'.$t2a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; padding:5px; font-size:10pt;\">'.$t2b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t3a) && !empty($t3b)){
    $tab .= '<!-- Linha 2 -->\n';
    $tab .= '<tr style=\"background-color:#EEEEEE;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t3a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t3b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t4a) && !empty($t4b)){
    $tab .= '<!-- Linha 3 -->\n';
    $tab .= '<tr style=\"background-color:#D9E2F3;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t4a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t4b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t5a) && !empty($t5b)){ 
    $tab .= '<!-- Linha 4 -->\n';
    $tab .= '<tr style=\"background-color:#EEEEEE;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t5a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t5b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t6a) && !empty($t6b)){ 
    $tab .= '<!-- Linha 5 -->\n';
    $tab .= '<tr style=\"background-color:#D9E2F3;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t6a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t6b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t7a) && !empty($t7b)){ 
    $tab .= '<!-- Linha 6 -->\n';
    $tab .= '<tr style=\"background-color:#EEEEEE;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t7a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t7b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t8a) && !empty($t8b)){ 
    $tab .= '<!-- Linha 7 -->\n';
    $tab .= '<tr style=\"background-color:#D9E2F3;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t8a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t8b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t9a) && !empty($t9b)){ 
    $tab .= '<!-- Linha 8 -->\n';
    $tab .= '<tr style=\"background-color:#EEEEEE;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t9a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t9b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t10a) && !empty($t10b)){ 
    $tab .= '<!-- Linha 9 -->\n';
    $tab .= '<tr style=\"background-color:#D9E2F3;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t10a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t10b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t11a) && !empty($t11b)){ 
    $tab .= '<!-- Linha 10 -->\n';
    $tab .= '<tr style=\"background-color:#EEEEEE;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t11a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t11b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t12a) && !empty($t12b)){ 
    $tab .= '<!-- Linha 11 -->\n';
    $tab .= '<tr style=\"background-color:#D9E2F3;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t12a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t12b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t13a) && !empty($t13b)){ 
    $tab .= '<!-- Linha 12 -->\n';
    $tab .= '<tr style=\"background-color:#EEEEEE;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t13a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t13b.'</td>\n';
    $tab .= '</tr>\n\n';
}

if(!empty($t14a) && !empty($t14b)){ 
    $tab .= '<!-- Linha 13 -->\n';
    $tab .= '<tr style=\"background-color:#D9E2F3;\">\n';
    $tab .= '<td width=\"20%\" style=\"border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;\">'.$t14a.'</td>\n';
    $tab .= '<td width=\"80%\" style=\"border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;\">'.$t14b.'</td>\n';
    $tab .= '</tr>\n\n';
}       

$tab .= '</tbody>\n';
$tab .= '</table>\n';

echo '<script>
      $(function(){
          $("#preto").show();
          $("#modalHtml").modal("show");
          $("#gerado").val("'.$tab.'");
      });
      </script>';
 }

1 answer

0


The problem is not in the PHP, but in your html/css. To create a "zebrada" table you must use the following CSS:

tr {
  background-color: #EEEEEE;
}

tr:nth-child(even) {
  background-color: #D9E2F3;
}

ps: There are many other problems in the code presented, such as the lack of separation between PHP code and HTML/CSS, as well as flaws in HTML (such as the lack of use of classes in css).

The example below shows how to at least solve the zebra table problem.

tr {
  background-color: #EEEEEE;
}

tr:nth-child(even) {
  background-color: #D9E2F3;
}
<p>tabela 1</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-family:Roboto, sans-serif;">
<tbody>

<tr>
  <td width="20%" style="border:1px solid #AAAAAA; padding:5px; font-size:10pt; font-weight:800;">texto 1</td>
  <td width="80%" style="border:1px solid #AAAAAA; border-left:none; padding:5px; font-size:10pt;">texto 2</td>
</tr>

<tr>
  <td width="20%" style="border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;">texto 3</td>
  <td width="80%" style="border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;">texto 4</td>
</tr>

<tr>
  <td width="20%" style="border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;">texto 5</td>
  <td width="80%" style="border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;">texto 6</td>
</tr>
</tbody>
</table>
<p>tabela 2</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-family:Roboto, sans-serif;">
<tbody>

<tr>
  <td width="20%" style="border:1px solid #AAAAAA; padding:5px; font-size:10pt; font-weight:800;">texto 1</td>
  <td width="80%" style="border:1px solid #AAAAAA; border-left:none; padding:5px; font-size:10pt;">texto 2</td>
</tr>

<tr>
  <td width="20%" style="border:1px solid #AAAAAA; border-top:none; padding:5px; padding-top:5px; font-size:10pt; font-weight:800;">texto 5</td>
  <td width="80%" style="border:1px solid #AAAAAA; border-left:none; border-top:none; padding:5px; padding-top:5px; font-size:10pt;">texto 6</td>
</tr>
</tbody>
</table>

  • Thank you very much friend helped me a lot even though I was not the one who wrote the code, I thought the problem was in the functions of php and as I am beginner I have a little difficulty, at last your help helped me a lot, thanks

  • @Wesleysilva in this case, do not forget to "up vote" and mark the answer as accepted.

Browser other questions tagged

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