Jump to content

for loop


mayman212

Recommended Posts

<table>
<tr>
<td align="center">
<?PHP

$b = 1;
for ($a=1; $a<=16; $a++)
{
        echo str_repeat($a, $b);
        $b++;
        echo "<br>";
}
$b = 17;
for ($a=1; $a<=26; $a++)
{
        echo str_repeat($a, $b);
        $b--;
        echo "<br>";
}
?>
</td>
</tr>
</table>

 

I am trying to make something like this:

                  1

                  23

                456

            7891011

          1213141516

            17181920

              212223

                2425

                  26

 

 

But i have managed what I've shown above. Anyone have any help on this?

Link to comment
https://forums.phpfreaks.com/topic/250284-for-loop/
Share on other sites

 

<table>
<tr>
<td align="center">
<?PHP

$b = 1;
for ($a=1; $a<=16; $a++)
{
        echo str_repeat($a, $b);
        $b++;
        echo "<br>";
}
$b = 17;
for ($a=17; $a<=26; $a++)
{
        echo str_repeat($a, $b);
        $b++;
        echo "<br>";
}
?>
</td>
</tr>
</table>

 

I am trying to make something like this:

                  1

                  23

                456

            7891011

          1213141516

            17181920

              212223

                2425

                  26

 

 

But i have managed what I've shown above. Anyone have any help on this?

(slightly updated)

Link to comment
https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284216
Share on other sites

<table>
<tr>
<td align="center">
<?PHP

$b = 1;
for ($a=1; $a<=1; $a++)
{
        echo $b++;
        echo "<br>";
}

$b = 2;
for ($a=1; $a<=2; $a++)
{
        echo $b++;
       
}

echo "<br>";
$b = 4;
for ($a=1; $a<=3; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 7;
for ($a=1; $a<=5; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 12;
for ($a=1; $a<=5; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 17;
for ($a=1; $a<=4; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 21;
for ($a=1; $a<=3; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 24;
for ($a=1; $a<=2; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 26;
for ($a=1; $a<=1; $a++)
{
        echo $b++;
       
}

?>
</td>
</tr>
</table>

 

Okay, I figured it out, well kind of, but I need to reduce the amount of line have. Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284221
Share on other sites

you can condense that into one for loop with if conditionals..

 

for($a=1; $a <= 26; $a++){
   if($a == 1)
      echo $a ."<br />".;
   if($a == 2 || $ == 3)
      echo $a;
   if($a == 4)
      echo $a ."<br />".;
}

 

won't get an award on its looks... but will use one for loop instead of 9

Link to comment
https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284226
Share on other sites

<table>
<tr>
<td align="center">
<?PHP

$b = 1;
for ($a=1; $a<=1; $a++)
{
        echo $b++;
        echo "<br>";
}

$b = 2;
for ($a=1; $a<=2; $a++)
{
        echo $b++;
       
}

echo "<br>";
$b = 4;
for ($a=1; $a<=3; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 7;
for ($a=1; $a<=5; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 12;
for ($a=1; $a<=5; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 17;
for ($a=1; $a<=4; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 21;
for ($a=1; $a<=3; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 24;
for ($a=1; $a<=2; $a++)
{
        echo $b++;
       
}
echo "<br>";
$b = 26;
for ($a=1; $a<=1; $a++)
{
        echo $b++;
       
}

?>
</td>
</tr>
</table>

This is still the code I am using, the one you gave earlier came up with the unexpected error.

Link to comment
https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284263
Share on other sites

Maybe a little primitive for the coding gurus, but it duplicates the example you gave.

<table>
<tr>
<td align="center">
<?PHP
$a=1; 
$b=1;
$d=16; 
$c=1;//line limit
while ($b<=$c && $a<=$d){
while ($b<=$c && $a<=$d){
	echo $a;
$a++;
$b++;
}
        echo "<br>";
IF ($c<3){
$c++;
}
ELSE{
$c=5;
} 
$b=1;
}
//Start Down  
$b=4;
$c=1;
$e=26;
while ($c<=$b && $a<=$e){
while ($c<=$b && $a<=$e){
	echo $a;
$a++;
$c++;
   }
        echo "<br>";
$b--;
$c=1; 
}
?>
</td>
</tr>
</table>

Link to comment
https://forums.phpfreaks.com/topic/250284-for-loop/#findComment-1284356
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.