dynamic table numbers php - php

I still don't get how to create a dynamic table using PHP.
I would like to display a table with a variable amount of columns and rows containing numbers between 1 and 200.
Example of the expected output
Can anyone help me with this?

A nested loop within an outer loop should do it - the outer loop controlling the rows and the inner loop controlling the columns.
$rows=20;
$cols=10;
$x=1;
$html=array();
$html[]="<table>";
for( $i=0; $i < $rows; $i++ ){
$html[]="<tr>";
for( $j=0; $j < $cols; $j++ ){
$html[]="<td data-cell='r{$i}c{$j}'>$x</td>";
$x++;
}
$html[]="</tr>";
}
$html[]="</table>";
echo implode( PHP_EOL, $html );
To cease creation when a pre-defined limit is reached
$rows=100;
$cols=10;
$x=1;
$limit=200;
$html=array();
$html[]="<table>";
for( $i=0; $i < $rows; $i++ ){
$html[]="<tr>";
for( $j=0; $j < $cols; $j++ ){
$html[]="<td data-cell='r{$i}c{$j}'>$x</td>";
$x++;
if( $x > $limit ) break;
}
$html[]="</tr>";
if( $x > $limit ) break;
}
$html[]="</table>";
echo implode( PHP_EOL, $html );

<?php
for($i=1;$i<=30;$i++) {
echo "*$i*";
if($i%10==0){
echo "<br>";
}
}
LIVE DEMO

Related

How to optimize code that multiplies every row and column in multidimensional array?

I'm trying to find a way to optimize my code, and I have difficulties in finding a right way. First I'm adding numbers in empty array and after that, I'm multiplying every row and column using pure logic, without any php function that could help me out. For now I've tried to calculate rows and columns in one nested for loop, but it doesn't work.
$nxmArr = [];
$rows = 4;
$cols = 4;
$value = 1;
for( $row = 0; $row < $rows; $row++ )
{
for( $col = 0; $col < $cols; $col++ )
{
$nxmArr[$row][$col] = $value++;
}
}
for( $row = 0; $row < $rows; $row++ )
{
$colSum = 1;
for( $col = 0; $col < $cols; $col++ )
{
echo $nxmArr[$row][$col]." ";
$colSum *= $nxmArr[$row][$col];
}
echo "Col Sum: $colSum<br>";
}
echo "<br>";
for( $col = 0; $col < $cols; $col++ )
{
$rowSum = 1;
for( $row = 0; $row < $rows; $row++ )
{
echo $nxmArr[$row][$col]." ";
$rowSum *= $nxmArr[$row][$col];
}
echo "Row Sum: $rowSum<br>";
}
You can approach this by using
$nxmArr = [];
$rows = 4;
$cols = 4;
$value = 1;
$flag = 0;
for( $row = 0; $row < $rows; $row++ )
{
for( $col = 0; $col < $cols; $col++ )
{
$nxmArr[$row][$col] = $value++;
$flag += $nxmArr[$row][$col];
($row > 0)
?
($colns[$col] += $nxmArr[$row][$col])
:
($colns[$col] = $nxmArr[$row][$col])
;
}
$rowSum[] = $flag;
$flag = 0;
}
echo 'Columns Sum :-';
print_r($colns);
echo '<hr>';
echo 'Rows sum :-';
print_r($rowSum);
Live Demo :- https://3v4l.org/d3ml4
Explanation :-
$flag += $nxmArr[$row][$col]; This will do the row elements addition one by one and when the single row traversed, assign sum to an array element i.e. $rowSum[] = $flag; and make the flag element 0 i.e. $flag=0
($row > 0)
?
($colns[$col] += $nxmArr[$row][$col])
:
($colns[$col] = $nxmArr[$row][$col])
When the $row=0, the code ($colns[$col] = $nxmArr[$row][$col]) will be executes and it will keep the values like $colns[0]=1,$colns[1]=2,$coln[2]=3,$colns[3]=4, Now when $row>0 , it will add the new values to indices 0,1,2,3

How to work with nested loop in laravel

Recently I am working with nest for loop but one loop work and another not working suppose I have 2 for loop.
For example
$data =DB::table('data')->get();
$job =DB::table('job')->get();
$recruiter =DB::table('recruiter')->get();
$admin =DB::table('commission')->get();
for($i=0;i<count($job);i++){
if(!$job->isEmpty()){
for($j=0;j<count($job);j++){
if( $data[$i]->job_id == $admin[$j]->job_id )
$job[$i]=$data[$i];
}
//if checking complete then skip $i or increment it (less than count($job)) or skip
//this index $i and continue with outer loop mean i++
}
}
I had done lots of research but haven't found any solution with this kind of problem
You have missed the $ in i and j
$data = DB::table('data')->get();
$job = DB::table('job')->get();
$recruiter = DB::table('recruiter')->get();
$admin = DB::table('commission')->get();
for($i=0; $i < count( $job ); $i++){
if($job->isEmpty()){
continue;
}
for($j=0; $j < count( $job ); $j++){
if( $data[$i]->job_id == $admin[$j]->job_id ){
$job[$i]=$data[$i];
}
}
}

Display array in a loop

Please tell me how can I display array in a for loop in php?
I have this array:
while($q = mysqli_fetch_assoc($selectQuestResult)) {
$answers[] = array(array($q['name']),array(round($total,1)));}
then I want display it:
for($i = 0; $i < count($answers); $i++)
{
echo($answers[$i][0][0].':'.$answers[$i][1][0]."<br />\n");
}
I must use a for loop makes this sound like stupid college professor tricks. But, that's ok. for loops can have break statements in them. And, they don't have to contain end conditions in the for statement.
for( $i = 0; ; $i++) {
$q = mysqli_fetch_assoc($selectQuestResult);
if (!$q) break;
$answers[] = array($q['name'], round( $total, 1));
}
for ( $i = 0; $i < count( $answers ), $i++) {
/* do something with $answers[i] */
}

