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.
Fiddle for this structure is here.
And this is what I am getting as output.
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?
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
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>
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
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..
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);
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>";
?>
Related
This is the output of my current code:
I was wondering if its possible to have the header on the left and the data from a csv file to be on the right?
For reference, here is my code:
<?php
$row = 1;
if (($handle = fopen("Book1.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
This is how my csv file looks like:
This can be done with a table with vertical headings, here is a basic example:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>Vertical Headings:</h2>
<table style="width:100%">
<tr>
<th>Name:</th>
<td>Bill Gates</td>
<td>Steve Jobs</td>
</tr>
<tr>
<th>Telephone:</th>
<td>555 77 854</td>
<td>123 77 854</td>
</tr>
<tr>
<th>Telephone:</th>
<td>555 77 855</td>
<td>543 77 855</td>
</tr>
</table>
</body>
</html>
To populate the table with php you can do it like this:
$file = fopen("Book1.csv","r");
$data = array();
while($row = fgetcsv($file)) {
$data[] = $row; //Get all the data
}
if($data){
$n_columns = count($data[0]); //Get number of columns
}
echo '<table border="1">';
for ($col = 0; $col < $n_columns; $col++) {
echo '<tr>';
foreach ($data as $row_k => $row) {
if ($row_k == 0) {
echo '<th>';
} else {
echo '<td>';
}
echo $row[$col] ?? '';
if ($row_k == 0) {
echo '</th>';
} else {
echo '</td>';
}
}
echo '</tr>';
}
echo '</table>';
fclose($file);
Result:
I am trying to add colors on an 2 dimensional array to look like this:
https://imgur.com/a/giFqm9F
What I have created right now is this :
https://imgur.com/a/Xab0FUj
My code right now :
<html>
<head>
<title>Two-dimensional Arrays</title>
</head>
<body>
<h1>Two-Dimensional Arrays</h1>
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$p = $col * $row;
echo "<td>$p</td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
I'd recommend using CSS:
<style>
table tbody tr:nth-child(odd) {
background-color: red;
}
table tbody tr:nth-child(even) {
background-color: green;
}
</style>
But if you want to do it in PHP, you can use inline styling:
<table border="1" style="border-collapse: collapse;">
<?php
for ($row = 1; $row <= 10; $row++) {
echo '<tr style="background-color: ' . ($row % 2 === 0 ? 'green' : 'red') . ';">';
for ($col = 1; $col <= 10; $col++) {
$p = $col * $row;
echo "<td>$p</td> \n";
}
echo '</tr>';
}
?>
</table>
I have the following php code that generates a 10X10 table:
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$a = "$row * $col";
echo "<td><a href = '$a'>$a</a></td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
How to recreate this table in HTML, such that links will work?
Every field in a table needs to do a multiplication, e.g. field '5*6' gives result '30'. How to write a php class that will do this operation? So, for row*column, return variable result.
in your Calculator.php
<?php
Class Calculator {
public function calculate($row, $col){
return $result = $row * $col;
}
}
Change index.php as follow.
<?php include_once('Calculator.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
a{
cursor: pointer;
}
</style>
</head>
<body>
<div>
<input type="text" value="<?php if( (isset($_GET['col']) && $_GET['row'])){ echo Calculator::calculate($_GET['row'],$_GET['col']); } ?>">
</div>
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$a = "$row * $col";
echo "<td><a href=?row=$row&col=$col>".$a;
//if( (isset($_GET['col']) && $_GET['row'] && $row==$_GET['row'] && $col==$_GET['col'])){ echo Calculator::calculate($row,$col); }else{ echo $a ;}
echo "</a></td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
$a = $row * $col;
Just remove the double quotes so it will multiple the numbers and not consider it a string.
I am Having an issue in my for loop using PHP, I want to create dynamic row and column, Each row has 10 column after 10 column, the second row also end with 10 column like this up to 5 row, how to do It for a loop.
My for loop code:
<table width="100%" border="1">
<?php
for($i=1; $i<=72; $i++)
{
?>
<tr>
<td width="100%">
<?php echo "Click Here to see Site No.'".$i."'. & Area sqft No" .$i;?></a></td>
</tr>
<?php
}
?>
</tr>
</table>
I tried like this also
<table width="100%">
<tr>
<?php
for($i=1; $i<=72; $i++)
{
$x = 10;
if ($i % $x == 0)
{
?>
<td><?php echo $i;?></td>
<?php
}
}
?>
</tr>
</table>
<?php
echo '<table width="100%" border="1">';
for($i=1; $i<=8; $i++)
{
$y=10;
$y*=($i-1);
echo '<tr>';
for ($x=1; $x <=10; $x++) {
if ($i==1) {
echo '<td>'.$x.'</td>';
}else{
$y+=$x;
echo '<td>'.$y.'</td>';
if ($y==72) {
break;
}
$y-=$x;
}
}
echo '</tr>';
}
echo '</table>';
This will print the bellow table:
As per what I understand you want simple 10 column in each row.
Here is my code which may help you:
<table style="border:1px solid #000">
<tr>
<?php $t=1; for($k=1;$k<=72;$k++){?>
<?php if($t == 10) { $t=0;?><td style="border:1px solid #000"> <?php echo $k; ?> </tr><?php } else {?><td style="border:1px solid #000"> <?php echo $k; ?></td><?php } ?>
<?php $t++;}?>
<?php $x = 10; if ($i % $x == 0) { ?>
....
<?php } ?>
If I am getting the question correct, why don't you do as below:
echo '<table>';
for($i=1; $i<=5; $i++) {
echo '<tr>';
for ($y=1; $y<=10; $y++) {
echo '<td>Row_'.$i.' - Col_'.$y.'</td>';
}
echo '</tr>';
}
echo '</table>';
It will print something like below:
Now i have the following:
<?php include 'header.php'; ?>
<body>
<?php
$rows_max = 10;
$columns_max = 10;
$links = Array(
'link' => "http://testlink.com",
'image' => "img100x100.png");
print '<table border="1px" style="border-collapse:collapse;">';
for($row = 1; $row <= $rows_max; $row++)
{
print '<tr>';
for($col = 1; $col <= $columns_max; $col++)
{
print '<td width="100px" height="100px" background="'.$links["image"].'" >';
print '<center> </center>';
//print "$row - $col";
print '</td>';
}
print '</tr>';
}
print '</table>';
include 'footer.php'; ?>
AND
td a
{
width: 100%;
height: 100%;
display: block;
margin-top: 0px auto;
}
All TD-s now link and have image, although they are all the same. How should i create backend so i can change all these data values individually? Thanks!
<?php
for ($tr=0; $tr<10; $tr++) {
echo "<tr>";
for ($td=0; $td<10; $td++) {
echo "<td width=\"10\" height=\"10\">".$td."</td>";
}
echo "</tr>";
}
?>
maybe something like this?
<?php
for($tr=0;$tr<=10;$tr++)
{
echo "<tr>";
for($td=0;$td<=10;$td++)
{
$image_link = "image_link";
$image_path = "path/to/image.jpg";
echo "<td width=\"10\" height=\"10\"><img src=\"{$image_path}\" /></td>";
}
echo "</tr>";
}
?>
That is a not bad start but, you will want to structure your code to be a nice as possible so when you want to change it you aren't straining your eyes to find the part to change.
<?php
$rows_max = 10;
$columns_max = 10;
// Start Table
print '<table style="">';
for($row = 1; $row <= $rows_max; $row++) // Start at 1 instead of 0 to be nice
{
print '<tr>';
for($col = 1; $col <= $columns_max; $col++)
{
print '<td>';
print "I am in row $row and column $col";
print '</td>';
}
print '</tr>';
}
print '</table>';