Fill html table with php rand() numbers - php

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>

Related

How to display numbers vertically 1 to 20 using php

this is the sample logic i want to use.
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
this is my sample code. i'm just getting the logic from numbers so i can use to to what i want to display. thank you in advance to those who can help me.
<?php
$counter = 0;
$columnctr = 0;
for ($x = 0; $x <= 4; $x++) :?>
<tr>
<?php for ($j = 0; $j <= 17; $j++) :?>
<?php if($counter == $x) : ?>
<?php
$i = 0;
?>
<?php foreach ($iterator as $val) :?>
<?php if($x == $i) : ?>
<td>
<?php if($val == "meron") : ?>
<span class="circle-blue">
</span>
<?php $counter++;?>
<?php else : ?>
<span class="circle-red">
</span>
<?php $counter++;?>
<?php endif; ?>
</td>
<?php endif; ?>
<?php $i++;?>
<?php endforeach;?>
<?php else : ?>
<td>
</td>
<?php endif; ?>
<?php endfor;?>
</tr>
$total = 20;
$totalRow = 5;
$totalCol = ceil($total/$totalRow);
echo '<pre>';
for($x = 1; $x <= $totalRow; $x++){
for($y=1; $y <= $totalCol; $y++){
$number = $x + (($y -1) * $totalRow);
// don't print extra number for last column, check for $total = 21 or 22
if($number <= $total)
echo $number. ' ';
}
echo PHP_EOL;
}
for($i=1;$i<=5;$i++):
$f = 0;
for($j=$i;$j < ($i+4) ; $j++ ):
if($f == 0):
echo $j." ";
$p = $j;
else:
echo $p = ($p + 5)." ";
endif;
$f = 1;
endfor;
echo "<br/>";
endfor;

Create table using php arrays

I am using 3 php arrays to create a table, these three arrays are used to generate table headers.
$array1 = array('A','B');
$array2 = array('S','C');
$array3 = array('A1','B1','C1','D1');
Generate a table with these array values are headers.
I want to create a table like in image using these three array.
The following code is used to dynamically generate the table that you have shown. You can change the values of $array1, $array2 or $array3 respectively and table is generate dynamically.
<?php
$array1 = array('A','B','C');
$array2 = array('S','C','M');
$array3 = array('A1','B1','C1','D1','E1','F1');
/* Get the length of all arrays */
$lengthArray1 = count($array1);
$lengthArray2 = count($array2);
$lengthArray3 = count($array3);
?>
<table border="1" width="50%">
<thead>
<!-- Print the header -->
<tr>
<!-- For first empty space -->
<td></td>
<?php
/* Based on the length of the array display the headers */
for($i = 0; $i < $lengthArray1; $i++){ ?>
<!-- Align center and add the colspan wrt to $array length -->
<td colspan="<?php echo $lengthArray2; ?>" align="center">
<?php echo $array1[$i]; ?>
</td>
<?php
} ?>
</tr>
<tr>
<!-- For first empty space -->
<td></td>
<?php
/* Loop through all the $array1 so that that many $array2 values will be assigned to $array1 header */
for($i = 0; $i < $lengthArray1; $i++){
for($j = 0; $j < $lengthArray2; $j++){
?>
<td align="center">
<?php echo $array2[$j]; ?>
</td>
<?php
}
} ?>
</tr>
</thead>
<tbody>
<!-- Loop through $array3 -->
<?php
for($i = 0; $i < $lengthArray3; $i++){ ?>
<tr>
<!-- Print $array3 value in first td -->
<td><?php echo $array3[$i]; ?></td>
<?php
/* To get the remaining td's I have multiplied $array1 X $array2 */
for($j = 0; $j < ($lengthArray1 * $lengthArray2); $j++){
?>
<td></td>
<?php
}
?>
</tr>
<?php
} ?>
</tbody>
</table>

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

Cannot Increment Object variable?

So i am trying to increment the $i in the actual call if that makes sense, please look at the code to understand:
<?php
for ($i=0; $i < 8 ; $i++) { ?>
<td><img src="http://ddragon.leagueoflegends.com/cdn/6.6.1/img/item/<?php echo $game->stats->item.$i; ?>.png"></img></td>
<?php }
?>
essentially what i need to do is increment it like follows:
$game->stats->item1
$game->stats->item2 etc..
Try it like this:
<?php
for ($i = 0; $i < 8 ; $i++) {
?>
<td>
<img src="http://ddragon.leagueoflegends.com/cdn/6.6.1/img/item/<?php echo $game->stats->{'item' . $i}; ?>.png" />
</td>
<?php
}
?>

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>

Categories