ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Problem setting up view file. How would you solve this issue? Be creative.

May 03, 2009 10:08pm

Subscribe [3]
  • #1 / May 03, 2009 10:08pm

    anonymous65551

    222 posts

    Ok, having problem separating out a view for this controller, because if I put the output into a view, the view would never be called (due to an infinite loop.)

    Any input on the best solution to this?  How would you solve this issue?  Be creative.  Have fun.

    <?php
    class Bottles_of_beer extends Controller {
    
      function Bottles_of_beer()
      {
        parent::Controller();
      }
    
      function index()
      {
        $this->sing();
      }
    
      function sing()
      {
        $j = 1;
        $store_inventory = 10000;
        while ($store_inventory > 0) 
        {
          for ($i = 99; $i = 0; $i -= $j) 
          {
            echo "$i bottles of beer on the wall.\n";
            echo "Take $j down, pass it around.\n";
          } 
          $store_inventory--;
          if ($store_inventory < 5000) { $store_inventory += 5001; }
          echo "Go to the store, buy some more.\n";
        }
      }
    }
    // End of file: bottles_of_beer.php
  • #2 / May 04, 2009 4:19am

    Dam1an

    2385 posts

    First of all, is this a hypothetical problem, or a real one?
    And secondly, does it not just time out?

  • #3 / May 04, 2009 5:10am

    yeah. at first I had a similar problem but then it kind of fixed itself. not sure what I did wrong or did to fix it but now it is working.

  • #4 / May 04, 2009 5:14am

    xwero

    4145 posts

    The view is not the problem here, when you reach 4999 you add 5001 and start all over again, causing the infinite loop. The code is added so what is the reason for doing this?

  • #5 / May 04, 2009 8:12am

    John_Betong

    690 posts

     
    Online modified version can be found here.

    The last parameter is the number of beers in the store.

    <?php
    include_once(APPPATH.'models/My_jokes.php');
    
    //===================================================
    class Bottles_of_beer extends My_jokes
    { 
    
      //===================================================
      function Bottles_of_beer()
        { 
        parent::My_jokes(); // controller();
      }//endfunc
    
    
      //===================================================
      function index($store_inventory=33)
      {
        $this->sing($store_inventory);
      }
    
      //===================================================
        function sing($store_inventory)
        {
            $data =    array();
            $z    = array();
            $j    = 1;
            
            $data['joke_title'] = 'Ten Green bottles';
    
            $z[]                = '<h3>Store has <b>' .$store_inventory .'</b> green bottles</h3><p>';<br />
            while ($store_inventory > 0)<br />
            {<br />
                // version 002 - <br />
                $start = $store_inventory; // 25, 15 5<br />
                if ($start > 10)<br />
                {<br />
                    $start = 10;<br />
                }<br />
                <br />
                $z[] = "<b>$start green bottles are standing on the wall</b><br />
    ";<br />
                for ($i = $start; $i >= 1; $i -= $j)<br />
                {<br />
                    $z[] = "$i bottles of beer on the wall.   ";<br />
                    $z[] = "Take $j down, pass it around.   <br />
    ";<br />
                }<br />
                <br />
                $store_inventory -= 10;<br />
                if ($store_inventory > 0)<br />
                {<br />
                    $z[] = "<b>Been to the store and got some more beer. (Store inventory now $store_inventory) </b></p>
    
    <p>";<br />
                }    <br />
            }<br />
            $z[] = "</p><h3>Been to the store and they have sold out.</h3><p>";<br />
            $z[] = '<br />
    Discussion:<br />
    <a href="http://ellislab.com/forums/viewthread/113340/">http://ellislab.com/forums/viewthread/113340/</a><br />
    ';<br />
            <br />
            $data['thelot'] = $z;<br />
            <br />
            $this->j_view($data);<br />
        }//endfunc</p>
    
    <p>    }//endclass<br />
    // End of file: bottles_of_beer.php

     
     
    edit: amended so that store can have any number of beers
     
     

  • #6 / May 05, 2009 10:07am

    anonymous65551

    222 posts

    @xwero: Yes, the infinite loop is on purpose.  If you knew the song, you’d know why. 

    @John_Betong:  You came the closest to understanding what I was trying to do, but I’m afraid no one actually knows this song.  Sad.  You missed the entire point as well.  You can’t change the starting amount.  It must always be 99.  The song is “99 bottles of beer” and is not “x green bottles standing on the wall.”  The bottles are not “standing” on the wall.  They are just “on” the wall.  No one knows how, exactly.  It doesn’t matter.  It’s just the song.

    No, the song is supposed to be an infinite loop.  The store never runs out, it is an endless supply.  You don’t stop singing until you pass out (or lose your voice from singing too much.)

    I can’t believe no one here has ever heard this song properly done.  Not enough people from the states old enough to know it I suppose.

  • #7 / May 06, 2009 2:28am

    John_Betong

    690 posts

    @xwero: Yes, the infinite loop is on purpose.  If you knew the song, you’d know why. 

    @John_Betong:  You came the closest to understanding what I was trying to do, but I’m afraid no one actually knows this song.  Sad.  You missed the entire point as well.  You can’t change the starting amount.  It must always be 99.  The song is “99 bottles of beer” and is not “x green bottles standing on the wall.”  The bottles are not “standing” on the wall.  They are just “on” the wall.  No one knows how, exactly.  It doesn’t matter.  It’s just the song.

    No, the song is supposed to be an infinite loop.  The store never runs out, it is an endless supply.  You don’t stop singing until you pass out (or lose your voice from singing too much.)

    I can’t believe no one here has ever heard this song properly done.  Not enough people from the states old enough to know it I suppose.

     
    I was born and bred a Brit and to quote Wiki

    99 Bottles of Beer
    From Wikipedia, the free encyclopedia

    “99 Bottles of Beer” is a traditional song in the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. In particular the song is frequently sung by children on long bus trips, such as class field trips, or on a Scout and/or Girl Guide outings.

    The song is derived from the English “Ten Green Bottles”.

     
    >>> No, the song is supposed to be an infinite loop. 
    >>> The store never runs out, it is an endless supply. 
    Our stores back in the UK are not as big as the ones in the ‘States 😊
     
     

  • #8 / May 06, 2009 4:51am

    Dam1an

    2385 posts

    Although the stores may run out of alcohol, they manage to restock faster then I can drink it, so its never a problem…

    And I though school kids sang ‘The wheels on the bus go round and round…’ on the bus… Encouraging schoolkids by singing about 99 bottles of beer?

  • #9 / May 06, 2009 8:02am

    n0xie

    1381 posts

  • #10 / May 06, 2009 8:06am

    xwero

    4145 posts

    <html>
    <head>
    <title>Never ending beer song</title>
    [removed][removed]
    [removed]
    $(function(){
        append = function(){
           if($('p').length > 100)
           {
                var j = Math.floor($('.line').length % 99);
                var i = 99 - j;
           }
           else
           {
                var i = 99 - $('.line').length;
                var j = 0
           }
    
    
           $('h1').after(''+i+' bottles of beer on the wall.
    Take 1 down, pass it around.');
           if((i == 0 && j == 0) || (i == 1 && j > 0))
           {
              $('h1').after('Go to the store, buy some more.');
           }
        }
    
        $('h1').click(function(){
             setInterval(function() { append() }, 1000)
        });
    
    });
    [removed]
    </head>
    <body>
    <h1>Never ending beer song (click here to start)</h1>
    
    </body>
    </html>

    Few minor flaws : the second run starts with 98 and from the second run on the “Go to the store, buy some more.” paragraph is added when the count is 1 instead of 0.

    I guess you could do the same in php with the sleep function but then you have to store all the previous lines.

    adit : the adding if jquery and the script tags have been removed but the code is intact

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases