Getting position from 2 dimensional array instead of value PHP - 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>";

Related

update 2 dimensional array PHP

I made a 2 dimensional array which checks if 3 of the same values are equal to each other.
My problem is that when the code detects 3 in a row it changes values, but it doesn't update to the original array ($aww_ar).
Is there a way to do this?
Code:
<?php
$aww_ar = array();
echo"<table>";
for($y = 0; $y < 6; $y++){
echo"<tr>";
for($x = 0; $x < 6; $x++){
$randomise = rand(1,4);
$aww_ar[$x][$y] = "<td>".$randomise."</td>";
echo $aww_ar[$x][$y];
}
echo"</tr>";
}
echo"</table>";
$counter = 0;
for ($h=0; $h < count($aww_ar); $h++) {
for ($u=0; $u < count($aww_ar[0]); $u++) {
if($aww_ar[$h][$u] == $aww_ar[$h][$u-1] && $aww_ar[$h][$u] == $aww_ar[$h][$u+1]){
$counter++;
$aww_ar[$h][$u] = 'X';
$aww_ar[$h][$u+1] = 'X';
$aww_ar[$h][$u-1] = 'X';
echo $aww_ar[$h][$u];
echo $aww_ar[$h][$u+1];
echo $aww_ar[$h][$u-1];
}
}
}
echo $counter;
It might be related to Passing By Reference
for ($h = 0; $h < count($aww_ar); $h++) {
for ($u = 0; $u < count($aww_ar[0]); $u++) {
if ($aww_ar[$h][$u] == $aww_ar[$h][$u - 1] && $aww_ar[$h][$u] == $aww_ar[$h][$u + 1]) {
$aww_ar[$h][$u] = 'X';
$aww_ar[$h][$u + 1] = &$aww_ar[$h][$u];
$aww_ar[$h][$u - 1] = &$aww_ar[$h][$u];
echo $aww_ar[$h][$u];
echo $aww_ar[$h][$u + 1];
echo $aww_ar[$h][$u - 1];
}
}
}
// Write this in simplified form use "foreach"
foreach ($aww_ar as $key=>&$value) {
foreach ($aww_ar[0] as $k=>$v) {
if ($k > 0 && $aww_ar[$key][$k] == $aww_ar[$key][$k - 1] && $aww_ar[$key][$k] == $aww_ar[$key][$k + 1]) {
$aww_ar[$key][$k] = 'X1';
$aww_ar[$key][$k + 1] = 'X1';
$aww_ar[$key][$k - 1] = 'X1';
echo $aww_ar[$key][$k];
echo $aww_ar[$key][$k + 1];
echo $aww_ar[$key][$k - 1];
}
}
}
Ive worked it out a bit with the help of Lopes Wang
<?php
$aww_ar = array();
// Create array
for($y = 0; $y < 9; $y++){
for($x = 0; $x < 9; $x++){
$randomise = rand(1,4);
$aww_ar[$x][$y] = "<td>".$randomise."</td>";
}
}
// Detects matches and marks them with X
foreach ($aww_ar as $key=>&$value) {
foreach ($aww_ar[0] as $k=>$v) {
if ($k > 0 && $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';
}
}
}
echo"<table border='2px;'>";
for($y = 0; $y < 9; $y++){
echo"<tr>";
for($x = 0; $x < 9; $x++){
echo "<td>" .$aww_ar[$x][$y]."</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.

Using loop to create a shape

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.

I'm new to PHP and I created a For loop and would like to know if anyone knows how to re-write it to a while loop?

This is the code I have. It currently works as is, However I'm experimenting with loops and want to see it can be done with a while loop and how it would be done. With this code I can take 2 input numbers and display them, then point out all odds, add all evens, and add all the squares of the odds.
define ("B","<br/>");
$firstNum = $_POST["firstNum"];
$secondNum = $_POST["secondNum"];
if ($firstNum < $secondNum)
{
$firstNum = true;
}
elseif ($firstNum >= $secondNum)
{
$firstNum = "You didn't listen, dumb dumb!".'<br/>GO BACK';
}
echo "First Number: ".$firstNum."<br/>"."Second Number: ".$secondNum;
echo B;
echo B;
$numbers = array();
$numbers = range($firstNum, $secondNum);
$length = count($numbers);
$odds = array();
$sumSqOdds = 0;
$sumEven = 0;
$j = 0;
for ($x = 0; $x < $length; $x++)
{
if (($numbers[$x] % 2) == 1)
{
$odds[$j] = $numbers[$x];
$sumSqOdds = $sumSqOdds + pow ($numbers[$x], 2);
$j++;
}
else
{
$sumEven = $sumEven + $numbers[$x];
}
}
$x = 0;
$y = 0;
printf("The odd numbers between your integers are: ");
for ($x = 0; $x < $j; $x++)
{
echo $odds[$x];
echo ' ';
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
echo B;
echo B;
printf("The sum of all even numbers between your integers is: ".$sumEven);
echo B;
echo B;
printf("The sum of the square of the odd numbers between your integers is: ".$sumSqOdds);
Here is my while loop but it seems to be infinite...
$numW = array ();
$numW = range ($firstNum, $secondNum);
$lengthW = count ($numW);
$oddsW = array ();
$sumSqOddsW = 0;
$sumEvenW = 0;
$j = 0;
$x = 0;
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++;
$j++;
}
else
{
$sumEvenW = $sumEvenW + $numW[$x];
}
}
$x = 0;
$y = 0;
printf ("The odd numbers between your integers are: ");
while ($x < $j)
{
$x++;
echo $oddsW[$x];
echo "nbsp;";
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
Equivalent loops:
for ($i = 0; $i < 10; $i++) {
echo $i;
}
$i = 0;
while ($i < 10) {
echo $i;
$i++;
}
For a loop to ever finish it has to change one of the two evaluating variables. So either $x, or $lengthW would have to change during iteration. You made an if statment, in the first case you define that X increases by 1, but in the else case you do not change any variable that then has an effect on either $x, or $lengthW
Nor is there any check that sees if the else state has been reached and to catch that by either changing $x or $lengthW in a later iteration.
As such there's an infinite loop as soon as you reach the else case.
The if statement uses the same $x value as the last iteration checking the same position of the $numW, as such nothing has changed since the last iteration and you'll hit the else again, and again, and so on.
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++; //$x is increased by one, and as such, the loop will progress.
// remove this $x++ if you place it outside the if else statement.
$j++;
}
else // reached when ($numW[$x] %2) != 1
{
$sumEvenW = $sumEvenW + $numW[$x];
// No changes to $x or $lengthW as such you'll hit the else again
// this could be solved by either adding $x++; here.
}
// or by adding $x++; here
// (if you do add it here, remove it in the if case above,
// or you risk increasing it by 2 every iteration
}

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

Categories