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>
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:
Glad you are here. I need solution and I am kinda newbie in these things.
Right now I have page looking like this
1.text 2.text 3.text 4.text
but I need it to be like this
1.text 2.text
3.text 4.text
Code I have
<table >
<?php
for ($x = 1; $x <= 2; $x++): ?>
<tr>
<?php while($row = $choices->fetch_assoc()): ?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php endwhile; ?>
</tr>
<?php endfor; ?>
</table>
The for() loop is useless in this, because the while() loop inside it will process every row returned by the query.
You should just use the while loop, and then use a counter to tell whether to start a new <tr>.
<table>
<?php
$counter = 0;
while ($row = $choices->fetch_assoc()) {
if ($counter % 2 == 0) { // Start a new row before event elements
echo "<tr>";
}
?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php
if ($counter % 2 == 1) { // End row after odd elements
echo "</tr>";
}
$counter++;
}
if ($counter % 2 == 1) { // End last row if it only had 1 column
echo "</tr>";
}
?>
</table>
<table >
<?php while($row = $choices->fetch_assoc()):
if(($row['id_var'] % 2) == 1) echo '<tr>';
?>
<td><button class="pogaAtbilzuVarianti" value="<?php echo $row['id_var']; ?>" id="atbilde_a"><?php echo $row['teksts']; ?></button></td>
<?php
if(($row['id_var'] % 2) == 0) echo '</tr>';
endwhile; ?>
</table>
This question already has answers here:
How to float 3 divs side by side using CSS?
(16 answers)
Closed 6 years ago.
My table has rows that's looped in a non-specific length because the values in the cells may be added or removed anytime. Anyway, here's the code:
<?php
$i = 1;
foreach($items as $item => $itemValue) {
if ($itemValue['item_id'] == $parentItemValue['id']) {
if (fmod($i,7)) echo '<tr>';
echo '<td class="inner-td"><input type="checkbox" id="itemId">'.$itemValue['item'].'</td>';
if (!fmod($i,7)) echo '</tr>';
$i++;
}
?>
The above code displays this:
I also tried if (!fmod($i,7)) echo '<tr>' and if (!fmod($i,8)) echo '</tr>' and gives me this:
Also, if (!fmod($i,10)) echo '<tr>' and if (!fmod($i,11)) echo '</tr>' and gives me this:
I want my table to look like this:
Is there a way that the cells will fill in the entire row before making a new one?
You can try this. Just change the $maxcol value for how many columns you want.
<?php
$tmp = array('test1','test2','test3','test4');
echo '<table border="1">';
$x = 0;
$maxcol = 2; // Max column
foreach($tmp as $i=>$v)
{
echo $x === 0 ? '<tr>' : '';
echo '<td>'.$v.'</td>';
echo $x === ($maxcol-1) ? '</tr>' : '';
$x++;
$x = $x == $maxcol ? 0 : $x;
}
echo '</table>';
?>
Try to follow this structure.
Note that all fields and rows are made up by myself.
<tbody>
<?php
require_once ('connectionWithDB.php');
$table = "members";
$array = $admin_query->viewTableData($table);
foreach ($array as $row){
print_r($array);
?>
<tr>
<td> <?php $row[member_id] ?> </td>
<td> <?php $row[member_password] ?> </td>
<td> <?php $row[member_first_name] ?> </td>
<td> <?php $row[member_last_name] ?> </td>
<td> <?php $row[member_DOB] ?> </td>
<td> <?php $row[member_address] ?> </td>
<td> <?php $row[member_email] ?> </td>
<td> <?php $row[member_phone] ?> </td>
<td> <?php $row[member_gender] ?> </td>
</tr>
</tbody>
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>
I am trying to make a table with 4 rows from a foreach-call.
My problem is, that in the result I get each ID twenty times in the same column.
I'm using this code:
<table width="80%" border="0" cellpadding="10px">
<?php foreach (array_chunk($items, 4) as $row) { ?>
<?php
$i = 0;
foreach ($items as $item):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
} ?>
<tr
<?php echo $class;?>
>
<?php foreach ($row as $item){ ?>
<td>
<?php echo htmlentities ($item['Item']['id']); ?>
</td>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php } ?>
</table>
Any idea how I could get each ID just once?
You are incrememnting $i at every $item as opposed to every $row
Is this the fix you are looking for?
Edit: Mikel has your fix, add this to fix the row bug (Typical of me to notice that first eck!)
<table width="80%" border="0" cellpadding="10px">
<?php
$i = 0;
$chunkedarray = array_chunk($items, 4);
foreach ($chunkedarray as $row) {
$class = null;
if ($i++ % 2 == 0)
$class = ' class="altrow"';
echo "<tr ".$class.">";
foreach ($row as $item){
echo "<td>";
echo htmlentities ($item['Item']['id']);
echo "</td>";
}
echo "</tr>";
}?>
</table>