Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1

    Default PHP Newbie: patabang ko to simplify a simple code :)


    ga self study ko ug PHP right now. Total newbie ko about programming.
    I would like to ask the experts for suggestions in how to make my code simpler.


    The Challenge:
    Write a program, in any free language, that prints to the terminal the lyrics to 99 Bottles of Beer | The lyrics to the song 99 Bottles of Beer

    Here's my code:

    Code:
    <?php    
        $bottles=99;
        $oneless=98;
    
    
        do {
            
            echo "$bottles bottles of beer on the wall, $bottles bottles of beer. <br />
                Take one down and pass it around, $oneless bottles of beer on the wall.<br /><br />";
            $bottles--;
            $oneless--;
            } while ($bottles>2);
        
        echo "$bottles bottles of beer on the wall, $bottles bottles of beer. <br />
                Take one down and pass it around,  $oneless bottle of beer on the wall.<br /><br />";
                $bottles--;
        echo "$bottles bottle of beer on the wall, $bottles bottle of beer. <br />
                Take one down and pass it around, no more bottles of beer on the wall.<br /><br />";
        echo "No more bottles of beer on the wall, no more bottles of beer. <br />
                Go to the store and buy some more, 99 bottles of beer on the wall.";    
    ?>


    Do-while lang sa ako gigamit but naa moy masuggest nga better approach to make it simpler?
    Murag bati kaayo tan-awon akong 3 echo statements sa last part of the code.

  2. #2

    Default Re: PHP Newbie: patabang ko to simplify a simple code :)

    e recurse bai.. para choi.. or mas lisud e obfuscate...
    Last edited by fixyourself; 12-27-2011 at 05:54 PM.

  3. #3

    Default Re: PHP Newbie: patabang ko to simplify a simple code :)

    Quote Originally Posted by fixyourself View Post
    e recurse bai.. para choi.. or mas lisud e obfuscate...
    medyo advance pa for me. Ill get back to it if makaya na hehe.
    Thanks.

  4. #4

    Default Re: PHP Newbie: patabang ko to simplify a simple code :)

    based on Time Complexity Ok ramn.. Basta as much as possible dili jud nested looping...

  5. #5
    Elite Member
    Join Date
    Jun 2010
    Gender
    Male
    Posts
    1,018

    Default Re: PHP Newbie: patabang ko to simplify a simple code :)

    akong masuggest ra kay wala nay do....while na lang diretso
    unya some if statements might allow you to remove your final echoes

    count = 99;

    while(count >= 0){
    if(count == 0){
    } else if(count == 1){
    } else if(count == 2){
    } else{
    }
    count--;
    }

    Final notes
    Determine the common strings.
    Take advantage of the fact that variables in PHP are mixed.
    Some will prefer case or switch.
    Wala ni nako gitest and didn't care to check syntax
    Last edited by Klave; 01-06-2012 at 10:30 PM.

  6. #6

    Default Re: PHP Newbie: patabang ko to simplify a simple code :)

    Quote Originally Posted by Klave View Post
    akong masuggest ra kay wala nay do....while na lang diretso
    unya some if statements might allow you to remove your final echoes

    count = 99;

    while(count >= 0){
    if(count == 0){
    } else if(count == 1){
    } else if(count == 2){
    } else{
    }
    count--;
    }

    Final notes
    Determine the common strings.
    Take advantage of the fact that variables in PHP are mixed.
    Some will prefer case or switch.
    Wala ni nako gitest and didn't care to check syntax
    Thanks, daghan ga recommend nako ug for loops. ako nya ning balikan ug post with a for loop solution.

  7. #7

    Default Re: PHP Newbie: patabang ko to simplify a simple code :)

    Another question:

    naa koy code nakit-an nga drop down for time selection.

    problem: create a drop down form where students will pick the time they sleep.
    I googled a working sample code, ang problema lang kay di jud nako mapawork and end time ug 12 midnight. 11:59pm ra ako mabutang or walay output sa browser.

    PHP Code:
    <?php
                $start 
    strtotime("8:00pm");
                
    $end strtotime("11:59pm");

                echo 
    '<select name="sleep">';
                    for (
    $i $start$i <= $end$i += 1800)
                        {
                        echo 
    '<option>' date('g:i a'$i);
                        }
                        echo 
    '</select>';
                
    ?>
    any suggestions?

  8. #8
    Elite Member
    Join Date
    Jun 2010
    Gender
    Male
    Posts
    1,018

    Default Re: PHP Newbie: patabang ko to simplify a simple code :)

    One problem i see in your code is not ending your option tag.

    My suggestion would be...use the alarm model for apple.
    Rather than listing full time values in the drop down.
    You can just make 3 dropdowns for hour, minute and ampm.
    Then you can use the three values to get the appropriate time. (Naa ra function sa PHP)

  9. #9

    Default Re: PHP Newbie: patabang ko to simplify a simple code :)

    Quote Originally Posted by Klave View Post
    One problem i see in your code is not ending your option tag.

    My suggestion would be...use the alarm model for apple.
    Rather than listing full time values in the drop down.
    You can just make 3 dropdowns for hour, minute and ampm.
    Then you can use the three values to get the appropriate time. (Naa ra function sa PHP)
    nice bitaw apple na cloc, but medyo complicated pana para nako hehehe.
    as for the option tag, considered bana as bad practice sa HTML?

    PHP man gud ako primary tutorial then learn HTML as needed. Wa jud ko background HTML before.

  10. #10
    Elite Member
    Join Date
    Jun 2010
    Gender
    Male
    Posts
    1,018

    Default Re: PHP Newbie: patabang ko to simplify a simple code :)

    What I meant was you opened an option (<option>) tag but did not close it (</option>).

    Sayon ra man...

    Without using <br> or new <div>

    Loop from 1 to 12 or 1 to 24 the first drop down.
    Loop from 00 to 59 the second drop down.
    If you used 1 to 12 make another drop down with am or pm as values.

    Then concatenate the selected values to make the date.

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. Replies: 43
    Last Post: 03-08-2015, 09:02 PM
  2. Im learning PHP, Help me hw to run the PHP!
    By kyme in forum Programming
    Replies: 23
    Last Post: 08-21-2012, 02:57 AM
  3. McDonald's, LOVE KO 'TO
    By eCpOnO in forum Food & Dining
    Replies: 292
    Last Post: 12-23-2011, 10:51 PM
  4. Replies: 333
    Last Post: 12-20-2007, 06:44 AM
  5. mga math geek dha patabang ko kadyot...
    By Dropd in forum Computer Hardware
    Replies: 5
    Last Post: 03-13-2007, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top