Removing the last element in a do-while - php

How can I remove the last element in a do-while generated table?
In my case the last tr/td where div.dividing_line is stored.
The code:
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
++$i;
} while ($i < $ArrayLength+1);
For example: If I have an array with 6 items, normally the do-while will do the job, so finally there will be 6 tr's with data and 6 tr's with the dividing_line.
What I need is 6 tr's of data and 5 tr's of dividing_line. Is that possible?

Try this-
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i != $ArrayLength) {
echo '<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
}
++$i;
} while ($i < $ArrayLength+1);

Use an extra if Statement to check whether you are at the last element:
if (%i < $ArrayLength) { echo '<tr>...dividing_line</tr>'; }

$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i < $ArrayLength)
{
echo '
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>';
}
++$i;
} while ($i < $ArrayLength+1);

I think your approach just needs an additional step.
It could be something like this:
$data = null;
foreach ($rows as $row) {
$data[] = "<tr><td valign=\"middle\">Data_Position</td><td valign=\"middle\">Data_Item</td><td valign=\"middle\">Data_Pieces</td><td valign=\"middle\">Data_Price</td></tr>";
}
print implode("<tr><td colspan=\"4\"><div class=\"dividing_line\"></div></td></tr>", $data);
This way, you could accomplish what you want without any more logic. Of course, it can be changed or re-design, but I think this way will provide you with a simple yet elegan solution to your problem.
Hope it helps :P

Related

how to sum array values from "for" loop

Here is my code :
<tr align="right">
<td style="white-space: nowrap" align="left"><?= $dt_left_header ?></td>
<td></td>
<?php
//trx data
for($p=0; $p < count($arr_prd_grp); $p++){
$prd_id = $arr_prd_grp[$p] ;
//print_r($arr_prd_grp[$p]);
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($arr_amt[$coa_id][$prd_id], 2,',','.').'</td>
';
}
}
//TOTAL
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($amt_tot += $arr_amt[$coa_id][$prd_id], 2,',','.').'</td>
';
}
?>
</tr>
In this case, I want to calculate total of $arr_amt[$coa_id][$prd_id] . My code already calculate it but the result is not equal with my expectation. Can someone tell me how to make it right? Thanks
Move the sum calculation to the first loop, then show the result in the appropriate place. To made it easier i've made an extra variable $totalAmount;
Also my guess is that you are having another outer loop (maybe for each table row). Your current code did not default the totalAmount to 0, so it was adding all the ammounts of each rows, thats why you resulted in such a big number. We add a default value 0 for each row to help that.
<tr align="right">
<td style="white-space: nowrap" align="left"><?= $dt_left_header ?></td>
<td></td>
<?php
//trx data
$totalAmount = 0; // default it
for($p=0; $p < count($arr_prd_grp); $p++){
$prd_id = $arr_prd_grp[$p] ;
//print_r($arr_prd_grp[$p]);
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($arr_amt[$coa_id][$prd_id], 2,',','.').'</td>
';
$totalAmount+=$arr_amt[$coa_id][$prd_id];
}
}
//TOTAL
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($totalAmount, 2,',','.').'</td>
';
}
?>
</tr>

How to start a new row in HTML table when showing MySQL results?

I am showing rows from a MySQL database into a HTML table
See my code below:
<table border='1' width='100%'>
<tr>
for ($i = 1; $i <= mysql_num_rows($result667); $i++)
{
$row = mysql_fetch_array($result667);
$InterviewTime=$row['interview_time'];
echo"<td><input type='radio' name='Time' value='$time' required>$time</td> ";
}
if ($i % 4 == 0) {
echo '</tr><tr>'; // it's time no move to next row
}
?>
</table>
The table is currently showing in one long row. How can I end the current row and start a new row <tr> after 4 columns?
<table border='1' width='100%'>
<tr>
<?php
for ($i = 1; $i <= mysql_num_rows($result667); $i++)
{
$row = mysql_fetch_array($result667);
$InterviewTime=$row['interview_time'];
echo"<td><input type='radio' name='Time' value='$time' required>$time</td>";
if ($i % 4 == 0) {
echo '</tr><tr>'; // it's time no move to next row
}
}
?>
</tr>
</table>
As i can see your if statement which checks for if $i % 4 ==0 is out of the for loop. check this code works for you. and i have added a </tr> to close your opened tr tag at the bottom
Try this Code might help you , and try to write HTML code
<table border='1' width='100%'>
<tr>
<?php
for ($i = 1; $i <= mysql_num_rows($result667); $i++)
{
$row = mysql_fetch_array($result667);
$InterviewTime=$row['interview_time'];
echo"?>
<td><input type='radio' name='Time' value='$time' required><?php $time; ?></td> <?php"
}
if ($i % 4 == 0) {
echo '?></tr><tr> <?php'; // it's time no move to next row
}
?>
</tr>
</table>
Just be simple
<table>
<?php for($i=0;$i<$total;$i++){ ?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php } ?>
</table>
Above code will add $total rows. Just write between <td></td> tags