Function call to set variable doesn't work

the following code is modified after suggestion does get the $i $j works well
<?php
$i=1;
$j=1;
function fixIndex() {
global $i, $j;
$a=$j-$i;
if ($a === 60){
$i += 60;
}
$j++;
}
but it does not work on my main code as follow:
$times = array();
$values1 = array();
$values2 = array();
$values3 = array();
$values4 = array();
$i=1;
$j=1;
$file_lines = file($DispFile, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
foreach( $file_lines as $line_num => $line_value) {
fixIndex();
if ($line_num < $i)continue; // skip records in 60* range
$line_elements = explode(",", $line_value);
$times[] = date("H:i:s", $line_elements[0]);
$values1[] = $line_elements[1];
$values2[] = $line_elements[2];
$values3[] = $line_elements[3];
$values4[] = $line_elements[4];
}
After the foreach loop I call for the fixIndex(), trying to get the $i value for the next line of code(if ($line_num < $i)continue;) to skip 60 records before i build an array. That $i doesn't seem to skip the record. If I change that $i to a number on it ($line_num < 60 )continue; Than it does skip the 60 records.
also is there any change of the program flow if this php program do a refresh on the every 10 sec on the web, What I mean is did $i and $j rest to 1 every time after refresh?
Thanks again to anyone for any help
You can use pass by reference to avoid needing global variables here. What that means is the function will modify the variables that were passed to it.
https://secure.php.net/manual/en/language.references.pass.php
<?php
function fixIndex(&$i, &$j) {
global $i, $j;
if ($j - $i == 5) {
$i += 5;
}
$j++;
}
$i=1;
$j=1;
for ($k=0;$k<=20; $k++){
fixIndex($i, $j);
echo $i.'<br />';
echo $j.'<br />';
}
?>
Also, I changed "$i + 5" (which doesn't make anything happen) to "$i += 5" so that it modifies the variable $i.
You are not storing your incremented value of $i.
<?php
$i=1;
$j=1;
function fixIndex() {
global $i, $j;
if ($j - $i == 5){
$i = $i+5;
}
$j++;
}
for ($k=0;$k<=20; $k++){
fixIndex();
echo $i.'<br />';
echo $j.'<br />';
}
Demo : https://eval.in/518511
You probably could describe what you want to do like this:"increment $i by 5 every time $j has grown by 5".This could be implemented in a straight and very compact form like this.(The solution covers all cases, and is not depending on the upper limit of the iteration.)
<?php
function fixIndex(&$i,$j) {
if ( ($j>1) && (($j-1) % 5 == 0) ) $i += 5;
}
$i = 1;
for ($j=1;$j<=20; $j++){
fixIndex($i,$j);
echo "\$j=$j, \$i=$i <br />";
}
Notes:
( ($j-1) % 5 == 0 )
…means: when $j -1 can be devided by 5 without a remainder (% 5 = "modulo 5").
function fixIndex(&$i,$j)
&$i means that a reference (pointer) to the memory of the $i variable is accepted instead of its value. This is necessary since the function is modifying the value of $i. So, you would not need the global statement.On the other hand the $j variable is not modified and thus can be accepted as a value.
Result:
$j=1, $i=1
$j=2, $i=1
$j=3, $i=1
$j=4, $i=1
$j=5, $i=1
$j=6, $i=6
$j=7, $i=6
$j=8, $i=6
$j=9, $i=6
$j=10, $i=6
$j=11, $i=11
$j=12, $i=11
$j=13, $i=11
$j=14, $i=11
$j=15, $i=11
$j=16, $i=16
$j=17, $i=16
$j=18, $i=16
$j=19, $i=16
$j=20, $i=16

PHP fill array into for

in this below code i want to fill array with for repeator. echo can display and not have problem but. my array could not fill by for.
<meta charset='UTF-8' />
<?php
error_reporting(1);
$handle='A-file.txt';
$handle = file_get_contents($handle);
$lines = explode(PHP_EOL,$handle );
$names = array();
for( $i = 0; count($lines)-1 ; $i+=4 )
{
$names[]= $lines[$i];
//$names= $lines[$i];
//$names[]+= $lines[$i];
//echo $lines[$i];
}
print_r($names);
?>
You've forgotten the comparison with $i:
for( $i = 0; $i <= count($lines)-1 ; $i+=4 )
{
$names[]= $lines[$i];
//$names= $lines[$i];
//$names[]+= $lines[$i];
//echo $lines[$i];
}
Try this, You have missed to add $i < count($lines)-1
for( $i = 0; $i < count($lines)-1; $i+=4 )
instead of
for( $i = 0; count($lines)-1 ; $i+=4 )
Check the file has more than 4 lines, and the for finishes with that condition maybe will be an eternal loop.
This is perhaps just a tip to solve the problem more likely.
Use file() (http://php.net/file) to directly read a file's content into an array (so you don't need to do it manually with more lines then needed) and iterate over these lines using foreach($lines as $i => $line) {...} instead. To skip lines you can do:
if($i % $nthElem !== 0) continue;
You could even do it in one turn:
foreach(file($yourFile) as $i => $line){
if($i % 4 !== 0) continue;
Always optimize your for loop by putting count function out of for loop and store it in a variable. Use that in the loop
$count = count($lines);
for( $i = 0; $count-1 ; $i+=4 ) {
}

Categories