PHP Skip something a certain amout of times in loop - php

I have a ton of for loops for generating a table, with results from my MySQL DB. Some of the more important information are the start and end date. I calculate how many days this absence has besides the start date(For example 02.03.2015 - 04.03.2015, 2 days). I save this result in $difference, however I get this result during a for loop, so I cant simply go to the for loop and say $xyz - difference.
So I thought about skiping the <td>'s in the loop, as many times as the difference.
Here in the picture you see that the <td>'s are off the amount of days the absence lasts.
How can I skip them as many times as the difference, so the tables looks right again?
My code is still a mess so no comments about that:
echo '<table class="table table table-bordered table-striped table-hover">';
echo '<thead>';
for ($l = 0; $l < $days; $l++) {
if ($l == 0) {
echo '<th>', '<b>Day</b>', '</th>';
} else {
$date = "$current-$month-$l";
$date = date('D', strtotime($date));
$date = substr($date, 0, -1);
echo '<th class="center">', $date,'</th>';
}
echo "\n";
}
echo '</thead>';
echo '<tbody>';
for ($l = 0; $l < $days; $l++) {
if ($l == 0) {
echo '<td>', '<b>Onshore</b>', '</td>';
} else {
echo '<th class="center">', $l, '</th>';
}
echo "\n";
}
for ($i = 0; $i < $count_user; $i++) {
echo '<tr>';
$result = mysql_query("select start, end, type_FK, employee_FK FROM absences where employee_FK = {$array_user[$i]['employee_ID']} and MONTH(start) = $month and YEAR(start) = $current");
while ($row = mysql_fetch_assoc($result)) {
$array_absences[] = $row;
}
$count = 0;
if (!empty($array_absences)) {
$count = count($array_absences);
}
for ($j = 0; $j < $days; $j++) {
$true = 0;
if ($j == 0 && $i == $count_on) {
echo '<td>';
echo '<b>Offshore</b>';
echo '</td>';
for($k = 0; $k < $days -1; $k++){
echo '<td>';
echo '</td>';
}
echo '</tr>';
}
if ($j == 0) {
echo '<td>';
echo $array_user[$i]['name'], ' ', $array_user[$i]['surname'];
echo '</td>';
}
for ($k = 0; $k < $count; $k++) {
$array_absences[$k]['start'] = substr($array_absences[$k]['start'], -2);
$array_absences[$k]['end'] = substr($array_absences[$k]['end'], -2);
$array_absences[$k]['start'] = ereg_replace("^0", "", $array_absences[$k]['start']);
$array_absences[$k]['end'] = ereg_replace("^0", "", $array_absences[$k]['end']);
$difference = $array_absences[$k]['end'] - $array_absences[$k]['start'];
if ($j == $array_absences[$k]['start'] && $array_absences[$k]['employee_FK'] == $array_user[$i]['employee_ID']) {
$true = 1;
$result = mysql_query("select approved from absences where DAY(start) = $j and MONTH(start) = $month");
while ($row = mysql_fetch_assoc($result)) {
$approved[] = $row;
$n++;
}
$now = date('Y-m-d');
$absence = strtotime("$current/$month/$j");
$absence = date('Y-m-d',$absence);
for ($q = 0; $q < $difference+1; $q++) {
if ($approved[$n]['approved'] == 1) {
echo '<td class="center green">';
} elseif ($approved[$n]['approved'] == 0 && $now <= $absence) {
echo '<td class="center orange">';
} elseif ($approved[$n]['approved'] == 0 && $now > $absence) {
echo '<td class="center red">';
}
for ($l = 0; $l < $count_types; $l++) {
if ($array_absences[$k]['type_FK'] == $types[$l]['type_ID']) {
echo $types[$l]['short'];
}
}
echo '</td>';
}
}
}
//Days that are not absences
//Skip this the amounts of $difference
echo '<td ';
//If weekend special coloring
$date = "$current-$month-$j+1";
$date = new DateTime($date);
$day = $date->format("w");
if ($day == 6 || $day == 0) {
echo 'class = "weekend"';
}
echo '>';
echo '</td>';
echo "\n";
}
echo '</tr>';
}

I'm afraid I did not really get the sense of your explanations, however, to skip something in a loop, you can use the continue keyword for this.
For example :
for($i = 0; $i++; i<1000){
if($i < 200 && $i > 100)
continue;
// Computations are here....
}

Related

Dynamic Table with select onChange Event not working

