Print rows in html table with for loop [duplicate] - php

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>

Related

Quantity of every product outputs like 123. How can i put this in a table row table data

I am having a problem to convert my $quantity_total which is as example (113) from 3 different products.
I want it to be in a table like below.
I have been trying to use chunk_split and explode but if i was able to succeed in that. I wouldn't be able to make it dynamic.
<table>
<tr>
<th>Quantity</th>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>3</td>
</tr>
</table>
$total=0;
$item_count=0;
$arr = array();
$quantity_all = '';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$arr[] = $row;
$_SESSION['cart-checkout'] = $arr;
$quantity=$_SESSION['cart'][$id]['quantity'];
$quantity_all .=$quantity;
$sub_total=$price*$quantity;
echo "<div class='cart-row'>";
echo "<div class='col-md-8'>";
echo "<div class='product-name m-b-10px'><h4>{$name}</h4></div>";
echo $quantity>1 ? "<div>{$quantity} items</div>" : "<div>{$quantity} item</div>";
echo "</div>";
echo "<div class='col-md-4'>";
echo "<h4>$" . number_format($price, 2, '.', ',') . "</h4>";
echo "</div>";
echo "</div>";
$item_count += $quantity;
$total+=$sub_total;
$_SESSION['total'] = $total;
$_SESSION['item-count'] = $item_count;
}
$_SESSION['quantity-all'] = $quantity_all;
Is this possible? And i need it to be dynamic. So if it were 10 different quantities. It would make 10 table rows.
I hope someone can help me, would really appreciate it a lot! It's the last thing to finish my e-commerce webshop.
As mentioned in my comment, I don't think this is good solution... but the function you're looking for is str_split. https://stackoverflow.com/a/9814389/296555
http://sandbox.onlinephpfunctions.com/code/0bbee53cafafc0d5e8954e07d0abc2c86c6c89a8
<?php
$rows = '156165165489465131';
echo '<table>';
echo '<tr><th>Quantity</th></tr>';
foreach (str_split($rows) as $row) {
echo "<tr><td>$row</td></tr>";
}
echo '</table>';
I dont know if this Is what you want, but you can try something like:
<table>
<tr>
<td>Quantity</td>
</tr>
<?php
$characters = str_split( (string) $quantity_total);
foreach($characters as $char){
echo "
<tr>
<td> $char </td>
</tr>
";
}
?>
</table>

PHP looping in table

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>

php foreach break table

