Creating a multiplication table in PHP - 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>";

Related

How can received and clear an array in PHP

I'm trying to look for a number with maximum divisors in a range of 1 - 10000.
I succeeded, but then I wish to verify if there exist more than two max divisors and print them out. My array is really the problem. How can I clear an array and assign a new integer to it in an if else if statement?
Here is what I have tried:
function countDivisors(){
$input = 10000;
$maxNumOfDiv = -1;
$intWMaxDivs = -1;
$curNumOfDiv = 0;
$arr = array();
for($i=1; $i <= $input; $i++) {
$curNumOfDiv = 0;
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0)
$curNumOfDiv++;
}
if($curNumOfDiv = $maxNumOfDiv){
$arr[] = $i;
$intWMaxDivs = $i;
$maxNumOfDiv = $curNumOfDiv;
} else if($curNumOfDiv > $maxNumOfDiv){
$arr = array();
$arr[] = $intWMaxDivs
$maxNumOfDiv = $curNumOfDiv;
}
}
for ($i; $i < count($arr); $i++){
echo $arr[$i]['intWMaxDivs'];
echo $arr[$i]['maxNumOfDiv'];
}
$div = [];
$maxDivKey = false;
$maxDiv = 0;
for($i = 1; $i <= 10000; $i++) {
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0){
$div[$i][] = $i.'/'.$j.'='.$i/$j;
}
if($j == $i-1){
$count = count($div[$i]);
$div[$i]['count'] = $count;
if($maxDiv < $count){
$maxDiv = $count;
$maxDivKey = $i;
}
}
}
}
echo '<h1>Max divisors:</h1>';
print_r($div[$maxDivKey]);
//print_r($div);
I may be misunderstanding this question a little. If you are looking for a single number with maximum number of dividers, it should be something like this.
<?php
$max_num=10000;
$start_num=1;
$max_divs=-1;
$max_number=-1;
$numbers=array();
$max_divs_arr=array();
for($i=$start_num;$i<=$max_num;$i++)
{
$divs=0;
$div_array=array();
for($j=$start_num;$j<=$i;$j++)
{
if($i%$j==0)
{
$divs++;
$div_array[]=$j;
}
}
if($divs==$max_divs)
$max_divs_arr[$i]=$div_array;
if($divs>$max_divs)
{
$max_divs_arr=array();
$max_divs=$divs;
$max_divs_arr[$i]=$div_array;
}
}
foreach($max_divs_arr as $number=>$divisors)
echo "\nNumber with most divisors is $number\nIt has $max_divs divisors\nThose divisors are:".implode(',',$divisors);

Find the largest three elements in an array

Find the largest three elements in an array, Given an array with all distinct elements, find the largest three elements. Expected time complexity is O(n) and extra space is O(1)
<?php
$number = array(1,2,3,4,5,6,7,8,9,10);
print_r($number);
echo "<br>";
$biggest_number_1 = 0;
$biggest_number_2 = 0;
$biggest_number_3 = 0;
for ($i=0; $i < count($number); $i++){
if($number[$i] > $biggest_number_1){
$biggest_number_1 = $number[$i];
}
if($number[$i] > $biggest_number_2 && $number[$i] != 10){
$biggest_number_2 = $number[$i];
}
if($number[$i] > $biggest_number_3 && $number[$i] != 10 && $number[$i] != 9){
$biggest_number_3 = $number[$i];
}
}
echo $biggest_number_1."<br>";
echo $biggest_number_2."<br>";
echo $biggest_number_3;
?>
Simpliest way should be something like that :
$number = array(1,2,3,4,5,6,7,8,9,10);
rsort($number); // order array desc
// Just echo first 3 result in your array
echo $number[0]."<br>";
echo $number[1]."<br>";
echo $number[2];
Now if you want to loop through to your array to get the result, you can try this :
$number = array(1,2,3,4,5,6,7,8,9,10);
$biggest_number_1 = $biggest_number_2= $biggest_number_3 = 0;
foreach ($number as $nb) {
if ($nb > $biggest_number_1) {
$biggest_number_3 = $biggest_number_2;
$biggest_number_2 = $biggest_number_1;
$biggest_number_1 = $nb;
} else if ($nb > $biggest_number_2) {
$biggest_number_3 = $biggest_number_2;
$biggest_number_2 = $nb;
} else if ($nb > $biggest_number_3) {
$biggest_number_3 = $nb;
}
}
echo $biggest_number_1."<br>";
echo $biggest_number_2."<br>";
echo $biggest_number_3;
Just sort the array by ascending and take last 3 element of array.
Try this:- This is perfectly work
$number=array(1,2,3,4,5,6,7,8,9,10);
$biggest_number_1 = $biggest_number_2= $biggest_number_3 = 0;
for ($i = 0; $i < sizeof($number) ; $i ++)
{
if ($number[$i] > $biggest_number_3 )
{
$biggest_number_1 = $biggest_number_2;
$biggest_number_2 = $biggest_number_3 ;
$biggest_number_3 = $number[$i];
}
else if ($number[$i] > $biggest_number_2)
{
$biggest_number_1 = $biggest_number_2;
$biggest_number_2 = $number[$i];
}
else if ($number[$i] > $biggest_number_1){
$biggest_number_1 = $number[$i];
}
}
echo $biggest_number_1."<br>";
echo $biggest_number_2."<br>";
echo $biggest_number_3;

