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>';
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 wanting to use 8 images from my database and load them into a HTML Table. I would like to display 4 images per table row, but however when i run my code below, i seem to get all images in one table row. Any help would be great. Thank you in advance.
$count = $get->rowCount();
if ($count > 0)
{
echo '<table id="" class="uiGrid _51mz _1m6c" cellpadding="2" cellspacing="0">';
$i = 0;
while ($r = $get->fetch(\PDO::FETCH_OBJ))
{
$globals = new \Libraries\Helpers\Views\Globals;
if ($i == 0)
{
echo '<tr class="_51mx">';
}
echo '
<td class="_51m-">
<a href="/e/a/'.$r->data_id.'">
<div class="uiScaledImageContainer _f-u2" style="width:74px;height:74px;">
<img class="scaledImageFitWidth img" src="https://gstatic.acfee.org/akamaihd/i/'.$globals->data_image_name($r->data_id).'">
<div class="_3s6x">
<div class="_50f3"></div>
</div>
</div>
</a>
</td>
';
if ($i > 4)
{
$i = 0;
echo '</tr>';
};
$i++;
echo '
<script type="text/javascript">
$("#favs-preloader").hide();
</script>
';
}
echo '</table>';
put this code directly it will works...
echo '<table id="" class="uiGrid _51mz _1m6c" cellpadding="2" cellspacing="0">';
$i = 0;
while ($r = $get->fetch(\PDO::FETCH_OBJ))
{
$globals = new \Libraries\Helpers\Views\Globals;
$i++;
if ($i == 1)
{
echo '<tr class="_51mx">';
}
echo '
<td class="_51m-">
<a href="/e/a/'.$r->data_id.'">
<div class="uiScaledImageContainer _f-u2" style="width:74px;height:74px;">
<img class="scaledImageFitWidth img" src="https://gstatic.acfee.org/akamaihd/i/'.$globals->data_image_name($r->data_id).'">
<div class="_3s6x">
<div class="_50f3"></div>
</div>
</div>
</a>
</td>
';
if ($i >= 4)
{
echo '</tr>';
$i = 0;
};
echo '
<script type="text/javascript">
$("#favs-preloader").hide();
</script>
';
}
echo '</table>';
In your current code, you're verifying if the $i variable is == 0 in order to add a row. But you're always increasing it's value at the end, even after you set it to zero in the if($i > 4)
Change these lines
if ($i > 4)
{
$i = 0;
echo '</tr>';
};
$i++;
To this:
if ($i > 4)
{
$i = 0;
echo '</tr>';
}
else
$i++;
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>";
?>
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>';