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

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

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:

PHP table, to add, remove and edit rows

I have issues with my table, I cannot use Javascript (which I know would be easier). I can only use HTML, PHP and CSS. This is my code that I currently have. The issues I need to resolve are the following:
I am able to add rows, delete and I am so also able to edit them by using the "contenteditable", however my issue is that every time I add a row or remove one, it refreshes the whole page. How can I fix this issue?
Also if there is a way to have an edit button instead of my conteneditable method.
Here is my code:
input {
display: block; /* makes one <input> per line */
width: 150px;
}
<?php
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
if (isset($_REQUEST['count'])) $count = $_REQUEST['count'] + 1;
// set value for first load
else $count = 1;
}
if( isset( $_REQUEST["btnremove"]) == "REMOVE") {
// decrement the row counter
$count = $_REQUEST['count'] - 1;
// set minimum row number
if ($count < 1) $count = 1;
}
?>
<form name="form1">
<table class="table table-bordered table-striped table-hover text-center" align='center'>
<tr>
<th align="center">Name</th>
<th>Start </th>
<th>Size</th>
<th>First Condition</th>
<th>Second Conditon</th>
<th><input type="submit" name="btnadd" id="btnadd" value="ADD" align='center'></th>
</tr>
<?php
// print $count rows
for ($i=1; $i<=$count; $i++) {
echo ' <tr>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td> <input type="submit" name="btnremove" id="btnremove" value="REMOVE"></td>
</tr>
';
}
?>
</table>
<input type="hidden" name="count" value="<?php echo $count; ?>">
</form>
every time I add a row or remove one, it refreshes the whole page. How can I fix this issue?
Without Javascript you can't.
if there is a way to have an edit button instead of my conteneditable method
It's rather hard to suggest what your code should be since you don't ofer a description of what the code is intended to do, other than the code which doesn't do what you want. I think you mean something like:
<?php
$width=5;
$data=isset($_REQUEST['data']) ? $_REQUEST['data'] : array();
$count=count($data);
if (isset($_REQUEST['delete'])) {
foreach ($_REQUEST['delete'] as $i) {
unset($data[$i]);
}
}
$data=array_values($data);
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
$data[]=array();
}
...
foreach ($data as $i=>$row) {
print "<tr>\n";
for ($x=0; $x<$width; $x++) {
#print "<td><input type='text' name='data[$i][$x]'></td>\n";
}
print "<td><input type='checkbox' name='delete[]' value='$i'>";
print "</tr>\n";
}
print "<tr>
<td colspan='$width'>
<input type="submit" name="btnadd" id="btnadd" value="Add">
</td>
</tr>\n";

How do I display the output of a "while loop" in a table?

I don't even have an idea of where to start for this. I need to display 100 numbers in a table and would like to use a while loop to do so. Is there a "shortcut" to doing this?
For a table you need some tags table, tr and td. The tr and td are in while loop and the value $i will print inside the td.
<table>
<?php
$i = 1;
while($i != 101){?>
<tr><td><?php echo $i++;?></td></tr>
<?php }?>
</table>
You can use while, for, foreach for your convenience, like below code
<table>
<thead>
<tr>
<th class="header">Number</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
while($i != 101){
?>
<tr>
<td><?php echo $i; ?></td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>
You can dipslay 100 numbers simply in tbale like this:
<?php
// Using For loop
echo "<table>";
for ($i = 1;$i <= 100; $i++) { // use for loop
echo "<tr><td>".$i."</td></tr>";
}
echo "</table>";
// OR
// Using while loop
$i = 1;
echo "<table>";
while($i <= 100) {
echo "<tr><td>".$i."</td></tr>";
$i++;
}
echo "</table>";
?>
You can use a while loop:
while ($i <= 100) {
echo "<td><tr>" . $i++ . "</tr></td>";
}

php +1 per set of content

I'm creating a series of content blocks. (4 in total).
It's stupid to create all for of them if you can let php create them for you.
So I created the first one and wrote a for loop that adds the content as long as it haven't a max number of 4.
Here it is:
<table>
<tr>
<?php
$counter =0;
for ($i=0; $i <= 3; $i++){
$counter++;
?>
<td>some content to repeat with id='$id++' and '$id++'</td>
<?php
if ($counter == 2){
echo '</tr><tr>';
$counter=0;
}
}
?>
</tr>
</table>
What this does is repeat the <td> two times and then end the rule and start a new one. Do this until you reach an amount of 4 (3 because 1=0)
The problem I have is that some of the content contains id's
The id's should also number up but only if the complete loop is repeated.
So $i++ should both have the same value if the loop runs once
So that would mean that if there are two id's in the first run they should both have the id1 and the next repeat should have id2
I have no idea how to do this.
The output should be:
<table>
<tr>
<td>
This content has id 1
And this content has id 1 to
<td>
<td>
This content has id 2
And this content has id 2 to
<td>
</tr>
<tr>
<td>
This content has id 3
And this content has id 3 to
<td>
<td>
This content has id 4
And this content has id 4 to
<td>
</tr>
</table>
M.
How about this
<table>
<?php
$id = 1
for ($i=0; $i < 2; $i++){
echo '<tr>';
for ($j=0; $j < 2; $j++){
echo 'This content has id $id';
echo' And this content has id $id too';
}
$id += 1;
echo '</tr>';
}
}
?>
</table>
Update: added an id counter.
Is this what you mean? You don't need $counter as you have $i keeping count for you.
<table>
<tr>
<?php for ($i=0; $i <= 3; $i++){ ?>
<td<?php
if ($i < 2) echo ' id="1"';
else echo ' id="2"';
?>>some content to repeat</td>
<?php if ($i == 1){ echo '</tr><tr>'; } ?>
<?php } ?>
</tr>
</table>

Removing the last element in a do-while

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

Categories