PHP Skip something a certain amout of times in loop

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....
}

print number vertically and grouping it

I am trying to print number vertically and it must be in group
here is my code
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= 24; $j = $j + $rows) {
$count++;
if($count>$nums){
break;
}
echo "<div class='fleft'>$count</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
out of above
but i want output like for the first column
and next group number will start from where first group number end. in this case next group start from 25
please ask if any doubt
$nums = 105;
$rows = 8;
$colsize = 3;
$col = floor($nums / $rows);
$group = floor($col / $colsize);
$count = 0;
$groupsize = $rows * $colsize;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
$modulo = 0;
$correction = 0;
$rest = $nums - $count;
if ($rest < $groupsize) {
$empty = $groupsize - $rest;
$correction = floor($empty / $colsize);
$modulo = $empty % $colsize;
}
for ($i = 1; $i <= $rows; $i++) {
$colind = 0;
for ($j = $i; $j <= $groupsize; $j = $j + $rows) {
$count++;
if ($count > $nums) {
break;
}
$val = $j + ($g * $groupsize);
$val -= $colind * $correction;
$modcor = $colind - ($colsize - $modulo);
if ( $modcor > 0 ) {
$val -= $modcor;
}
echo "<div class='fleft'>" . $val . "</div>";
$colind++;
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
This works:
Also, you can change number of digits, columns or size of column
for($group = 0; $group < 3; $group++){
for($row =1 ; $row <= 8; $row++){
for($col = 0; $col <= 2; $col++){
echo ($group*24)+ $row + 8 * $col; echo " ";
}
echo "\n";
}
}
This code will print the number in the requested format. You need to modify according to your need.
may be i am mad , made a simple alter .... try this
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$letCounter=0; //added a counter
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
$letCounter=0; //reset counter on each loop
for ($j = $i; $j <= 24; $j = $j + $rows)
{
$count++;
if($count>$nums)
{break;}
//made an alter in the below line , some math :)
echo "<div class='fleft'>".($letCounter++ * $rows +$i)."</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
Thanks !
This May work
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$flag = true;
for($c=1;$c<=$col;$c++)
{
if($c%$group== 1)
{
echo "Group Start";
$flag = false;
}
for ($i = 1; $i <= $rows; $i++) {
$count++;
echo "<div class='fleft'>$count</div>";
echo "<div class='clear'></div>";
}
echo "Line End";
if($c%$group == 2&& $flag)// Check here for your requirement
echo "Group End </br>";
$flag = true;
}

create the letter T with PHP code

I need to create the Letter T with PHP code:
This is what I have so far but can't seem to figure how to just have the asterisks on the top two lines in order to extend the top of the T:
<?php
echo "<pre>";
for ($row > 2; $row < 15; $row++) {
for ($column = 2; $column < 12; $column++) {
if (($row < 2 || $row < 2) || ($column < 2 || $column >= 6)) {
echo "*";
}
else echo " ";
}
echo "\n";
}
echo "</pre>";
?>
You have several bugs in your code.
for ($row > 2; $row < 15; $row++) {
for ($column = 2; $column < 12; $column++) {
why do you use $row > 2 and $column = 2 ? Just start from zero.
if (($row < 2 || $row < 2) || ($column < 2 || $column >= 6)) {
Why do you check if $row < 2 is true or $row < 2 is true if they are the same?
Here is an example:
echo "<pre>";
for($i=0; $i <= 10; $i++){
for($j = 0; $j < 10; $j++){
if($i > 2 && ($j < 3 || $j > 6)){
echo " ";
}else{
echo "*";
}
}
echo "\n";
}
for ($row > 2; $row < 15; $row++) {
This condition is wrong, and should be:
for ($row = 0; $row < 15; $row++) {
And:
if (($row < 2 || $row < 2)
is wrong and doesn't do what you probably think it does.
The code in the thread j08691 linked you to, contains the correct solution and you could use that:
<?php
echo "<pre>";
for ($row = 0; $row < 15; $row++) {
for ($column = 0; $column <10; $column++) {
if (($row < 1 || $row > 15) ||( $column == 4)) {
echo "*";
}
else echo " ";
}
echo "\n";
}
echo "</pre>";
?>
See the live demo.
start your for loop with $row = 0
for ($row = 0; $row < 15; $row++) {
you were ignoring the first 2 lines and only drawing the vertical line
Also ($row < 2 || $row < 2) is the same as $row < 2
#j08691 found your exact question if you want more info

Categories