php +1 per set of content - php

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>

Related

Fill html table with php rand() numbers

I need to fill a 10x10 html table using php code with a random number from 1-10 rand(1,10), then depending on the result paint the cell red if <5 or green if >5. i manually created 10 rows with 10 td each and inside I put echo(rand(1,10)) for each one of them but my syntax is wrong and it looks gross
<table>
<tbody>
<?php for ($i = 0; $i < 10; $i++) : ?>
<tr>
<?php for ($k = 0; $k < 10; $k++) : ?>
<?php $num = rand(1, 10); ?>
<td style="color: <?= $num < 5 ? 'red' : 'green'; ?>"><?= $num; ?></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</tbody>
</table>

PHP totalling column and simplifying Sessions

I'm trying to total a column in a table of prices and it's not working out for me. I've looked at a ton of solutions such as array_sum() but it's not displaying anything for me. This is a cart page where I'm posting items to a session and then displaying in a for loop. I'm not sure if my sessions are overly complicated or what. Here is my session and post code:
session_start();
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
$_SESSION['item'][] = array($_POST['item'], $_POST['price']);
}
?>
And here is my for loop section of code:
for($i=0; $i < count($_SESSION['item']); $i++){
$prices = $_SESSION['item'][$i][1];
$sum = 0;
$sum += $_SESSION['item'][$i][1];
print $sum;
?>
<tr>
<td class="left"> <?php print_r($_SESSION['item'][$i][0]) ?></td>
<td class="center"><?php echo "$ " . $_SESSION['item'][$i][1] ?></td>
</tr>
<?php
}
?>
<tr>
<td class="center">Total:</td>
<td></td>
</tr>
<?php
}
?>
</table><br>
Any help would be greatly appreciated!
Replace your current forloop with the following:
You were assigning $sum=0; inside loop so every time loop executed, your $sum variable would be assigned to zero and hence you would lose its value.
Sidenote: You should consider printing sum outside of loop.
$sum = 0;
for($i=0; $i < count($_SESSION['item']); $i++){
$prices = $_SESSION['item'][$i][1];
$sum += $_SESSION['item'][$i][1];
print $sum;
?>
At first your code must be like this:
$sum = 0;
for($i=0; $i < count($_SESSION['item']); $i++){
$prices = $_SESSION['item'][$i][1];
$sum += $prices;
?>
<tr>
<td class="left"> <?php print_r($_SESSION['item'][$i][0]) ?></td>
<td class="center"><?php echo "$ " . $_SESSION['item'][$i][1] ?></td>
</tr>
<?php
}
?>
<tr>
<td class="center">Total: <?=$sum?></td>
<td></td>
</tr>
<?php
}
?>
</table><br>
And the second if you want to sum column of prices from database then you can use mysql SUM function
Reference for mysql SUM

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

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>";
}

Print database data in html Table

I know how to print data from database on html table but i have one task that i can not understand, and i can not do it.
I have a table in database and when i select those data i want them to display on html table but something like this:
Image http://img683.imageshack.us/img683/9350/tablea.png
To achieve a table layout like that you just need to condition the placement of the end of your table rows.
<table>
<tr>
<?php
$count = 0;
while($row = mysql_fetch_assoc($result)) :
$count++;
?>
<td><?php echo $row['value'] ?></td>
<?php if($count % 2 == 0 || $count == mysql_num_rows($res)) : ?>
</tr>
<?php if($count != mysql_num_rows($result)) : ?>
<tr>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
</table>
What the above does is use the modulus operator (%, calculates the remainder from division) to print a closing and opening row tag whenever we're at an evenly numbered result.
Also, if you wanted to change your table layout to be 3 or 4 columns wide all you need to do is change the number applied to the modulus:
$count % 3 == 0 //3 columns
$count % 4 == 0 //4 columns, etc
<table>
<?php while (TRUE) { ?>
<?php
// Make an array of n rows. Trailing items may be FALSE if there
// are not enough rows to fill the table row.
//
$rows= array();
for ($i= 0; $i<n; $i++)
$rows[$i]= mysql_fetch_assoc($result);
if (!$row[0])
break;
?>
<tr>
<?php foreach ($rows as $row) { ?>
<td>
<?php if ($row) { ?>
Value: <?php echo(htmlspecialchars($row['value']); ?>
<?php } ?>
</td>
<?php } ?>
</tr>
<?php } ?>
</table>

Categories