<?php
echo "<table border='1'><br />";
for ($row = 0; $row < 10; $row ++) {
echo "<tr>";
for ($col = 0; $col < 5; $col ++) {
$rand = rand (1, 200);
echo "<td>", $rand, "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Here is code but my question is how can I find max and min from values of the table? Do I have to make random numbers somehow into array?
You can do as following without changing much of your code
<?php
$array = array(); // <-- added code
echo "<table border='1'><br />";
for ($row = 0; $row < 10; $row ++) {
echo "<tr>";
for ($col = 0; $col < 5; $col ++) {
$rand = rand(1, 200);
$array[] = $rand; // <-- added code
echo "<td>", $rand, "</td>";
}
echo "</tr>";
}
echo "</table>";
$min = min($array); // <--- added code
$max = max($array); // <--- added code
?>
Check it out the max and min value of an aray in PHP using library function.
for($col = 0; $col < 5; $col ++) {
$rand[] = rand (1, 200);
}
echo $max = max($rand)."<br/>";
echo $min = min($rand)."<br/>";
print_r($rand);
Related
This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed last year.
I had a little problem, i cannot break the table because I'm looking for a 16 columns in these table to create a unicode table...
The aim is to find where i must break the line of the column as like as there:
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
enter image description here
You used the modulus in the first loop, so use it again in the second
print "</tr>";
print "<tr>";
for($i = 1; $i <= 50000; $i++){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>$i</td><td>$unicodeChar</td>\n";
if ( $i % $columnas/2 ) == 0 ) {
print "</tr><tr>\n";
}
}
print "</tr>\n";
Thank you very much for this help, i have been fixed!!
here is the code that i fixed, with your idea :)
´´´´
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
print "<tr>";
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
if (($i % 8 ) == 0 ){
print "</tr><tr>";
}
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
´´´´
Like Riggsfolly said but maybe divide the columns number by 2 before the modulo, because there are 2 TDs / iteration:
if ( $i % ($columnas/2) ) == 0 ) {
print "</tr><tr>\n";
}
I loop through a multidimensional array to echo the first five values of every "row" as a table. As far as that, it works perfectly:
"<table>";
for ($row = 0; $row < $index_number; $row++) {
echo "<tr>";
for ($col = 0; $col < 5; $col++) {
echo "<td>".$tablevalue[$row][$col]."</td>";
}
echo "</tr>";
}
echo"</table>";
Now I want to display numbers 1 to 3 and then 8 and 9 again. Neither one for loop inside another nor two seperate loops work the way I want them to.
Thats what I tried so far:
echo "<table>";
for ($row = 0; $row < $index_number; $row++) {
echo "<tr>";
for ($col = 0; $col < 3; $col++ && $col2 = 8; $col2 < 10; $col2++) {
echo "<td>".$tablevalue[$row][$col]."</td>";
}
echo "</tr>";
}
echo"</table>";
Any ideas on how to make it work?
For your inner loop just use a conditional, if you have integer indexes starting at 0:
foreach($tablevalue[$row] as $col => $val) {
if($col < 3 || ($col > 7 && $col < 10)) {
echo "<td>$val</td>";
}
}
However, as you can see foreach is much easier for the entire thing:
foreach($tablevalue as $row) {
echo "<tr>";
foreach($row as $col => $val) {
if($col < 3 || ($col > 7 && $col < 10)) {
echo "<td>$val</td>";
}
}
echo "</tr>";
}
If you don't have integer indexes starting at 0, then just foreach(array_values($tablevalue)... and foreach(array_values($row)... or you can slice what you want and implode:
foreach($tablevalue as $row) {
echo "<tr><td>";
echo implode("</td><td>", array_slice($row, 0, 3, true) +
array_slice($row, 7, 2, true));
echo "</td></tr>";
}
I have to make a diamond-shaped asterisk using for loop, inside a table. It has to have "blank" <td> spaces before and after the asterisks to move and make it look centered, so it looks like a diamond. How do I do that? (I used PHP inside an HTML code.)
Code without the <tr> and <td> tags, it looked like a diamond because it was center aligned:
<center>
<?php
echo "<table border = 1>";
// loop for the pyramid
for($i = 1; $i <= 10; $i += 2) {
for($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br />";
}
// loop for the inverted pyramid, so it looks like a diamond
for($i = 7; $i >= 1; $i -= 2) {
for($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br />";
}
echo "</table>";
?>
</center>
Code with the <tr> and <td> tags, need "spaces" for it to look like it's center aligned:
<?php
echo "<table border = 1>";
// loop for the pyramid
echo "<tr>";
for($i = 1; $i <= 10; $i += 2) {
echo "<tr>";
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
echo "</tr>";
}
echo "</tr>";
// loop for the inverted pyramid, so it looks like a diamond
for($i = 7; $i >= 1; $i -= 2) {
echo "<tr>";
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
echo "<br />";
echo "</tr>";
}
echo "</table>";
?>
Please help!
Here is new Code with your solution. I have added logic to put blank td forward and backward to *
<?php
echo "<table border = 1>";
// loop for the pyramid
echo "<tr>";
$max = $initAmount = 10;
for($i = 1; $i <= $initAmount; $i += 2) {
$max = $max -2;
$halfTD = (int)$max/2;
echo "<tr>";
for($b = 1; $b <= $halfTD; $b++){
echo "<td></td>";
}
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
for($b = 1; $b <= $halfTD; $b++){
echo "<td></td>";
}
echo "</tr>";
}
echo "</tr>";
// loop for the inverted pyramid, so it looks like a diamond
$max = $initAmount = 10;
for($i = 7; $i >= 1; $i -= 2) {
$max = $max -2;
$diff = $initAmount - $max;
$blankTd = $diff/2;
echo "<tr>";
for($b = 1 ; $b <= $blankTd; $b++){
echo "<td></td>";
}
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
for($b = 1 ; $b <= $blankTd; $b++){
echo "<td></td>";
}
echo "</tr>";
}
echo "</table>";
?>
I used the code below without using a table to make a diamond shape.
<div style="text-align: center">
<?php
$n = 8;
if($n === 1){ die("input must be greater than 1"); }
$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;
for($x = 1; $x <= $nn; $x++){
$temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
$total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);
echo nl2br(str_repeat('* ', $total) . "\r\n");
}
?>
I used the code below.
<div style="text-align: center">
<?php
$n = 8;
if($n === 1){ die("input must be greater than 1"); }
$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;
for($x = 1; $x <= $nn; $x++){
$temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
$total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);
echo nl2br(str_repeat('* ', $total) . "\r\n");
}
?>
<?php
for($i=0;$i<=5;$i++){
for($j=5;$j>=$i;$j--){
echo ' ';
}
for($k=0;$k<=$i;$k++){
echo '*';
}
echo '<br />';
}
for($i=0;$i<=4;$i++){
for($k=0;$k<=$i+1;$k++){
echo ' ';
}
for($j=4;$j>=$i;$j--){
echo '*';
}
echo '<br />';
}
?>
I want to iterate an array of data using foreach loop.
However, I want the first 4 results to display two items per row.Thereafter I want the rest of the data to display I item per row
i found a possible solution here
$data = range(1, 30);
for($count = 0; $count < count($data);)
{
echo "<tr>\n";
for($i = 0; $count < count($data) && $i < 2; $count++, $i++) {
echo "\t<td>$data[$count]</td>\n";
}
for(; $i < 2; $i++) {
echo "\t<td>-</td>\n";
}
echo "</tr>\n";
}
how the problem with this code is that it displays all the data in rows of two.i however only want the first 4 results to display that way.
You can do something like this:
$data = range(1, 30);
for($count = 0; $count < count($data); ++$count){
if($count < 4){
if($count % 2 == 0){
echo "<tr><td>" . $data[$count] . "</td>";
}else{
echo "<td>" . $data[$count] . "</td></tr>";
}
}else{
echo "<tr><td>" . $data[$count] . "</td></tr>";
}
}
Here's the demo
You're using the already incremented $i variable twice.
$i is never < 2
Add this instead
for($j = 0; $j < 2; $j++) {
echo "\t<td>-</td>\n";
}
Test here
Try with -
$data = range(1, 30);
for($count = 0; $count < count($data); $count++)
{
echo "<tr>";
if($count < 2) {
for($i = 1; $i <= 2; $i++) {
echo "<td>" . $data[$count] . "</td>";
if($i == 1) {
echo "</tr><tr>";
}
}
} else {
echo "<td>" . $data[$count] . "</td>";
}
echo "</tr>";
}
What about something like this?
echo '<table border=1>';
$data = range(1, 30);
for($count = 0; $count < count($data); $count++){
echo "<tr>\n";
if($count < 4) {
echo "\t<td>$data[$count]</td>\n";
$count++;
echo "\t<td>$data[$count]</td>\n";
} else {
echo "\t<td colspan=2>$data[$count]</td>\n";
}
echo "</tr>\n";
}
echo '</table>';
Here's the code
<?php
echo "<table border=\"0\">";
for ($d = 1; $d <= $times;$d++ )
{
echo "<tr><td>";
echo rand(1,6), "\n";
echo "</td></tr>";
}
echo "</table>"; ?>
I'm trying to get the max,min and sum of the rand(1,6), "\n";
But i can't figure it out. And it's killing me.
$rands = array(); // rand() storage
for($d = 1; $d <= $times; $d++){
$rands[$d] = rand(1, 6); // store rands
}
var_dump($min = min($rands)); // min() of rands
var_dump($min = max($rands)); // max() of rands
^ see sample code.
(PS: I use [$d] as he has a 1-based increment and it may be needed for his further logic. This way the rands keys match his $d and can be easily accessed later on.)
You need to collect the random numbers in an array, too:
echo "<table border=\"0\">";
$rands = array();
#################
for ($d = 1; $d <= $times; $d++)
{
echo "<tr><td>";
echo $rands[] = rand(1,6), "\n";
###########
echo "</td></tr>";
}
echo "</table>"; ?>
Afterwards you can make use of max, min and array_sum (all these links come with nice examples).
As your code already shows you should start to differ between code that does data-processing and code that does the HTML output:
// handle the data
$randomNumbers = array();
foreach (range(1, $times) as $d)
{
$randomNumbers[$d] = rand(1,6);
}
// output the data
echo '<table border="0">';
foreach ($randomNumbers as $number) {
printf("<tr><td>%d</tr></td>", $number);
}
echo "</table>";
<?php
$min = 10;
$max = -1;
$sum = 0;
for ($d = 1; $d <= $times; $d++) {
$n = rand(1, 6);
if ($n < $min) $min = $n;
if ($n > $max) $max = $n;
$sum += $n;
}
echo $min . ' ' . $max . ' ' . $sum . '<br/>';
?>
$sum=0;
for ($d = 1; $d <= $times;$d++ ) {
echo "<tr><td>";
$r=rand(1,6);
$sum +=$r;
echo "$r, \n";
echo "</td></tr>";
if ($d==1) { $min=$r; $max=$r; }
if ($r>$max) $max=$r;
if ($r<$min) $min=$r;
}
// do something with $min, $max and $sum;
<?php
$total = 0;
echo "<table border=\"0\">";
for ($d = 1; $d <= $times;$d++ )
{
$rand = rand(1,6);
$total += $rand;
$array[] = $rand;
echo "<tr><td>";
echo $rand, "\n";
echo "</td></tr>";
}
echo "</table>"; ?>
echo $total;
echo min($array);
echo max($array);