Need to pull data from MySQL data base using PHP to read dynamic checkboxes

I am trying to pull data from mysql table using dynamic checkboxes created previously, however it looks like my search only displays 2 records max and if I select more than 3 checkboxes it will not return anything, so I was wondering if someone could help me figure out how to do it.
Below is my code, I really appreciate your help:
$warehouse = #$_POST['wh'];
switch($button){
case 'Submit':
if(#$_POST['wh']){
$warehouse = #$_POST['wh'];
$length = count($warehouse);
for ($i = 0; $i < $length; $i++){
//echo '<br>'.$warehouse[$i];
}
$consult = "SELECT * FROM contact_info WHERE ";
for ($i = 0; $i < $length; $i++){
$consult = $consult . "warehouse='$warehouse[$i]'";
if(!$i+1 == $length){
$consult = $consult . " OR ";
}
}
echo '<br>'.$length.'<br>';
print_r ($consult);
$response = mysqli_query($connection, $consult);
if($response){
$registry = mysqli_affected_rows($connection);
if($registry > 1){
echo '<br><table width="800" align="center" border="2" cellspacing="1" cellpadding="1">
<form action="index.php" method="post" >
<tr>
<td align="center"><strong>Warehouse</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Phone</strong></td>
<td align="center"><strong>I-net</strong></td>
<td align="center"><strong>E-mail</strong></td>
</tr>';
while($registry = mysqli_fetch_array($response, MYSQLI_ASSOC)){
echo '<form action="index.php" method=post >
<tr>
<td div align="center">'.$registry['warehouse'].'
<td div align="center">'.$registry['name'].'
<td div align="center">'.$registry['lastname'].'
<td div align="center">'.$registry['phone'].'
<td div align="center">'.$registry['inet'].'
<td div align="center">'.$registry['email'].'
</tr>';
}
echo '</form>';
echo '
</form>
</table>';

PHP AND FOR LOOP

I am trying to use for loops to create a table which dynamically returns something like this: Note how the td content have been arranged
<table>
<tr>
<td>1</td>
<td>3</td>
<td>5</td>
<td>7</td>
<td>9</td>
</tr>
<tr>
<td>2</td>
<td>4</td>
<td>6</td>
<td>8</td>
<td>10</td>
</tr>
</table>
among What I have tried so far is
$row=2;
$col=20;
$x=0;
echo '<table>';
for($i=0;$i<$row;$i++){
echo '<tr>';
for($k=0;$k<$col;$k++)
{
echo '<td>'.echo $x+=1.'</td>';
}
echo '</tr>';
}
In this case I get something different and which is not what I want.
<table>
<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>
</table>
Kindly someone help me map this.Thanks
<?php
# how high to count
$count = 10;
# how many rows in the table
$rows = 2;
# figure out how many columns
$columns = ceil($count/$rows);
# could also go the other way and declare there to be 5 columns and then
# divide to get the rows, but since you're counting down the columns,
# I thought this made more sense. Either way.
?><table><?php
for ($i=0; $i<$rows; ++$i) {
?><tr><?php
for ($j=0; $j<$columns; ++$j) {
# calculate which number to show in column $j of row $i. Each column adds
# $rows numbers to the total, while each row just adds one more. And we
# want to start at 1 instead of 0.
$n = $j * $rows + $i + 1;
?><td><?= $n ?></td><?php
}
?></tr><?php
}
?></table>
$total_count = 10;
$total_rows = 2;
$table = array();
//building table array
for($i=1;$i<=$total_count;$i++) {
$row = $i % $total_rows;
$row = $row == 0 ? $total_rows : $row;
$table[$row][] = $i;
}
//generate table based on array
echo "<table>";
for($row=1;$row<=$total_rows;$row++) {
echo "<tr>";
foreach($table[$row] as $cell) {
echo "<td>".$cell."</td>";
}
echo "</tr>";
}
echo "</table>";
This isnt as complicated as people are making it seem
Start the inner loop at whatever row you're currently on and add 2 each time.
<?php
$rows=2;
$cols=10;
?>
<table>
<?php for($i=1;$i<=$rows;$i++): ?>
<tr>
<?php for($k=$i;$k<$cols;$k+=2): ?>
<td><?php echo $k ?></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</table>
Id probably use range and foreach though
<?php
$rows=2;
$cols=10;
?>
<table>
<?php foreach( range( 1, $rows ) as $row ): ?>
<tr>
<?php foreach( range( $row, $cols, 2 ) as $col ): ?>
<td><?php echo $col ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
This approach will split the data itself
function array_chunk_vertical($array = null,$cols = 3, $pad = array(null))
{
if (is_array($array) == true and !empty($array))
{
$rows = ceil(count($array)/$cols);
$array = array_chunk($array,$rows);
if (count($array) < $cols)
{
$array = array_pad($array,$cols,$pad);
}
foreach($array as $key => $value)
{
$array[$key] = array_pad($value,$rows,null);
}
foreach($array as $key => $value)
{
foreach($value as $sub_key => $sub_value)
{
$output[$sub_key][$key] = $sub_value;
}
}
return $output;
}
return $array;
}
$data = array(1,2,3,4,5,6,7,8,9,10,11);
echo '<table border="1">';
foreach(array_chunk_vertical($data,ceil(count($data)/2)) as $row)
{
echo '<tr>';
foreach($row as $col)
{
echo '<td>' . $col . '</td>';
}
echo '</tr>';
}
echo '</table>';

PHP 2-dimensional array -> how to convert to HTML table syntax?

I need to read a bi-dimensional array and convert the values into an HTML table. For example:
$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;
Reading this array, I want to dynamically write the <TABLE> syntax where the cells with the same number in the grid are merged horizontally or vertically:
That is, I should be able to create the following syntax from the $grid above:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td colspan="4">4</td>
</tr><tr>
<td colspan="2">3</td>
<td colspan="2" rowspan="2">5</td>
</tr><tr>
<td> </td>
<td> </td>
</tr></table>
I have been able to process the colspan correctly, but am having trouble with the rowspan issue. Any ideas? basically I want to be able to create custom homepages with custom content from the array "format" established by the $grid.
Any help greatly appreciated!
Code:
<?php
$grid = array(
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
);
$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;
$w = count($grid[0]);
$h = count($grid);
for ($y = 0; $y < $h; ++$y) {
echo "<tr>\n";
for ($x = 0; $x < $w; ++$x) {
$value = $grid[$y][$x];
if ($value === null) {
continue;
}
$colspan = 1;
while ($x + $colspan < $w && $grid[$y][$x + $colspan] === $value) {
$grid[$y][$x + $colspan++] = null;
}
$rowspan = 1;
$rowMatches = true;
while ($rowMatches && $y + $rowspan < $h) {
for ($i = 0; $i < $colspan; ++$i) {
if ($grid[$y + $rowspan][$x + $i] !== $value) {
$rowMatches = false;
break;
}
}
if ($rowMatches) {
for ($i = 0; $i < $colspan; ++$i) {
$grid[$y + $rowspan][$x + $i] = null;
}
++$rowspan;
}
}
$rowspan = $rowspan > 1 ? " rowspan=\"$rowspan\"" : "";
$colspan = $colspan > 1 ? " colspan=\"$colspan\"" : "";
echo " <td$rowspan$colspan>$value</td>\n";
}
echo "</tr>\n";
}
?>
Output:
<tr>
<td colspan="4">4</td>
</tr>
<tr>
<td rowspan="2" colspan="2">3</td>
<td rowspan="3" colspan="2">5</td>
</tr>
<tr>
</tr>
<tr>
<td colspan="2">0</td>
</tr>
<?php
$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
foreach($grid AS $rowkey => $row)
{
echo "<tr>\n";
foreach($grid[$rowkey] AS $columnKey => $field)
{
echo "<td>{$field}</td>\n";
}
echo "</tr>\n";
}
?>

Categories