Using loop to create a shape - php

I'm supposed to use nested for loops to create this shape: https://imgur.com/a/prh6zwj
This is what I currently have:
<?php
for ($x = 1; $x <= 10; $x++){
for ($y = 1; $y <= 6; $y++){
echo "Y";
}
}
?>
I have no clue what to do.
Thanks in advance!

<?php
$position = 1;
for ($x = 1; $x <= 11; $x++){
for ($y = 1; $y <= 6; $y++){
if ($y == $position) {
echo "Y";
} else {
echo "0";
}
}
if ($x < 6) {
$position++;
} else {
$position--;
}
echo "\n";
}

<?php
$length = 6; // change this to change height width
$pos = 0;
for ($x = 1; $x <= (($length*2)-1); $x++){
if($x <= $length)
{$pos = $pos+1; }
else
{$pos = $pos-1; }
for ($y = 1; $y <= $length; $y++){
if($y == $pos)
echo "Y";
else
echo "O";
}
echo "\n";
}

There are many possible ways to achieve this when I started programming I never cared about the code quality and just focused on the output. I have added two examples to help you understand it better!
<?php
//We have 6 columns & 11 rows so we need two loops one size of 11 and second size of 6
$counter = 1;
for ($i = 1; $i <= 11; $i++){
for ($j = 1; $j <= 6; $j++){
if ($j == $counter) {
echo "Y";
} else {
echo "O";
}
}
if ($i < 6) {
$counter++;
} else {
$counter--;
}
echo "<br/>";
}
echo "**************************** METHOD TWO ****************************";
//Following is not efficient But its also printing the same results
for ($i = 0 ; $i < 66 ; $i++){
if($i == 65)
{
echo "O";
break;
}
if($i % 6 == 0){
echo "<br/>";
}
if($i <= 36)
{
if ($i % 7 == 0){
echo "Y";
}else{
echo "O";
}
}else{
if ($i % 5 == 0){
echo "Y";
}else{
echo "O";
}
}
}
?>

$k=2; // for calculating position from backside
for($i=1;$i<=11;$i++) //for row
{
for($j=1;$j<=6;$j++) //column
{
if($j==$i && $i<=6) //logic for printing "Y" till the end of row
echo "Y";
else if($i>6 && $j==($i-$k)) //logic for printing "Y" in reversal order from the end of row
{
echo "Y";
$k+=2;
}
else
echo "O"; // filling rest places with "O"
}
echo"\n"; // jumping to new Row;
}
Hope you can understand it easily.

Related

Getting position from 2 dimensional array instead of value PHP

Currently working on a project like bejuweld (candy crush type)
I've got a 2 dimensional table generated which contains 0 matches of 3 in a row. currently i want to click any of the numbers inside the table. I've got it clickable but I don't know how to make it an individual position.
When you click on the 4th row and 5th column I want it to output an int which says 45.
This is the part where it is about:
// Create the playing field
echo"<table border='2px;'>";
for($y = 0; $y < $th; $y++){
echo"<tr>";
for($x = 0; $x < $tw; $x++){
echo "<td><a href='#'>" .$aww_ar[$x][$y]."</a></td>";
}
echo"</tr>";
}
echo"</table>";
Which looks like this->
Inside the TD i made a href, I want the href to output the current position it's in.
I've Tried to do this ->
But it outputs the current value of position.
// Create the playing field
echo"<table border='2px;'>";
for($y = 0; $y < $th; $y++){
echo"<tr>";
for($x = 0; $x < $tw; $x++){
echo "<td><a href='".$aww_ar[$x][$y]."'>" .$aww_ar[$x][$y]."</a></td>"; // <---
}
echo"</tr>";
}
echo"</table>";
Here's all of the code ->
<?php
error_reporting('0');
echo("<link rel='stylesheet' type='text/css' href='./stylesheet.css'>");
$aww_ar = array();
// SELECT PLAYING FIELD VALUES
$th = 10;
$tw = 10;
// Amount of numbers
$amount = 5;
// Create array
for($y = 0; $y < $th; $y++){
for($x = 0; $x < $tw; $x++){
$randomise = rand(1,$amount);
$aww_ar[$x][$y] = $randomise ;
}
}
$numb = 0;
$match = true;
$counting = 0;
while ($match){
// Deletes matches and replaces with the value above
foreach ($aww_ar as $key=>&$value) {
foreach ($aww_ar[0] as $k=>$v) {
if($aww_ar[$key][$k] == 'X'){
$aww_ar[$key][$k] = $aww_ar[$key ][$k - 1];
}
}
}
// Fills in empty space
foreach ($aww_ar as $key=>&$value) {
foreach ($aww_ar[0] as $k=>$v) {
if($aww_ar[$key][$k] == ""){
$aww_ar[$key][$k] = rand(1,$amount);
}
}
}
// Detects matches and marks them with X
foreach ($aww_ar as $key=>&$value) {
foreach ($aww_ar[0] as $k=>$v) {
if ($k > 0 && $k < $tw &&$aww_ar[$key][$k] == $aww_ar[$key][$k - 1] && $aww_ar[$key][$k] == $aww_ar[$key][$k + 1]) {
$aww_ar[$key][$k] = 'X';
$aww_ar[$key][$k + 1] = 'X';
$aww_ar[$key][$k - 1] = 'X';
}
if ($key > 0 && $key < $th && $aww_ar[$key][$k] == $aww_ar[$key - 1][$k] && $aww_ar[$key][$k] == $aww_ar[$key + 1][$k]) {
$aww_ar[$key][$k] = 'X';
$aww_ar[$key + 1][$k] = 'X';
$aww_ar[$key - 1][$k] = 'X';
}
}
}
// Checks if X is in playing field, Else it will call it a day
for ($i=0; $i < $th; $i++) {
if(!in_array('X',$aww_ar[$i])){
$numb++;
if($numb == $th){
echo "The playing field has developed: ".$counting." Times";
$match = false;
}else {
$counting++;
}
}
}
if($numb != 0){
$numb = 0;
}
}
// Create the playing field
echo"<table border='2px;'>";
for($y = 0; $y < $th; $y++){
echo"<tr>";
for($x = 0; $x < $tw; $x++){
echo "<td><a href='#'>" .$aww_ar[$x][$y]."</a></td>";
}
echo"</tr>";
}
echo"</table>";

