Sajeev C
Sajeev C

Reputation: 1538

Matrix creation failed in PHP

I am trying to build a 4x4 matrix like structure in PHP using HTML table. There can be n number of elements. So calling it 4x4 matrix is kinda appropriate. There can be any number of rows. But a row can have only 4 columns.

Here's what I am trying to make.

enter image description here

Fiddle for this structure is here.

And this is what I am getting as output.

enter image description here

PHP Code:

<table class="tablematrix content table-striped">
<?php
$total=7;
if($total%4==0)
{
    $tr=$total/4;
}
else
{
    $tr_temp=$total/4;
    $tr=$tr_temp+1;
}

for($i=1;$i<=$tr;$i++)
{
 echo '<tr>';
  for($j=1;$j<=$total;$j++)
  {
   echo '<td>'.$j.'</td>'; 
  }
 
 echo '</tr>';                       

}
?>
</table>

CSS

<style>
.tablematrix {      
      border-collapse:collapse;
      table-layout:fixed;
    }
.tablematrix * {
    height:50px;
    width:50px;
    min-width:50px;
    min-height:50px;
    margin:0px;
    padding:0px;
}

    .tablematrix th,
    .tablematrix td {
      text-align: center;
      border: 1px solid #dddddd;
    }

    .tablematrix th {
      font-weight: bold;
    }

.tablematrix tbody > tr:nth-child(odd) > td,
.tablematrix tbody > tr:nth-child(odd) > th {
  background-color: #f9f9f9;
}

</style>         

Output here

I am kinda stuck here. Don't know how to proceed. Any suggestions?

Upvotes: 1

Views: 207

Answers (6)

mokNathal
mokNathal

Reputation: 553

Check this code:

<table class="tablematrix content table-striped">
<?php
$row=6;
$col=4;
$total=$row*$col;

echo "<tr>";
for($i=1;$i<=$total;$i++)
{
echo "<td>".$i."</td>";
if($i%$col==0)
{ echo '</tr>';
    if($i!=$total)
     echo '<tr>';            
}
}
?>
</table>

OR

<table class="tablematrix content table-striped">
<?php

$total=24;

echo "<tr>";
for($i=1;$i<=$total;$i++)
{
echo "<td>".$i."</td>";
if($i%4==0)
{ echo '</tr>';
    if($i!=$total)
     echo '<tr>';            
}
}
?>
</table>

Upvotes: 1

knives22
knives22

Reputation: 303

try this

 <?php

 $row = 6;
 $column = 3;
 echo "<table style = 'border: 1px solid black'>";

 for($x= 0; $x<= $row; $x++){


echo "<tr>"; 

for($y=0; $y<= $column; $y++){
    echo "<td>";
    echo $y;
    echo "</td>";
    }

echo "</tr>";


 }

 echo "</table>";

 ?>

Upvotes: 0

Md Johirul Islam
Md Johirul Islam

Reputation: 5162

This code should work

<table class="tablematrix content table-striped">
<?php
$total=7;
$tr=$total;
$count=1;
for($i=1;$i<=$tr;$i++)
{
 echo '<tr>';
  for($j=1;$j<=4;$j++)
  {
   echo '<td>'.$count.'</td>';
   $count=$count+1;
  }

 echo '</tr>';                       

}
?>
</table>

Please try this. You are making a logical mistake. I didn't test my code. So let me know if my code is not working. Thanks. You can check my ideone here https://ideone.com/STHC7H. Its showing your desired output. So as you said $total is the total number of elements instead of total number of rows the following code will give you correct result

      <?php
    $total=7;
    if($total%4==0)
    {
        $tr=$total/4;
    }
    else
    {
        $tr_temp=$total/4;
        $tr=$tr_temp+1;
    }

    $count=1;
    for($i=1;$i<=$tr;$i++)
    {
     echo '<tr>';
      for($j=1;$j<=4;$j++)
      {
    if($count==$total+1)
break;
       echo '<td>'.$count.'</td>'; 

        $count=$count+1;

      }

     echo '</tr>';                       

    }
    ?>

I hope it will solve your problem

Upvotes: 1

Robert
Robert

Reputation: 20286

You can use this function. It can be optimised not to use $tmp variable

function createHtmlMatrix ($w, $h) {
    $matrixHtml = '<table class="tablematrix content table-striped">';
    $tmp = 0;
    for ($i = 0 ; $i < $h ; ++$i) {
       $matrixHtml .= '<tr>';
        for ($j = 0 ; $j < $w ; ++$j) {   
            $matrixHtml .= '<td>'.(++$tmp).'</td>';  
        }
       $matrixHtml .= '</tr>';  
    }

    $matrixHtml .= '</table>';

    return $matrixHtml;
}

echo createHtmlMatrix(4,5);

Upvotes: 0

AmBeam
AmBeam

Reputation: 332

Here you have:

<?php
    $rows = 6;
    $cols = 4;

    for ($i = 1; $i <= $rows; $i++) {
        echo '<tr>';

        for ($l = 1; $l <= $cols; $l++) {
            echo '<td>'.((($i - 1) * $cols) + $l).'</td>';
        }

        echo '</tr>';
    }
?>

Tested, should do the job..

Upvotes: 0

Gal Rettig
Gal Rettig

Reputation: 11

Amount of columns = "" => 4. Amount of Rows = "" = ?

Look at this part of your code

  echo '<tr>';
  for($j=1;$j<=$total;$j++)
  {
   echo '<td>'.$j.'</td>'; 
  }

 echo '</tr>';                       

you have set $total to 7 so you are runing 7 times and creating 7 columns instead of row which would have made more sense so basically you need to transpose your matrix

Upvotes: 0

Related Questions