I have a table, with 12 lines (rows) and 10 columns, for columns odd lines (rows), I have one div inside of cell, with index in Id NumColum_NumLine, and for pair lines (rows), i have one select element equal for all. like this enter image description here
What i need is when i select load image on div above.
<table border="solid">
<?php
for ($i = 1; $i <= 12; $i++) {
echo "<tr>";
for ($A = 1; $A <= 10; $A++)
{
if ($i % 2 == 0)
{
echo '<td id="data">';
$songs = file('pkmlist.txt');
$options = '';
$num = 1;
foreach ($songs as $song) {
$options .= '<option value="'.$num.'" onchange="load_img('.$A.'_'.($i-1).')\">'.$song.'</option>';
$num++;
}
$select = '<select name="songs">'.$options.'</select>';
echo $select;
//echo ''.$A.'_'.($i-1).'</td>';
}
else
{
echo '<td id="Pkm"> <div id="'.$A.'_'.$i.'"> '.$A.'_'.$i.' </div> </td>';
}
}
echo "</tr>";
}
?>
</table>
OnChange don't work.
There I found some syntax error. try this ...
<table border="solid">
<?php
for ($i = 1; $i <= 12; $i++) {
echo "<tr>";
for ($A = 1; $A <= 10; $A++)
{
if ($i % 2 == 0)
{
echo '<td id="data">';
$songs = file('pkmlist.txt');
$options = '';
$num = 1;
foreach ($songs as $song) {
$options .= '<option value="'.$num.'">'.$song.'</option>';
$num++;
}
$select = '<select name="songs" onchange="load_img('.$A.'_'.($i-1).');" >'.$options.'</select>';
echo $select;
//echo ''.$A.'_'.($i-1).'</td>';
}
else
{
echo '<td id="Pkm"> <div id="'.$A.'_'.$i.'"> '.$A.'_'.$i.' </div> </td>';
}
}
echo "</tr>";
}
?>
</table>

How to do a Diamond Pattern / Shape (asterisks) inside a table? (html + php)

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

Creating a multiplication table in PHP

I'm having a little trouble figuring out why there's an extra "0" box on my multiplication table, and here's the code that I have so far:
$cols = 10;
$rows = 10;
$number = 0;
$number2 = 0;
echo "<table border=\"1\">";
for ($r = 0; $r < $rows; $r++){
echo('<tr>');
if ($r == 0) {
for ($i = 0; $i < $rows; $i++) {
echo('<td>' .$number2++.'</td>');
}
}
for ($c = 0; $c < $cols; $c++){
if ($c == 0) {
echo('<td>' .$number++.'</td>');
} else if ($r != 0) {
echo( '<td>' .$c*$r.'</td>');
}
}
echo('</tr>');
}
echo("</table>");
So far it looks good, but that extra 0 on the first row is bothering me. Also I would like to keep the original format of the multiplication table if possible.
Here is:
$cols = 10;
$rows = 10;
$number = 1;
$number2 = 0;
echo "<table border=\"1\">";
for ($r = 0; $r < $rows; $r++){
echo('<tr>');
if ($r == 0) {
for ($i = 0; $i < $rows; $i++) {
echo('<td>' .$number2++.'</td>');
}
}
for ($c = 0; $c < $cols; $c++){
if ($c == 0 && $r != 0) {
echo('<td>' .$number++.'</td>');
} else if ($r != 0) {
echo( '<td>' .$c*$r.'</td>');
}
}
echo('</tr>');
}
echo("</table>");
You have a progression from 0 to 10. But, in the first td of the second for, you should not start from 0, you need to start from 1, or the 0 will be showed at the end of the first row. It's becase you already started the first row using the if, so the second one will repeat it.
You just need to check if the $r is 0 (to avoid repeat the first row) and start the $number from 1 (to follow the same logic, but starting from 1).
How about this:
$cols = 10;
$rows = 10;
$number = 0;
$number2 = 0;
echo "<table border=\"1\">";
for ($r = 0; $r <= $rows; $r++){
echo('<tr>');
if ($r == 0) {
for ($i = 0; $i < $rows; $i++) {
echo('<th>' .$number2++.'</th>');
}
}
for ($c = 0; $c <= $cols; $c++){
if ($c == 0) {
echo('<th>' .$number++.'</th>');
} else if ($r != 0) {
echo( '<td>' .$c*$r.'</td>');
}
}
echo('</tr>');
}
echo "</table>";

Max, Min and sum Random Number php

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

PHP - Dynamic 3 column result table

I have this script which produces a 3 column table of results:
$cols = 3;
$row = 0;
$column = 0;
$count = 0;
echo '<table>';
while($row = mysql_fetch_assoc($result)) {
$count++;
if ($column == 0) {
echo '<tr>';
}
echo "<td>$row['dbField']</td>";
$column++;
if ($column >= $cols) {
$row++;
echo '</tr>';
$column = 0;
}
}
echo '</table>';
This works okay, except if there's only 1 result, it only prints one cell. I would like it to finish off the row of 3, so in this case, two cells would be empty.
I have the total number of records stored in a session variable $_SESSION['r_count'] and thought it would be fairly simple to add the following snippet after the $column++ part of the above:
if ($count == $_SESSION['r_count'] && $column < $cols) {
echo '<td></td>';
$column++;
}
I thought wrong. Could anyone advice me on how to modify this correctly?
If you know the number of rows (i.e. mysql_num_rows()):
for ($i = 0, $n = ceil($nr_of_rows / $cols); $i != $n; ++$i) {
$row = mysql_fetch_assoc($result);
if ($i % $cols == 0) { echo '<tr>'; }
echo '<td>', $row ? htmlspecialchars($row['dbField']) : '', '</td>';
if (($i + 1) % $cols == 0) { echo '</tr>'; }
}

Categories