PHP: Show 3 td's of data per table row - php

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++;

Related

Having issue in my for loop using PHP, I want to create dynamic rows and column in PHP, Each row has 10 column

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:

Table with multiple buttons - need to get the value of the button that I click on in php.

The table below contains buttons with values. When a button is clicked I want to proceed "predictSeason.php". Say I want to print/echo the team I selected there (i.e the value of clicked button) - how should I do this?
<table class ="teamTable">
<form action="predictSeason.php" method="post">
<?php
$number_per_row = 5;
$i = 0;
foreach ($soccerseason->getTeams() as $team) {
if (($i % $number_per_row) == 0) {
echo '<tr>';
}
?>
<td class="crestTd"style="background-image:url(<?php echo $team->crestUrl ?>);">
<button id="button<?php echo $i ?>" name="button<?php echo $i ?>" class="tableButton" value="<?php echo $team->name ?>"></button>
</td>
<?php
if (($i % $number_per_row) == $number_per_row - 1) {
echo '</tr>';
}
$i = $i + 1;
}
?>
</form>
</table>
You should write
<table class ="teamTable">
<form action="predictSeason.php" method="post">
<?php
$number_per_row = 5;
$i = 0;
foreach ($soccerseason->getTeams() as $team) {
if (($i % $number_per_row) == 0) {
echo '<tr>';
}
?>
<td class="crestTd"style="background-image:url(<?php echo $team->crestUrl ?>);">
<button id="button<?php echo $i ?>" name="button<?php echo $i ?>" class="tableButton" value="<?php echo $team->name ?>" type="submit" ></button>
</td>
<?php
if (($i % $number_per_row) == $number_per_row - 1) {
echo '</tr>';
}
$i = $i + 1;
}
?>
</form>
</table>

Can't make it appear in the correct way PHP

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.

Need little PHP help

I need little PHP help, here is a code which generate a product list:
<table class="list">
<?php for ($i = 0; $i < sizeof($products); $i = $i + 1) { ?>
<tr>
<?php for ($j = $i; $j < ($i + 1); $j++) { ?>
<td width="100%"><?php if (isset($products[$j])) { ?>
<a class="prod_snimka" href="<?php echo $products[$j]['href']; ?>"><img width="200" src="<?php echo $products[$j]['thumb']; ?>" title="<?php echo $products[$j]['name']; ?>" alt="<?php echo $products[$j]['name']; ?>" /></a>
<?php } ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
Output: http://d.pr/PPVq+
What I need to modify to produce output one TR and TD's inside, after 4 TD (products) make another TR. In one row I want 4 TD, in that case too, if there is only 3 product then the last one will be an empty TD. Huhh.. It is possible?
Thanks
It does not look like you need that inner loop. It will only execute once anyway.
Try this out:
<?php
$columns = 4;
$i = 0;
?>
<table class="list">
<?php foreach ($products as $product) {
if (!($i % $columns)) {
echo '<tr>';
} ?>
<td width="25%">
<a class="prod_snimka" href="<?php echo $product['href']; ?>"><img width="200" src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" /></a>
</td>
<?php
$i++;
if (!($i % $columns)) {
echo '</tr>';
}
?>
<?php } ?>
<?php
if (!($i % $columns)) {
while (!($i % $columns)) {
echo '<td width="25%"></td>';
$i++;
}
echo '</tr>';
}
?>
</table>
<table class="list">
<?php $i = 0;
$cols = 4;
for each $products as $product {
if (!($i % $cols)) {
echo "<tr width='25%'>";
}
echo '<td><a class="prod_snimka" href="'.$product['href'].'"><img width="200" src="'.$product['thumb'].'" title="'.$product['name'].'" alt="'.$product['name'].'" /></a></td>';
if !($i % $cols) {
echo "</tr>";
}
$i++;
}
if ($i-1 % $cols) {
echo "</tr>";
}
</table>

How to show nine images in a table , three images in each row using php

I want to show nine images in a table , three images in each row using PHP.
It mostly depends on how you have PHP representing the images. The HTML markup should look something like
<div class="img-table">
<img ...>
<img ...>
<img ...>
....
</div>
and the CSS:
.img-table {
width:1000px; /* or whatever works */
}
.img-table img {
width:30%;
height:auto;
display:inline;
margin: 10px 1.5%; /* spacing: 20px vertically, 5% horizontally */
}
or, if you want to use <table> tables:
<table class="img-table">
<tr>
<td><img ...></td>
<td><img ...></td>
<td><img ...></td>
</tr>
.....
</table>
If you have your image urls located in a array, like
$imgs = array('path/to/img1.jpg','path/to/img2.jpg',...);
then you can generate the HTML like this:
$index = 0;
$html = '<div class="img-table">';
for ($i=0; $i<3; $i++) {
for ($j=0; $j<3; $j++) {
$html .= '<img src="' . $imgs[$index++] . '">';
}
}
$html .= '</div>';
echo $html;
or for the table:
$index = 0;
$html = '<table class="img-table">';
for ($i=0; $i<3; $i++) {
$html .= '<tr>'
for ($j=0; $j<3; $j++) {
$html .= '<td>';
$html .= '<img src="' . $imgs[$index++] . '">';
$html .= '</td>';
}
}
$html .= '</table>';
echo $html;
Hope that helps.
Laying out column images in a table
<? $perrow=3; $n=0; ?>
<table>
<? foreach($images as $i): ?>
<? if($n == 0): print '<tr>'; endif; ?>
<td><img src="<?=$i?>"></td>
<? if(++$n >= $perrow): print '</tr>'; $n = 0; endif; ?>
<? endforeach; ?>
</table>
<?php
$images=ARRAY( a.gif","b.jpeg","c.jif","d.bmp");
?>
<table border="0" cellspacing="1" cellpadding="0">
<tr>
<?php
for($i=0; $i< count($images); $i++) {
echo "<td>
<table width=100% border=0>
<tr><td>
<img src=Download/$images[$i] width=130 height=130 border=0></td></tr>
</table>
</td>";
if ( (($i+1) % 3) == 0 ) echo $newrow="</tr><tr>"; // change 6 to 8 and see
}
?>
</tr>
</table>

Categories