10gbPHPTeacher


 10 Green Bottles - PHP (with a little bit of html)

 

First asks how many bottles to sit on the wall and then proceeds.  Even includes bottle pictures but can be removed if it begins to take too long.  I tried with 1000 bottles and gave up!

 

<?php

if (isset($_POST['numbottles'])) {

  $numbottles = ((int)$_POST['numbottles']);

 

for ($i = $numbottles; $i >= 1; $i--){

    for ($j = 1; $j <= $i; $j++){

        echo  "<img src=\"green_bottle.png\" />";

        }

    echo "<BR>";

    if ($i == 1)

    {

        echo $i." green bottle sitting on the wall!<BR>";

        echo $i." green bottle sitting on the wall!<BR>";

        echo "And if one green bottle should accidentally fall<BR>";

        echo "There'll be no green bottles sitting on the wall!<BR>";

        echo "<BR>";

    } else

    {

        echo $i." green bottles sitting on the wall!<BR>";

        echo $i." green bottles sitting on the wall!<BR>";

        echo "And if one green bottle should accidentally fall<BR>";

        if ($i-1 == 1)

        {

            echo "There'll be ".($i-1)." green bottle sitting on the wall!<BR>";

        } else

        {

            echo "There'll be ".($i-1)." green bottles sitting on the wall!<BR>";

        }

        echo "<BR>";

    }

}

echo "<a href=\"10gb.php\">Reset the bottles on the wall and start again.</a>";

} else

{

?>

<form id="form1" name="form1" method="post" action="10gb.php">

  <p>How many bottles to you want to begin with on the wall?</p>

  <p>

    <input name="numbottles" type="int" id="numbottles" />

    <input type="submit" name="Submit" value="Submit" />

  </p>

</form>

<p>

<?php

}

?>

 

Download