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>';
Related
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 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>";
?>
we have an assignment for today in which we have to echo some data of the database. The data is month, case, vehicle_id. In a month there can be many cases and in a case there can be many vehicle_ids. What i have achieve to do is to show it in the following way
month st_case veh_id
1 10001 1000011
1 10002 1000021
1 10002 1000022
2 10058 1000581
using this code:
<table border="1">
<tr>
<th>month</th>
<th>st_case</th>
<th>veh_id</th>
</tr>
<?php
// Loop on rows in the result set.
for($ri = 0; $ri < $numrows; $ri++) {
echo "<tr>\n";
$row = pg_fetch_array($result, $ri);
echo " <td>", $row["month"], "</td>
<td>", $row["st_case"], "</td>
<td>", $row["veh_id"], "</td>
</tr>
";
}
pg_close($link);
?>
</table>
The problem is that what i really want to do is to make it show for each month the cases and for each case the vehicles.
1
10001
1000011
10002
1000021
1000022
2
10058
1000581
I tried to do something like this but it doesn't show correctly. If someone could help me with this i would be really thankfull.
<table border="1">
<tr>
<th>month</th>
<th>st_case</th>
<th>veh_id</th>
</tr>
<?php
// Loop on rows in the result set.
$currentmonth = 0;
for($ri = 0; $ri < $numrows; $ri++)
{
$row = pg_fetch_array($result, $ri);
if ($currentmonth != $row["month"])
{
echo "<tr>";
echo "<td>";
echo "<strong>".$row["month"]."</strong>";
echo "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td>".$row["st_case"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row["veh_id"]."</td>";
echo "</tr>";
$currentmonth = $row["month"];
}
pg_close($link);
?>
</table>
picture: http://i61.tinypic.com/2nb8vgl.png
$ret = pg_fetch_all($result);
$group = array();
foreach($ret as $v){
$group[$v['month']][$v['st_case']][] = $v['veh_id'];
}
foreach($group as $month=>$value){
echo $month."<br/>";
foreach($value as $st_case=>$v){
echo $st_case."<br/>";
foreach($v as $veh_id){
echo $veh_id."<br/>";
}
}
}
PS.then add some css to style your table
If you do not intend to display items in a table, do not use table
What you want is something like this:
<div class="result">
<div class="month_group">
<span class="month bold">1</span>
<div class="case_group">
<span class="case">10001</span>
<div class="veh_group">
<span class="veh_id">1000011</span>
</div>
<span class="case">10002</span>
<div class="veh_group">
<span class="veh_id">1000021</span>
<span class="veh_id">1000022</span>
</div>
</div>
</div>
<div class="month_group">
<span class="month">2</span>
<div class="case_group">
<span class="case">10058</span>
<div class="veh_group">
<span class="veh_id">1000081</span>
</div>
</div>
</div>
</div>
Then all that's left to do is applying simple css to give the classes some padding/margin.
HTML is a markup language. You structure the web page using the HTML tags. However, how it would look like depends more on css (which is why it's called Cascading Style Sheets).
Here's the rendering code. I have not tested it yet, and I have not touched PHP for sometime, but it should give you some general ideas:
echo "<div class=\"result\">";
for($ri = 0; $ri < $numrows; $ri++) {
$row = pg_fetch_array($result, $ri);
$month = $row["month"];
echo "<div class=\"month_group\">\n";
echo "<span class=\"month\">".$month."</span>\n";
echo "<div class=\"case_group\">\n";
for ($ci = $ri; $ci < $numrow; $ci++) {
if ($ci == $ri) {
$c_row = $row;
} else {
$c_row = pg_fetch_array($result, $ci);
}
if ($c_row["month"] != $month) {
// We have moved to another month
break;
}
$case = $c_row["st_case"];
echo "<span class=\"case\">".$case."</span>\n";
echo "<div class=\"veh_group\">\n";
for ($vi = $ci; $vi < $numrow; $vi++) {
if ($vi == $ci) {
$v_row = $c_row;
} else {
$v_row = pg_fetch_array($result, $vi);
}
if ($v_row["st_case"] != $case) {
// We have moved to another case
break;
}
$veh = $v_row["veh_id"];
echo "<span class=\"veh_id\">".$veh."</span>\n";
// we have already processed rows whose indexes are less or equal $vi
$ri = $vi;
}
echo "</div>";
}
echo "</div></div>";
}
Try this updated code
<table border="1">
<tr>
<th>month</th>
<th>st_case</th>
<th>veh_id</th>
</tr>
<?php
// Loop on rows in the result set.
$currentmonth = 0;
for($ri = 0; $ri < $numrows; $ri++)
{
$row = pg_fetch_array($result, $ri);
// Open the row
echo "<tr>";
echo "<td><strong>" . $row["month"] . "</strong></td>"; //open and close each column
echo "<td>".$row["st_case"]."</td>";
echo "<td>".$row["veh_id"]."</td>";
echo "</tr>"; //close the row
$currentmonth = $row["month"];
}
pg_close($link);
?>
</table>
You will also need to call the ORDER BY keyword in your MySQL statement to order by month.
Here is a simple tutorial on how to do this.
Here is a Demo of how it might look in your application.
Let me know if this helps and if i can be of any other help.
echo '<table>';
echo '<tr>';
$i = '';
while($res = mysql_fetch_assoc($mysqlqry)){
echo '<td><img src="'. $res['link'] .'" style="border: 0px; width: 150px; height: 150px;" alt="afbeelding"></td>';
$i++;
if($i = '4'){
echo '</tr><tr>';
}
}
echo '</tr>';
echo '</table>';
I want to set a max of 4 <td>s, then set a new <tr> when I get something from a mysql table, only this is not w3 valid and not working.
How can I make this work, and set other <td>s when there are less then 8 results?
Your problem is this:
if($i = '4'){
That's assigning '4' to $i. You probably want to do something like:
if($i % 4 == 0){
In other words, every time $i divided by four is an even division (there's no remainder), start a new <tr>.
echo '<table>';
for ($i=0, $max = 4; $res = mysql_fetch_(...); ) {
if ($i++ % $max == 0)
echo '<tr>';
echo '<row stuff>';
if ($i % $max == 0)
echo '</tr>';
}
echo '</table>';
You can also hardcode $max if you prefer. This also generates no "phantom" rows.
Tip: Don't use a 'string' if your value is numeric.
This is how i have been doing it for years....
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>4 Column Display Table</title>
</head>
<body>
<?php
//connection
$con = mysqli_connect("Host", "UserName", "Password", "TableName");
if (!$con)
{
die('Database connection is - <strong>unsuccessful</strong> ' . mysqli_error());
}
//The Query
$get_query_sql = "SELECT * FROM Query";
$get_query_res = mysqli_query($con, $get_query_sql) or die(mysqli_error($con));
//The Results
while ($res = mysqli_fetch_assoc($get_query_res)) {
$Column1 = $res['Column1'];
$Column2 = $res['Column2'];
//Table display
echo '<table align="center" border="1px" bordercolor="#999999" cellpadding="5px" cellspacing="0" bgcolor="#CCCCCC" style="vertical-align:middle">';
echo '<tr>';
$i = 1;
echo '<td align="middle" height="200px" width="200px"><strong>ID =</strong> '.$Column1.' and the <strong>Number =</strong>'.$Column2.' </td>';
if($i++ % 4 == 0){
echo '</tr><tr>';
}
}
echo '</tr>';
echo '</table>';
?>
</body>
</html>
if($i == 4){
echo '</tr><tr>';
$i = 0;
}
Should work fine, I've done similar code before, you'll need to also fill out the last row with blanks (or use a colspan).
Edit:
Or get fancy:
echo '<table>';
echo '<tr>';
$i = 0;
while($res = mysql_fetch_assoc($mysqlqry)){
echo '<td><img src="'. $res['link'] .'" style="border: 0px; width: 150px; height: 150px;" alt="afbeelding"></td>';
if($i++ % 4 == 0){
echo '</tr><tr>';
}
}
echo '</tr>';
echo '</table>';