Non-recursive dynamic binary tree with PHP - php

I try to create a binary tree as html-table which is not recursive build. The order of the fields should be like this:
C1 C2 C3
7
3
8
1
9
4
10
11
5
12
2
13
6
14
C1 stands for col 1, C2 for col2 etc.
The following code creates a table in a recursive way, but this is not what I want!
<?php
$cols = 4;
$counter = 0;
$lines = pow(2,$cols);
echo '<table border=1 style="border:1px solid black;"> ';
for($i = 0; $i < $lines; $i++){
echo '<tr>';
for($j = 0; $j < $cols; $j++){
$rowspan = $lines/pow(2,$j+1);
if(0 === $i%$rowspan) {
$counter++;
echo '<td rowspan='.$rowspan.'>'.$counter;
}
}
}
echo '</table>';
?>
I hope someone could give me a hint how to solve this problem.

Used this expression to calculate the row's values: ($i / $rowspan + pow(2,$j+1) - 1) wherein $i / $rowspan is the number of the row in the current level starting with 0 for the first row and pow(2,$j+1) - 1 is the level's first value, i.e. 7 for the third level.
$cols = 4;
$lines = pow(2,$cols);
echo '<table border=1 style="border:1px solid black;">';
for($i = 0; $i < $lines; $i++){
echo '<tr>';
for($j = 0; $j < $cols; $j++){
$rowspan = $lines/pow(2,$j+1);
if(0 === $i%$rowspan) {
echo "<td rowspan='$rowspan'>".
($i/$rowspan + pow(2,$j+1)-1).
"</td>";
}
}
}
echo '</table>';
Outputs your desired result. Hope your teacher won't hate me now! ;-)
You can alternativly use ($i/$lines + 1 ) * pow(2,$j+1) - 1 for the row's value, if you don't want to depend on $rowspan.

Related

Print triangular number patterns with loops and modulus calculation

I need to print a number pattern like this:
1
12
123
1234
2
23
234
2341
3
34
341
3412
4
41
412
4123
My code:
for($i=1; $i<=4; ++$i) {
for($j=1; $j<=$i; ++$j) {
echo $j;
}
echo ("<br/>");
}
for($i=2; $i<=4; ++$i) {
for($j=2; $j<=$i; ++$j) {
echo $j;
}
echo ("<br/>");
}
I don't know how to recycle to the first number after the max number is reached. Since my max number is 4, 1 should used instead of 5 (and 2 for 6 and 3 for 7).
You loop from 1 to 4, and subtract 4 if the value is bigger than 4.
This is for 2, 23, 234, 2341:
for ($i = 1; $i <= 4; $i++) {
for ($j = 1; $j <= $i; $j++) {
$value = $j + 1; // or +2, or +3
echo $value > 4 ? $value - 4 : $value;
}
echo "\n";
}
And this would generate all output within one big loop:
$max = 4;
for ($start = 0; $start < $max; $start++) {
for ($i = 1; $i <= $max; $i++) {
for ($j = 1; $j <= $i; $j++) {
$value = $j + $start;
echo $value > $max ? $value - $max : $value;
}
echo "\n";
}
}
Whenever you need circular array access, it is a good idea to implement a modulus calculation. To set this up, use a range between 0 and 3 instead of using 1 and 4. A modulus calculation can return a 0 result, so the formula must be prepared to handle this value. To adjust the value, just add 1 after the modulus calculation to generate numbers between 1 and 4.
Accumulate strings of numbers as desired and reset the string upon each start of the outer loop.
Below proves that you do not need three nested loops for this task.
Code: (Demo)
for ($i = 0; $i < 4; ++$i) {
for ($s = '', $j = 0; $j < 4; ++$j) {
$s .= ($i + $j) % 4 + 1;
echo $s . "\n";
}
}
Output:
1
12
123
1234
2
23
234
2341
3
34
341
3412
4
41
412
4123

PHP position output on specific table row

I want to create a log where I have a start/end date and I need to mark (1) the days between that date. The dates can span months, which I already took care of. I just need some help formatting the output so the table accurately represents the data. Data might be exported to Excel later or just printed directly from the browser, that's up for discussion.
Thank you in advance.
$i = 1;
$max = 10;
$j = 5;
echo '<table><tr>';
for($i; $i <= $max; $i++){
echo '<th>' . $i . '</th>';
}
echo '<tr>';
for($j; $j<=$max; $j++){
echo '<td>1</td>';
}
echo '</tr></table>';
This code gets me the following output
1 2 3 4 5 6 7 8 9 10
1 1 1 1 1
What I am looking for is for the output to be like this:
1 2 3 4 5 6 7 8 9 10
1 1 1 1 1 1
Then you will have to output an empty cell $max - $j number of times
$i = 1;
$max = 10;
$j = 5;
echo '<table><tr>';
for($i; $i <= $max; $i++){
echo '<th>' . $i . '</th>';
}
echo '</tr><tr>';
// output empty table sells to right justify the other output
for($x=0; $x<=$max-$j; $x++){
echo '<td> </td>';
}
for($j; $j<=$max; $j++){
echo '<td>1</td>';
}
echo '</tr></table>';

