My next loop doesn't start - php

I try to make a function that creates some triangles.
<pre>
<?php
$angka = isset($_POST['angka']) ? $_POST['angka'] : "0";
if ($angka)
{
$segitiga = "";
$max = $angka + $angka - 1;
$ctr = 0;
for ($i=1; $i<=$angka; $i++){
echo $i;
for ($j=1;$j<=$max;$j++){
if ($i = $j){
if($j<=$angka){
$ctr++;
}elseif($j>$angka){
$ctr--;
}
echo " ". $ctr * $ctr;
}else{
echo " ";
}
}
echo ""."<br>";
}
}
?>
</pre>
The line of for ($i=1; $i<=$angka; $i++) is stop. It just run it once.
The next $i is not running. There is no next $i.
Actually, I want to make it as a function it, but this still doesn't work.

It looks like this may be the culprit
if ($i = $j){
It should be
if ($i == $j){
Because you're setting $i to $j which if $angka is 1, it will exit the for loop since it's $i <=$angka

Here is the fixed code:
<?php
$angka = isset($_POST['angka']) ? $_POST['angka'] : "0";
if ($angka)
{
$segitiga = "";
$max = $angka + $angka - 1;
$ctr = 0;
for ($i=1; $i<=$angka; $i++){
echo $i;
///*
for ($j=1;$j<=$max;$j++){
if ($i == $j){ //NOTICE how this needs to be `==` and not `=`
if($j<=$angka){
$ctr++;
}elseif($j>$angka){
$ctr--;
}
//$segitiga = $segitiga . ($ctr * $ctr);
echo " ". $ctr * $ctr;
}else{
//$segitiga = $segitiga . " ";
echo " ";
}
}
//*/
//$segitiga = $segitiga . "\n";
echo ""."<br>";
}
//echo $segitiga;
}
?>

for ($i=1; $i<=$angka; $i++){
your $i start at 1 and stops where it is equal to $angka
where $angka could be 0 because of
$angka = isset($_POST['angka']) ? $_POST['angka'] : "0";
so that loop may never execute , not even once
if you want to test what I'm saying, I'm executing your code with $angka set manually and it runs normally
here is the output for the following code
$angka = "3";
echo "<pre>";
if ($angka)
{
$segitiga = "";
$max = $angka + $angka - 1;
$ctr = 0;
for ($i=1; $i<=$angka; $i++){
echo $i;
echo 'the i loop here'.PHP_EOL;
for ($j=1;$j<=$max;$j++){
echo 'the j loop here'.PHP_EOL;
if ($i == $j){
if($j<=$angka){
$ctr++;
}elseif($j>$angka){
$ctr--;
}
echo " ". $ctr * $ctr;
}else{
echo " ";
}
}
echo ""."<br>";
}
}

Related

Display the pattern in which the first and a last number of each row will be 1

I am trying to use for loops to make a shape that looks like the diamond shape .
<?php
for($i=0;$i<=9;$i++){
for ($d=10-$i; $d > 0; $d--) {
echo " ";
}
for($j=1;$j<=$i;$j++){
echo " ".$i." ";
}
echo "<br>";
}
for($i=8;$i>=1;$i--){
for ($d=0; $d <= 9-$i; $d++) {
echo " ";
}
for($j=$i;$j>=1;$j--){
echo " ".$i." ";
}
echo "<br>";
}
?>
What it looks like :
What I desire :
I need a help in this.
Thank you
I'll post this here because I had fun making this. Felt like learning the basics again.
<?php
$max_size = 9;
function createSpaces($n){
$spaces = '';
for($i = 0; $i < $n; $i++){
$spaces .= ' '; //change to ' ' for HTML
}
return $spaces;
}
function createRows($i, $max_size){
$row = '';
$row .= createSpaces($max_size - $i);
for($j = 1; $j <= ($i-1); $j++){
$row .= $j;
}
for($k = $i; $k >= 1; $k--){
$row .= $k;
}
$row .= createSpaces($max_size - $i);
echo $row . PHP_EOL; //change PHP_EOL to '<br>' for HTML
}
for($i = 1; $i <= $max_size; $i++)
{
createRows($i, $max_size);
}
for($i = ($max_size - 1); $i >= 1; $i--)
{
createRows($i, $max_size);
}
Javascript version:
var max_size = 9;
function createSpaces(x) {
var spaces = '';
for (var i = 0; i < x; i++) {
spaces += ' ';
}
return spaces;
}
function createRow(i, max_size) {
var row = '';
row += createSpaces(max_size - i);
for (var j = 1; j <= (i - 1); j++) {
row += j;
}
for (var k = i; k >= 1; k--) {
row += k;
}
row += createSpaces(max_size - i);
document.write(row + '<br>');
}
for (var i = 1; i <= max_size; i++) {
createRow(i, max_size)
}
for (var i = (max_size - 1); i >= 1; i--) {
createRow(i, max_size)
}
<style>
body {
font-family: monospace;
}
</style>
Here is a similar version, where you only need to call the function once.
function diamond($max){
$result = '<p style="text-align:center">';
$last = 0;
for($i=1;$i<=$max;$i++){
for($x=1;$x<=$i;$x++){
$last=$x;
$result .= $last;
}
for($y=$last-1;$y>=1;$y--){
$result .= $y;
}
$result .= "<br />";
}
for($i=$max-1;$i>=1;$i--){
for($x=1;$x<=$i;$x++){
$last=$x;
$result .= $last;
}
for($y=$last-1;$y>=1;$y--){
$result .= $y;
}
$result .= "<br />";
}
$result .= "</p>";
return $result;
}
echo diamond(9);
echo diamond(7);
echo diamond(10);

How can I output this with a simple PHP for loop?

How can I output these with a simple 'for' loop?
####*
###**
##***
#****
*****
*****
#****
##***
###**
####*
hashtag = spaces
I've tried to changing the * and the 'br' within this one:
for($i = 0; $i < 5; $i++){
for($b = 0; $b <= $i; $b++) {
echo '*';
}
echo "</br>";
}
But than it will just output them under each other >.>
for($i=1;$i<6;$i++){
for($k=0;$k<5-$i;$k++){
echo " ";
}
for($j=0;$j<$i;$j++){
echo "*";
}
echo "<br>";
}
echo "<br>";
for($i=0;$i<6;$i++){
for($j=0;$j<$i;$j++){
echo " ";
}
for($k=0;$k<5-$i;$k++){
echo "*";
}
echo "<br>";
}
My Solution. Next time you should do your homework by yourself ;-)

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

I Want to print Star pattern

I want to print star pattern like :
below is my code. but it doesn't work for me. How can i fix this problem.
for($i=1; $i<=5; $i++){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
for($i=4; $i>=1; $i--){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
?>
You should put all text that should keep form and not whitespace trimmed in between a <pre/> tag
<pre><?php
for($i=1; $i<=5; $i++){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
for($i=4; $i>=1; $i--){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
} ?></pre>
Note: if your target is console and PHP-CLI, don't put the <pre/> tag, omit the end of PHP tag (?>) and replace all <br/> tags with \n.
Thanks to all of you guys. I add <pre> tag before code and also change <br> tag with \n and now it is work. Thanks again. Have a nice day guys..
Try this...
function printPattern($n)
{
$k = 0;
// Print upper triangle
for ($i = 1; $i <= $n; $i++)
{
// Print spaces
for ($j = 1; $j <= $n - $i; $j++)
{
echo " ";
}
// Print #
while ($k != (2 * $i - 1))
{
if ($k == 0 or $k == 2 * $i - 2)
echo "*";
else
echo " ";
$k++;
}
$k = 0;
// move to next row
echo "\n";
}
$n--;
// Print lower triangle
for ($i = $n; $i >= 1; $i--)
{
// Print spaces
for ($j = 0; $j <= $n - $i; $j++)
{
echo " ";
}
// Print #
$k = 0;
while ($k != (2 * $i - 1))
{
if ($k == 0 or $k == 2 * $i - 2)
echo "*";
else
echo " ";
$k++;
}
echo "\n";
}
}
// Driver Code
$n = 6;
printPattern($n);
// This Code is contributed by mits
?>

How to do nested loop in descending order?

What I want to get with for loop. Something will be like this.
*
**
***
****
*****
****
***
**
*
This thing I want to create right now I have this code.
<?php
$i = 1;
$end = 5;
$star = "*";
for($i; $i <= $end; $i++){
for($b=1; $b <= $i; $b++){
echo $star;
}
if($i == $end){
for($c=1; $c <= $end; $c++ ){
for($d=i; $d >= 2; $d--){
echo $star;
}
echo "<br>";
}
}
};
?>
And it's working fine with
*
**
***
****
*****
But then I need opposite loop first 4 then 3 then 2 then 1...
Where am I going wrong with this?
Just for fun. Try this:
$stars = 1;
for ($i = 0; $i < 9; $i++) {
for ($s = 1; $s <= $stars; $s++) {
echo "*";
}
echo "\n";
$stars += ($i<4)?1:-1;
}
And, for even more fun, one with just one for loop:
$stars = 0;
$starctr = 0;
for ($i = 0; $i < 25; $i++) {
echo "*";
if ($stars == $starctr) {
echo "\n";
$stars += ($i<14)?1:-1;
$starctr = 0;
} else {
$starctr++;
}
}
Can using nested for loop. Example:
$n = 5;
for($i = 1; $i <= $n; $i++){
for($j = 1; $j <= $i; $j++){
echo '*';
}
echo '<br />';
}
for($i = $n-1; $i >= 1; $i--){
for($j = $i; $j >= 1; $j--){
echo '*';
}
echo '<br />';
}
Another technique using array_fill(), array_map(), array_reverse()
$n = 5; $arr = array();
for($i = 1; $i <= $n; $i++){
$arr[] = array_fill(0, $i, '*');
}
array_map(function($v){echo join('', $v) . '</br>';},$arr);
unset($arr[count($arr) - 1]); //remove last index value
array_map(function($v){echo join('', $v) . '</br>';},array_reverse($arr));
<?php
for($i=0;$i<=6;$i++){
for($k=6;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
for($i=5;$i>=1;$i--){
for($k=6;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
?>
This can be achieved with only 2 for loops.
$offset = 1;
for ($i = 1; $i > 0; $i += $offset)
{
for($j = 1; $j <= $i; $j++){
echo '*';
}
echo '<br />';
if ($i === 5)
$offset = -1;
}
<?php
$i = 1;
$end = 5;
$star = "*";
for($i; $i <= $end; $i++){
for($b=1; $b <= $i; $b++){
echo $star;
}
};
for($c=$send; $c>=2; $c-- ){
for($d=$end; $d >= 2; $d--){
echo $star;
}
echo "<br>";
};
?>
Using a single for loop:
$end = 5;
$out = array();
for ($i=0;$i<$end;++$i)
{
$out[] = str_repeat('*', $i+1);
}
echo implode(PHP_EOL, $out).PHP_EOL;
array_pop($out);
$out = array_reverse($out);//remove last ****** <-- only one line with 5 stars
echo implode(PHP_EOL, $out).PHP_EOL;
Replace PHP_EOL with <br> if you want to, but this is the least loopy way to write this code I can think of
live demo
Here's a recursive attempt:
function ladder($n, $counter=1, &$acc=array())
{
if ($counter == ($n*2)) {
return implode($acc);
}
if ($counter <= $n) {
$acc[$counter] = str_repeat('*', $counter) . "\n";
$counter++;
}
if ($counter > $n) {
$diff = (int) $n-$counter;
$acc[$counter] = str_repeat('*', $n+$diff) . "\n";
$counter++;
}
return ladder($n, $counter, $acc);
}
print_r(ladder(5));

Categories