star pattern using ternary operator - php

<?php
for ($row = 1; $row <= $_POST["number"]; $row++)
{
for ($col = 1; $col <= ($row >= ($_POST["number"]/2) ? ($_POST["number"]+1)- $row : $row); $col++)
{
echo '* ';
}
echo "<br>";
}
print(json_encode(count($row)));
?>
the question is to print the pattern and also the total number of stars in each row.
i tried degugging my self but if i change the condition and only for some inputs the answer is correct.

You can use the following:
<?php
$_POST["number"] = 10;
$row_num = array();
for ($row = 1; $row <= $_POST["number"]; $row++)
{
$count = 0;
for ($col = 1; $col <= ($row >= ($_POST["number"]/2) ? ($_POST["number"]+1)- $row : $row); $col++)
{
$count++;
echo '* ';
}
echo "<br>";
array_push($row_num, $count);
}
print_r ($row_num);
?>

Related

PHP get min and max values from 50 random numbers?

<?php
echo "<table border='1'><br />";
for ($row = 0; $row < 10; $row ++) {
echo "<tr>";
for ($col = 0; $col < 5; $col ++) {
$rand = rand (1, 200);
echo "<td>", $rand, "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Here is code but my question is how can I find max and min from values of the table? Do I have to make random numbers somehow into array?
You can do as following without changing much of your code
<?php
$array = array(); // <-- added code
echo "<table border='1'><br />";
for ($row = 0; $row < 10; $row ++) {
echo "<tr>";
for ($col = 0; $col < 5; $col ++) {
$rand = rand(1, 200);
$array[] = $rand; // <-- added code
echo "<td>", $rand, "</td>";
}
echo "</tr>";
}
echo "</table>";
$min = min($array); // <--- added code
$max = max($array); // <--- added code
?>
Check it out the max and min value of an aray in PHP using library function.
for($col = 0; $col < 5; $col ++) {
$rand[] = rand (1, 200);
}
echo $max = max($rand)."<br/>";
echo $min = min($rand)."<br/>";
print_r($rand);

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

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

How do I simplify this basic loop?

I have code which pretty much does this.....
//get the row info
$Row1 = $FullTable->find('div[class=ismPitchRow1]',0);
$Row2 = $FullTable->find('div[class=ismPitchRow2]',0);
$Row3 = $FullTable->find('div[class=ismPitchRow3]',0);
$Row4 = $FullTable->find('div[class=ismPitchRow4]',0);
$Row5 = $FullTable->find('div[class=ismPitchRow5]',0);
//Loop 5 times. One for each row on the pitch.
for ($i=1; $i<=5; $i++)
{
if ($i = 1) { echo $Row1; }
if ($i = 2) { echo $Row2; }
if ($i = 3) { echo $Row3; }
if ($i = 4) { echo $Row4; }
if ($i = 5) { echo $Row5; }
}
It works, but as you can see it's not very efficient and badly designed. How would I simplify this? I know there are much smaller ways that these kind of loops can be done.
Thanks.
use the great invention of arrays:
//get the row info
$Row[1] = $FullTable->find('div[class=ismPitchRow1]',0);
$Row[2] = $FullTable->find('div[class=ismPitchRow2]',0);
$Row[3] = $FullTable->find('div[class=ismPitchRow3]',0);
or, even more clever...
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
$Row[$i] = $FullTable->find($find,0);
}
do the same for echoing:
for ($i = 1; $i <= 5; $i++) {
echo $Row[$i];
}
but why not do everything in 1 loop?
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
echo $FullTable->find($find,0);
}
I would store $Row1 through $Row5 in an array and iterate through it with a foreach loop.
<?php
$YourArray = array();
array_push($YourArray,$FullTable->find('div[class=ismPitchRow1]',0),$FullTable->find('div[class=ismPitchRow2]',0),$FullTable->find('div[class=ismPitchRow3]',0),$FullTable->find('div[class=ismPitchRow4]',0),$FullTable->find('div[class=ismPitchRow5]',0));
foreach($YourArray as $row){
echo $row;
}
?>

Categories