Reflective pattern of numbers in rows and columns of html table using loops

I'm stuck trying to use nested loops to make a reflective pattern from numbers.
I've already tried, but the output looks like this:
|0|1|2|
|0|1|2|
|0|1|2|
This is my code:
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 3; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 3; $col++) {
$p = $col-1;
echo "<td>$p</td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
I expected this result:
|0|1|0|
|1|2|1|
|0|1|0|
Each columns' and rows' cell values must increment to a given amount then decrement to form a mirror / palindromic sequence.
First declare the square root of the of the table cell count. In other words, if you want a 5-by-5 celled table (25 cells), declare $size = 5
Since your numbers are starting from zero, the highest integer displayed should be $size - 1 -- I'll call that $max.
I support your nested loop design and variables are appropriately named $row and $col.
Inside of those loops, you merely need to make the distinction between your "counters" as being higher or lower than half of the $max value. If it is higher than $max / 2, you subtract the "counter" (e.g. $row or $col) from $max.
By summing the two potentially adjusted "counters" and printing them within your inner loop, you generate the desired pattern (or at least the pattern I think you desire). This solution will work for $size values from 0 and higher -- have a play with my demo link.
Code: (Demo)
$size = 5;
$max = $size - 1;
echo "<table>\n";
for ($row = 0; $row < $size; ++$row) {
echo "\t<tr>";
for ($col = 0; $col < $size; ++$col) {
echo "<td>" . (($row >= $max / 2 ? $max - $row : $row) + ($col >= $max / 2 ? $max - $col : $col)) . "</td>";
}
echo "</tr>\n";
}
echo "</table>";
Output:
<table>
<tr><td>0</td><td>1</td><td>2</td><td>1</td><td>0</td></tr>
<tr><td>1</td><td>2</td><td>3</td><td>2</td><td>1</td></tr>
<tr><td>2</td><td>3</td><td>4</td><td>3</td><td>2</td></tr>
<tr><td>1</td><td>2</td><td>3</td><td>2</td><td>1</td></tr>
<tr><td>0</td><td>1</td><td>2</td><td>1</td><td>0</td></tr>
</table>
There are a lot of ways to achive that.
An easy way to do that is;
<?php
$baseNumber = 0;
echo "<table border='1' style='border-collapse: collapse'>";
for ($row = 0; $row < 3; $row++) {
echo "<tr>";
if ($row % 2 !== 0) {
$baseNumber++;
} else {
$baseNumber = 0;
}
for ($col = 0; $col < 3; $col++) {
echo "<td>" . ($col % 2 === 0 ? $baseNumber : $baseNumber + 1) . "</td>";
}
echo "</tr>";
}
echo "</table>";
this code will do what you want just call the patternGenerator function with the number of distinct numbers you want for your example these numbers are 3 (0,1,2).
the idea in this code is to use two for loops one that starts from the minimum number to the maximum one and the other one that starts after the maximum number decreasing to the minimum.
for example:
if min = 0 and max = 5
the first loop will print 0,1,2,3,4,5
and the second will print 4,3,2,1,0
and that's it.
at first, I created a function that creates just on row called rowGenerator it takes $min and $max as parameters and prints one row
so if we want to print a row like this: |0|1|0| then we will call this function with min = 0 and max = 1 and
if we want to print a row like this: |1|2|1| then we will call it with min = 1 and max = 2.
function rowGenerator($min, $max)
{
echo '<tr>';
for($i = $min; $i<=$max;$i++)
echo '<td>'.$i.'</td>';
for($i = $max-1; $i>=$min;$i--)
echo '<td>'.$i.'</td>';
echo '</tr>';
}
for now, we can print each row independently. now we want to print whole the table if we look at the calls we do for the rowGenerator function it will looks as follow:
(min = 0, max = 1),
(min = 1, max = 2) and
(min = 0, max = 1).
minimums are (0,1,0).
yes, it's the same pattern again. then we need two loops again one to start from 0 and increase the number until reach 1 and the other one to loop from 0 to 0.
and that's what happened in the patternGenerator function. when you call it with the number of distinct numbers the function just get the min that will always be 0 in your case and the max.
function patternGenerator($numberOfDistinct )
{
echo "<table border =\"1\" style='border-collapse: collapse'>";
$min = 0;
$max = $numberOfDistinct - 2;
for($i = $min;$i<=$max; $i++)
{
rowGenerator($i,$i+1);
}
for($i = $max-1;$i>=$min;$i--)
{
rowGenerator($i,$i+1);
}
echo '</table>';
}
this is the output of calling patternGenerator(3):
the output of calling patternGenerator(5):

php display array data in multiple columns by using ceil function

For example, I have a array with 8 data. I want to display in this format
$a = array(1,2,3,4,5,6,7,8);
======
1 2
3 4
5 6
7 8
=====
OR array with 11 data
$a = array(1,2,3,4,5,6,7,8,9,10,11);
=========
1 2 3
4 5 6
7 8 9
10 11
=========
Not sure which part i got wrong. Here is my code
$a = array(1,2,3,4,5,6,7,8);
$c = ceil(count($a)/2);
echo "<table>";
for($i=0; $i<$c;$i++){
echo "<tr>";
echo '<td>'.$a[$i].'</td>';
echo '<td>'.$a[$i+1].'</td>';
}
echo "</table>";
However, my data display in this way instead
======
1 2
2 3
3 4
4 5
=====
Basically, I want to display my data from mysql in this format. But before that, I need to test out the ceil function and see if its working. Anyone knows whats wrong with my coding?
Since you have 2 cels per row you need to calculate the actual index. Also don't forget to close the <tr>
for($i=0; $i<$c;$i++){
echo "<tr>";
echo '<td>'.$a[$i * 2].'</td>';
echo '<td>'.$a[$i * 2 + 1].'</td>';
echo '</tr>';
}
Here is a full example of a cleaner solution:
<?php
$a = array(1,2,3,4,5,6,7,8);
$cols = 2;
$c = ceil(count($a) / $cols);
echo "<table>";
for($i = 0; $i < $c; $i++){
echo "<tr>";
for ($col = 0; $col < $cols; $col++) {
$value = isset($a[$i * $cols + $col]) ? $a[$i * $cols + $col] : '';
echo '<td>'. $value .'</td>';
}
echo "</tr>";
}
echo "</table>";
If you increase it manually you have to skip 1 loop every time.
$a = array(1,2,3,4,5,6,7,8,9,10,11);
$size = sizeof($a);
/* Your magic method to determine the total table cols.
*/
$cols = 3;
#$cols = 2;
echo "<table>";
for($i = 0; $i < $size; $i+=$cols){
echo "<tr>";
for($c = 0; $c < $cols; $c++){
if($i+$c >= $size){
break;
}
echo '<td>'.$a[$i+$c].'</td>';
}
echo "</tr>";
}
echo "</table>";
I've used the method of math to determine if it should print or not because it is faster. However, if you have empty indexes in the array you might want to use the isset() method as the whole code in the inner for loop.
if(isset($a[$i+$c])){
echo '<td>'.$a[$i+$c].'</td>';
}

Change for in PHP with table

<table><tr>
<?php
for($i=0;$i<15;$i++) {
if($i%5 == 0) {echo '</tr> <tr>';}
?><td><?php echo $i ?></td>
<?php
}?>
</tr>
</table>
this generate:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
how can I make:
0 3 6 9 12
1 4 7 10 13
2 5 8 11 14
?
You need a nested loop
<table>
<?php
$rows = 3;
for($i=0 ; $i < $rows ; $i++ ) {
echo "<tr>";
for( $j = 0 ; $j < 5 ; $j++ ) {
echo "<td>" . ($j * $rows + $i) . "</td>";
}
echo "</tr>";
}
?>
</table>
I tried to make the variable names descriptive:
<table>
<?php
// Set the number of rows, cols, and starting number here:
$number_rows = 3;
$number_cols = 5;
$starting_num = 0;
// You can use foreach w arrays... much easier
$rows = range(0,$number_rows - 1);
$cols = range(0,$number_cols - 1);
foreach($rows as $one_row) {
?>
<tr>
<?php
foreach($cols as $one_col) {
// Do the calculation
echo "<td>" .
($starting_num + $one_col + ( max($rows) * $one_col ) + $one_row) .
"</td>";
}
?>
</tr>
<?php
}
?>
</table>
Working example like in the OP.
Now let's say you want to go from (1300-1431), then you start at 1300 and want a 4 x 8.
$number_rows = 8;
$number_cols = 4;
$starting_num = 1300;
Like this.
There are two "tricks."
The first is using range() to quickly define an array integers. I find arrays more pleasant to work with than raw numbers, since arrays can be worked on with the intuitive construct of foreach().
The second is figuring out how to know what number to print if you have the column, row, and starting, number. It's simplest to figure out the numbers for the first row and go from there. Let's number the rows and cols from 0. So col:0 row:0 will be the starting number. Col:1 Row:0, the number to the right, will just be the starting number + one (the column number) + the number of rows less one (easiest to see by looking at the matrix of number), and the number of rows less one is the maximum number in the rows array. So for the first row we have:
$starting_num + $one_col + ( max($rows) * $one_col )
then all we just add the row number to take that into account, and we've got it all:
$starting_num + $one_col + ( max($rows) * $one_col ) + $one_row
Tested example with given start and end:
<table>
<?php
$start = 1300;
$end = 1432;
$n = $end - $start + 1;
$cols = 5;
$rows = ceil($n / $cols);
for($i=0 ; $i < $rows ; $i++ ) {
echo "<tr>";
for( $j = 0 ; $j < $cols ; $j++ ) {
$val = $j * $rows + $i;
echo "<td>";
echo ($val < $n) ? $val + $start : ' ';
echo "</td>";
}
echo "</tr>";
}
?>
</table>

Categories