PHP position output on specific table row - php

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>';

Related

PHP: for loop $i value turns zero if the number is greater

I want to turn the $i variable value to start counting from 1 if the given value is greater than 10:
here is what i am trying to achieve
<?php
$givenValue = 15; //number of x value
for ($i = 1; $i < $givenValue; $i++) {
if ($givenValue > 10){
$i = 1;
}
echo $i."<br>";
}
?>
This is how i want my result to look like
output: 1
output: 2
output: 3
output: 4
output: 5
output: 6
output: 7
output: 8
output: 9
output: 10
output: 1
output: 2
output: 3
output: 4
output: 5
in for loop body
Any help is welcome
You can use modulo calculation to get the result you want.
I also changed your if from $givenvalue to $i as $givenvalue will "always" be 10+.
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++) {
if ($i > 10){
Echo $i%10 . "\n";
}else{
echo $i . "\n";
}
}
https://3v4l.org/5afc5
Another option, if that is possible for you, is to start at zero and only use modulo calculation and add one to it to get the same result.
This also means I need to stop the loop at <$givenvalue as your original code shows.
$givenValue = 15; //number of x value
for ($i = 0; $i < $givenValue; $i++) {
Echo $i%10+1 . "\n";
}
https://3v4l.org/r0sgA
A method that uses less looping is to add 10 to the loop on each iteration and create the values using range().
Then add them to the array with array_merge, and output with implode.
$givenValue = 47; //number of x value
$breakpoint = 10;
$arr=[];
For($i = $breakpoint; $i< $givenValue;){
// Add new values from 1-$breakpoint in array
$arr = array_merge($arr, range(1,$breakpoint));
$i +=$breakpoint;
}
// Loop will exit before all values been collected
// Add the rest of the values
$arr = array_merge($arr, range(1,$givenValue-($i-10)));
// Echo the values in array
Echo implode("\n", $arr);
https://3v4l.org/jGsO4
Your code can be written like this:
<?php
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++)
{
if ($i > 10)
{
$i = 1;
$givenValue-=10;
}
echo "output: $i\n";
}
?>
http://sandbox.onlinephpfunctions.com/code/ed34d8dcd12a9a5a866b73338ad1209f55298519
You are resenting the counter, I would expect the behaviour you have. To do what you want add another counter to the mix
$j=1;
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++) {
if ($j > 10){
$j = 1;
}
echo $j."\n";
++$j;
}
You also had several missing ;
Output:
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
If you want to end on 5 you have to do 16 as the $givenValue or change it to <= less than or equal
See it here live
See what I have now, the $i variable counts to the $givenValue then the $j variable counts along side it, but with a range of 1-10 ( resets to 1 after 10 )

How to make this Number Pyramid Pattern

is it possible if I want an output like this?
5 4 3 2 1
4 3 2
3
with PHP, this is the code that I've been Try.
$n = 3;
for ($i = 3; $i > 0; $i--) {
for ($j = $n - $i; $j > 0; $j--)
echo "  ";
for ($j = 2 * $i - 1; $j > 0 ; $j--)
echo " ".$j;
echo "<br>";
}
and I got this , from that code
5 4 3 2 1
3 2 1
1
Wich part of my code that I do get wrong? can someone help me?
EDIT: Thanks people. most of the stackoverflow question that similar to my question is like my result that I have. the task was not that. 54321 432 3
most of them we're like 54321 4321 321 21 1. I'm sorry , I'm newbie. don't know that much as you people. once again , Thanks Alot!
You need to fix your last for loop's initial value and condition to match your criteria:
$n = 3;
for ($i = 3; $i > 0; $i--) {
for ($j = $n - $i; $j > 0; $j--)
echo " ";
for ($j = $n + $i - 1; $j > $n - $i ; $j--)
echo " ".$j;
echo "<br>";
}

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

Non-recursive dynamic binary tree with 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.

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