Generate tables with predefined values php7

0

I need to make a script in PHP7 that generates a table of 10x10 and that in the diagonal is formed by ones and the rest by zeros. I've been documented and here describes how to get it out of a database data, however I do not know how to give the values of the diagonal or 0 by default. The PHP:

<?php
$end=10; 
$rows=ceil($end/8);
$x=0;
$start=0;
?>
<table>
<?php
while($x<=$rows) {
  echo "<tr>";
  for ($y = 0; $y < 8; $y++, $start++) {
    if ($start <= $end) echo "<td>$start</td>";
  }
  echo "</tr>";
  $x++;
}
?>
</table>
    
asked by ras212 25.10.2016 в 23:16
source

2 answers

1

Use two Repetitive cycles for , and for printing if 1 or 0 a if by ternary operator ternary-operators

<table border="2">
<?php 

  for ($i=0; $i < 10 ; $i++) {
    echo "<tr>";
    for ($j=0; $j < 10 ; $j++) {
         echo "<td>", ($i==$j)? 1 : 0 ,"</td>";
    }
    echo "</tr>";
  }
 ?>
</table>
    
answered by 26.10.2016 / 00:21
source
1

That's simple you just have to use two repetitive cycles, to get the diagonal you compare i == j and you put zeros, otherwise you put some

<?php
$tabla = "<table border='1'>";
$tabla .= "<tbody>";
for($i = 0; $i<10;$i++){
    $tabla .= "<tr>";
    for($j = 0; $j<10;$j++){
        if($i == $j){
            $tabla .= "<td>";
            $tabla .= "1";
            $tabla .= "</td>";
        }else{
            $tabla .= "<td>";
            $tabla .= "0";
            $tabla .= "</td>";
        }

    }
    $tabla .= "</tr>";
}
$tabla .= "</tbody>";
$tabla .= "</table>";

echo $tabla;
?>

The previous code is without using databases.

    
answered by 25.10.2016 в 23:33