Place PHP tabs in HTML table

Asked

Viewed 728 times

1

I’m trying to put the function of a table table that I made in PHP in an HTML table, but unfortunately I’m not able, someone could give me a push?

 <?php
function tabuada($tabuada){
   $cont = 1;

   while ($cont <= 10){
      $resultado = $tabuada * $cont;
      echo ($tabuada."x".$cont." = ".$resultado)."<br>";
      $cont++;
   }
}

$tabela = '<center><table border="1";>';
$tabela .= '<tr>';
$tabela .= '<td>tabuada (10)</td>';
$tabela .= '</tr>';
$tabela .= '</table></center>';
echo $tabela;
?>

1 answer

1

It turns out the way you called the function tabuada, it is being interpreted as a string:

$tabela .= '<td>tabuada (10)</td>';

As its function tabuada already makes the impression using echo, simply call for the table to be displayed:

tabuada(10);

However this clear, would be far from being printed inside the table.


For the table to be printed inside the table, its function tabuada needs to be changed and the rest of the code too... There are many ways to do this, below an example trying to keep your code:

<?php

function tabuada($tabuada){
   $cont = 1;
   $resultado = '';

   while ($cont <= 10){
        $resultado .= '<tr><td>' . $tabuada . 'x' . $cont . ' = ' . ($tabuada * $cont) . '</td></tr>';
      $cont++;
   }

   return $resultado;
}

$tabela .= '<center><table border="1";>';
$tabela .= tabuada(10);
$tabela .= '</table></center>';

echo $tabela;

?>

Now its function tabuada returns a string, containing the tabs already inside the tags tr and td, with this we create to invoke the function tabuada and concatenate its return to the rest of the code that generates the table.

Browser other questions tagged

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