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>
Related
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:
I'm trying create loop in my table, there is 4 item, when column is 3 then create new row . The current output is like this:
x
x
x
x
Here's my code:
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item){ // there is 4 item
$i++;
echo "<tr>";
if ($i <= 3) { ?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php }
}
echo "</tr>";
?>
</table>
What i expected is like this:
x|x|x
x
Thank you.
In the comment section of your question, Sirko is right.
Anyway you can do this like below;
<?php
$i = 0;
foreach ($list_items as $item) {
if($i % 3 == 0)
echo '<tr>';
echo '<td> bla bla bla </td>';
if($i % 3 == 0)
echo '</tr>';
$i++;
}
Change your code to below, it should work.
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item){ // there is 4 item
$i++;
echo "<tr>";
if($i%3==0) echo echo "</tr><tr>";
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
</td>
<td>
<?php echo $item['qty'] ?>
</td>
<?php
}
if($i%3!=0)
echo "</tr>";
?>
</table>
use array_chunk()
<?php
foreach (array_chunk($list_items,3) as $items) {
echo '<tr>';
foreach($items as $item){
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php
}
echo '</tr>';
}
?>
try this ==>
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item) { // there is 4 item
if ($i % 3 == 0) // for i=0,3,6,9 <tr> tag will open
echo "<tr>";
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php
if ($i % 3 == 0) // for i=0,3,6,9 <tr> tag will close
echo "</tr>";
$i++;
}
?>
</table>
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>
I want to make 4 columns in table. In code all the images are comes in single row and single column. But I want a single row containing 4 columns with 4 images (images fetching from database), then create another row and automatically add next 4 images & so on. I don't know how I do this can anyone please suggest me how I do this.
<form name="form">
<select id="sorting" style="width:140px" onChange="optionCheck()">
<option id="s">---Sort By----</option>
<option value="bydate">Sort By Date</option>
<option value="bytopic">Sort By Topic</option>
</select>
</form>
<br />
</div>
<?php include 'connection.php'; ?>
<div id="showByDefault">
<table style="width:60%">
<tr>
<?php include 'connection.php'; ?>
<div id="showByDefault">
<!--<table style="width:60%"><tr>-->
<?php
$sql1=mysqli_query($con,"select * from `insert-n-retrive-pdf` ORDER BY date DESC") or die(mysqli_error($con));
$i=0;
echo "<table><tr>";
while($row=mysqli_fetch_array($sql1))
{
if($i != 0 && $i%4 == 0) {
echo '<tr></tr>';
}
?> <td style="padding:20px;">
<img src="<?php echo $row["thumbnails"]; ?>" /></td><?php
echo '</tr>';
$i++;
}
?></tr></table>
</div>
<div id="hideall">
<div id="topic1">
<?php include 'pdf-sort-by-topic.php'; ?>
</div>
<div id="topic">
<?php include 'pdf-sort-by-date.php'; ?>
</div>
</div>
Try this one 100% working: Nice and easy.
<?php
$sql1=mysqli_query($con,"select * from `insert-n-retrive-pdf` ORDER BY date DESC") or die(mysqli_error($con));
$i = 0;
echo "<tr>";
while($row=mysqli_fetch_array($sql1)) {
if($i != 0 && $i%4 == 0) {
echo "</tr><tr>";
}
?>
<td style="padding:20px;"><img src="<?php echo $row["thumbnails"]; ?>" /></td>
<?php
$i++;
}
?>
Hope this helps!
You can try this code
$query = mysql_query("SELECT * FROM insert-n-retrive-pdf ORDER BY date DESC");
echo '<table width="960">';
$i = 0; //first, i set a counter
while($fetch = mysql_fetch_assoc($query)){
//counter is zero then we are start new row
if ($i==0){
echo '<tr>';
}
//here we creating normal cells <td></td>
$image_name = $fetch['thumbnails'];
$image_location = $fetch['path'];
echo '<td>'.'<img src="'.$image_location.'" alt="'.$image_name.'"/>'.'</td>';
//there is a magic - if our counter is greater then 4 we set counter to zero and close tr tag
if ($i>4){
$i=0;
echo '</tr>';
};
$i++; //$i = $i + 1 - counter + 1
}
echo '</table>';
You can fetch all your images into one-dimensional array and then use function array_chunk(). It will split an array into smaller parts you need. Here's a manual page.
Actually, You can get something like this:
<?php
$images = array();
while($row=mysqli_fetch_array($sql1))
{
$images[] = $row;
}
$images = array_chunk($images, 4);
?>
<?php foreach($images as $imagesChunk): ?>
<tr>
<?php foreach ($imagesChunk as $image): ?>
<td style="padding:20px;">
<a href="<?=$image["path"];?>" target="_blank">
<img src="<?=$image["thumbnails"];?>" />
</a>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
<table style="width:60%">
<tr>
<?php
$counter = 0;
$sql1 = mysqli_query($con, "select * from `insert-n-retrive-pdf` ORDER BY date DESC"
) or die(mysqli_error($con));
while($row=mysqli_fetch_array($sql1))
{
?>
<td style="padding:20px;">
<a href="<?php echo $row["path"]; ?>" target="_blank">
<img src="<?php echo $row["thumbnails"]; ?>" />
</a>
</td>
<?php
if($counter == 4)
{
echo "</tr>";
echo "<tr>";
$counter = 0;
}
$counter++;
}
?>
</tr>
</table>
I have an array of products and I need to display them 5 items per row. How can I make it, because now if i do
<?php foreach($data as $entries) : ?>
<td><?php echo $entries->name; ?>
<?php endforeach; ?>
it doesn't work. Should I make a counter?
Something like:
$data = range(1, 17);
for($count = 0; $count < count($data);)
{
echo "<tr>\n";
for($i = 0; $count < count($data) && $i < 5; $count++, $i++) {
echo "\t<td>$data[$count]</td>\n";
}
for(; $i < 5; $i++) {
echo "\t<td>-</td>\n";
}
echo "</tr>\n";
}
Output
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
</tr>
<tr>
<td>16</td>
<td>17</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
Perhaps a bit more readable?
<table>
<tr>
<?php
$data = range(1, 17);
$counter = 1;
foreach($data as $value){
if (!(($counter++ ) % 5)){
echo "<td>$value</td></tr><tr>";
}else{
echo "<td>$value</td>";
}
}
?>
</table>
Produces :
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17
Hope this helps
echo"<table>";
echo"<tr>";
for($i = 0; $i < count($productsArray); $i++){
if($i % 4 ==0){
echo"</tr><tr>"
}
echo "<td>$products[$i]; </td>";
}
Here's a simple counter that's also easy to modify if layout requirements change
<?php
//initialize a counter
$count = 0;
foreach($data as $entries){
//if it's divisible by 5 then echo a new row
if($count > 0 && $count % 5 == 0){
echo("</tr><tr>\n");
}
$count++;
?>
<td>
<?php
// We're also creating a new cell for each item
echo $entries->name; ?>
</td>
<?php
}
?>
<div class="container-fluid">
<?php
$count = 1;
foreach ($home_products as $pro_img) {
$parent_cat_link = base_url('category/view/'.$pro_img['category_id']);
if ($count % 6 == 1) {
$class="cap1";
$class1="cap";
?>
<div class="row">
<?php }
if($i %2 == 0){?>
<div class="col-md-2 col-sm-4 <?php if($count >=6){ echo $class; } else{ echo 'cap'; } ?> ">
<div class="">
<img src="<?php echo base_url() . 'uploads/category/' . $pro_img['image_name'] ?>" class="img-responsive" alt="logo">
</div>
</div><?php }else{ ?>
<div class="col-md-2 col-sm-4 <?php if($count >=6){ echo $class1; } else{ echo 'cap1'; } ?>">
<div class="">
<img src="<?php echo base_url() . 'uploads/category/' . $pro_img['image_name'] ?>" class="img-responsive" alt="logo">
</div>
</div><?php } $i++;
if ($count % 6 == 0) {
?> </div> <?php } $count++;
} if ($count % 6 != 1) echo "</div>"; ?>
</div>