How to get the decimal point on the whole number [duplicate]

This question already has answers here:
How to get whole and decimal part of a number?
(20 answers)
Closed 1 year ago.
I want to get the breakdown of the charges but im stock on the decimal point, it should be the output i fill.out below. please see and check my code if there is lacking. it really could help me much. Ill show the output of my code.
$charge = 1600.50;
$base = 750;
$difference = $charge - $base;
$installment = $charge / 750;
$remainder = ($charge % 750);
$counter = 1;
if (is_float($charge)) {
if($remainder == 0) {
$count = 0;
for ($i=1; $i <= round($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
} else {
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
}
} else {
if($remainder == 0){
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
} else {
if($difference < 0){
echo $remainder."\n";
} else {
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
}
}
}
This is the output of my code.
750
750
100
0.50
But the correct output would be this.
750
750
100.50
I hope there is anyone could help me to solve this problem. it took weeks but im not able to solve this.
I don't know if i faced your problem correctly, but according to your desired output the following will do the trick.
else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
// echo $remainder."\n";
echo $remainder + $charge-floor($charge); // get fractial part of your $charge and add it to your remainder
}
$counter = $counter + 1;
}
/* unnecessary if-statement, is already checked by surrounding if-statement
if(is_float($charge)){
*/
// echo 'TRUE';
// $exploadedAmount = explode('.', $charge);
// echo "0.".$exploadedAmount[1];
}
}
I really don't understand what are you trying to do but isn't it better to write the code in partly functions?
anyways by editing this part of your code I manage to output the result you want:
$charge = 1600.50;
$base = 750;
$difference = $charge - $base;
$installment = $charge / 750;
$remainder = ($charge % 750);
$counter = 1;
if (is_float($charge)) {
// echo "TRUE";
if($remainder == 0){
$count = 0;
for ($i=1; $i <= round($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
}
else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}
$counter = $counter + 1;
}
if(is_float($charge)){
// if you want to concatinate the number to previuse one you must do it here
// the extra 0.5 is echo here
$exploadedAmount = explode('.', $charge);
echo "$remainder.".$exploadedAmount[1];
}
}
}
else{
if($remainder == 0){
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
}
else{
if($difference < 0){
echo $remainder."\n";
}else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
}
}
}
The output will be :
750 750 100.5
echo ".".$exploadedAmount[1]."0" ;
At line number 63 , if u change that number and 0 adding at the beginning and end means your can get that accurate output and also it is applicable for other phone numbers also.

How to fix this PHP loop?

this is what i want.
123456
23456
3456
456
56
6
Hi, i have trouble with this loop.
<?php
for ($x = 7; $x >= 1; $x--) {
for ($y = 7; $y > $x; $y--) {
echo " &nbsp";
}
$s = 7;
while ($s < $x) {
$f++;
$s--;
}
for ($f=1; $f < 7; $f++) {
echo "$f";
}
echo "<br>";
}
?>
this is what i got. I want to get the $f work but it is ignoring it.
You can make it simpler than you did.
for($x = 1; $x <= 6; $x++) {
for($y = 1; $y <=6; $y++){
if($x > $y)
echo "&nbsp ";
else
echo $y;
}
echo "<br>";
}
With x you control the lines and with y the columns. If the lines is greater than the column you print the spaces, and if not, the number.

how to print this pattern in php?

I run this program, but I need the output within table. So would you please solve this?
<?php
$s="*";
for($b=1; $b<=5; $b++) {
for($c=5; $c>=$b-1; $c--) {
if($c>=$b) {
echo $s;
}
else if($b != 1) {
echo " ";
}
}
for($d=5; $d>=$b; $d--) {
echo $s;
}
echo "<br/>";
}
?>
If you're fine regardless of how the code was written (only results matter), you can use this:
print('<table>');
for ($i = 0; $i < 5; $i++)
{
print('<tr>');
for ($j = 1; $j <= 5; $j++)
{
print('<td>');
(5 - $i >= $j) ? print('*') : '';
print('</td>');
}
for ($j2 = 1; $j2 <= 5; $j2++)
{
print('<td>');
(1 + $i <= $j2) ? print('*') : '';
print('</td>');
}
print('</tr>');
}
print('</table>');
What I did there is I sliced table in half vertically and used 2 for loops to fill left and right halfs. You will get something like this:

Not showing an echo message

I am unsure why I am not getting an echo if it is a prime number
$numPrime = 2;
function Number($numPrime)
{
for($numPrime = 0; $numPrime < 100; $numPrime++)
{
if($numPrime == TRUE)
{
echo("TRUE");
}else{
echo("FALSE");
}
}
}
You want something like this
for($i = 2; $i < $num; $i++) {
if($num % $i == 0) {
echo "false";
}
}
echo "true";

Categories