Im using tables to store content that's dynamically loaded. It's for a reservation form which will be responsive. What I'm looking to do is break each table row into two if there are more than 5 columns in order for the mobile version to fit on screen.
I'm sure this can be achieved by extending what I already have but can't get it to work.
Here's my current code:
<table>
<tr>
<?php foreach ($hostel->getAvailableDates() as $date): ?>
<th><?php echo $date->getDayOfTheWeek(); ?></th>
<?php endforeach ?>
</tr>
<tr>
<?php foreach ($hostel->getAvailableDates() as $date): ?>
<td>
<?php if($date->getAvailable()) { ?>
<b class="avail tick">Available</b>
<?php } else { ?>
<b class="avail cross">Unavailable</b>
<?php }?>
</td>
<?php endforeach ?>
</tr>
</table>
I'd need to break the loop for each row tr after 5 loops, then add a new row underneath.
I've been experimenting with
$max_loop = 5;
$count = 0;
But no luck so far.
I prefer to reorganize data:
<?php
$availDates = array();
foreach ($hostel->getAvailableDates() as $date) {
$availDates[] = $date;
}
$maxCols = 5;
$chunked = array_chunk( $availDates, $maxCols );
?>
<table>
<?php
foreach ($chunked as $chunk) {
?><tr>
<?php foreach ($chunk as $date): ?>
<th><?php echo $date->getDayOfTheWeek(); ?></th>
<?php endforeach; ?>
</tr>
<tr>
<?php foreach ($chunk as $date): ?>
<td>
<?php if($date->getAvailable()) { ?>
<b class="avail tick">Available</b>
<?php } else { ?>
<b class="avail cross">Unavailable</b>
<?php }?>
</td>
<?php endforeach; ?>
</tr><?php
}
?>
</table>
Look at the mod operator. It should give you what you need.
if($count % $max_loop == 0)
I hope this may help you. thanks.
<?php
$avDates = $hostel->getAvailableDates();
echo "<table><tr>";
foreach($avDates as $i=>$date){ {
if ($i == $max_loop) {
echo "</tr><tr>";
}
echo "<td>".($date->getAvailable() ? '<b class="avail tick">Available</b>' : '<b class="avail cross">Unavailable</b>')."</td>";
}
echo "</tr></table>";
?>
If the value returned by getAvailableDates is an array, you could use a for loop instead of a foreach, and check if the current index is a multiple of five, so you don't have to keep track of the count variable
$avDates = $hostel->getAvailableDates();
for ($i = 0; $i < count($avDates); $i++) {
$date = $avDates[$i];
//do your staff
//if multiple of five add another tr
if ($i % 5 == 0) {
}
}

Multiple rows with PHP foreach?

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>

Unexpected output when comparing database column to variable

I have code which retrieves information about players from a MySQL database. I want to apply a special case to the HTML output if their ranking changes. I want it to look like this: http://i27.tinypic.com/f406tz.png
But i cant get it to be like i want, instead it prints the rank on every row:
$old_rank = '';
while ($g = mysql_fetch_object($q)) {
if ($g->rankname != $old_rank) {
echo "<tr><td>$g->rankname</td>\n";
$old_rank = "<tr><td> </td>\n";
}
echo " <td>$g->name</td></tr>\n";
}
What I want:
<tr>
<td>One</td>
<td>Kraven the Hunter</td>
</tr>
<tr>
<td> </td>
<td>Kull the Conqueror</td>
</tr>
<tr>
<td> </td>
<td>Zazi The Beast</td>
</tr>
<tr>
<td>Vice-leader</td>
<td>Igos du Ikana</td>
</tr>
<tr>
<td> </td>
<td>Saint Sinner</td>
</tr>
<tr>
<td> </td>
<td>Midvalley the Hornfreak</td>
</tr>.......................
What I get:
<tr><td>One</td>
<td>Tester</td></tr>
<tr><td>One</td>
<td>Kraven the Hunter</td></tr>
<tr><td>One</td>
<td>Kull the Conqueror</td></tr>
<tr><td>One</td>
<td>Zazi The Beast</td></tr>
<tr><td>Vice-Leader</td>
<td>Midvalley the Hornfreak</td></tr>
<tr><td>Vice-Leader</td>
<td>Saint Sinner
</td></tr>
<tr><td>Vice-Leader</td>
<td>Igos du Ikana</td></tr>
$old_rank is never equal to $g->rankname because the way you are setting $old_rank, it will contain HTML tags, and the $g->rankname that you get from the DB will never have HTML tags.
Try changing your if statement to something like this:
if ($g->rankname != $old_rank) {
echo "<tr><td>$g->rankname</td>\n";
$old_rank = $g->rankname;
} else {
echo "<tr><td> </td>\n";
}
It prints the rank name if it's a new rank name, else it prints empty space.
The following (notwithstanding typos) separates out the display logic from the database loop. This has the advantages:
- You don't need to depend on the order of the results returned
- You don't need to maintain dodgy logic (like 'old_rank')
- You can display them more nicely (with a rowspan for repeated ranks
I believe the total code is more compact too.
// fill ranks array
$ranks = array();
while ( $g = mysql_fetch_object($q) ) {
if ( !in_array($g->rankname, $ranks) ) {
$ranks[htmlentities($g->rankname)] = array();
}
$ranks[$g->rankname][] = htmlentities($g->name);
}
// do other program logic here
// end of program
?>
<!-- display the page -->
<table>
<tr>
<th>Rank</th><th>Users</th>
</tr>
<?php foreach($ranks as $rankName => $userList): ?>
<tr>
<td rowspan="<?php echo (string)sizeof($userList); ?>">
<?php echo $rankName; ?>
</td>
<td> <?php echo implode('</td></tr><tr><td>', $userList); ?> </td>
</tr>
<?php endforeach; ?>
</table>
I prefer breaking things up a bit more than that. Keeping things separate makes it easier to modify. This should work.
$old_rank = '';
while ($g = mysql_fetch_object($q)) {
echo '<tr>' . "\n";
echo '<td>';
if ($g->rankname != $old_rank) {
$old_rank = $g->rankname;
echo $old_rank;
} else {
echo ' ';
}
echo '</td>';
echo '<td>' . $g->name . '</td>' . "\n";
echo '</tr>' . "\n";
}

Categories