SitePoint Sponsor

User Tag List

Page 5 of 5 FirstFirst 12345
Results 101 to 114 of 114
  1. #101
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    $i = 0;
    do {
    echo $i;
    $i++;
    } while ($i <= 9);
    ?>

  2. #102
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    Alot of people have alot of spare time and some great ideas!

    How would everyone feel about a regular 'Heath Robinson' thread with simple questions and complex, over worked answers?
    I'd be all for it. This thread has been fun and educational. I never thought of printing inside the loop definitions.
    MySQL v5.1.58
    PHP v5.3.6

  3. #103
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $db = new mysqli($host$username$passwd$dbname);
    $db->query('CREATE TABLE counts (number INT NOT NULL)');
    for (
    $i=0;$i<=9;$i++) {
      
    $db->query('INSERT INTO counts (number) VALUES (' $i ')');
    }
    $result $db->query('SELECT number FROM counts');
    while (
    $row $result->fetch_assoc()) {
      echo 
    $row['number'];
    }
    $result->close();
    $db->query('DROP TABLE counts');
    $db->close(); 
    Didn't bother testing this; somehow it didn't seem terribly important Oh, and it does technically have a for loop...

  4. #104
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    How would everyone feel about a regular 'Heath Robinson' thread with simple questions and complex, over worked answers?
    Yes! That would be a lot of fun!

    Note for all Americans: 'Heath Robinson' is what we call 'Rube Goldberg'. I had to look it up.

  5. #105
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    I seen some OOP of this, but one is to CPU intensive, and the other one... not that flexible.

    Here is a way you can loop whatever you want, in whatever way you want.
    The code could be re-factored a bit, but you should get the idea.

    If OOP is the way to go, I find that you get more flexibility this way:

    PHP Code:
    /**
     * Interface class for everything that needs to do stuff
     */
    interface iStuff {
        
    /**
         * This function must do the magic, and return whatever needs to be returned.
         */
        
    function execute();

    PHP Code:
    /**
     * Class to do stuff to strings
     */
    class cStringStuff implements iStuff {
        
    /**
         * My string to do stuff to
         *
         * @var String
         */
        
    private $_string;

        
    /**
         * Counter to see how many time we executed this function.
         *
         * @var Integer
         */
        
    private $_counter 0;

        
    /**
         * Constructor: Takes in the string to do stuff to.
         *
         * @param String $string
         */
        
    public function __construct($string) {
            
    $this->_string $string;

        }

        
    /**
         * Function will do stuff to our string, and then return the result.
         *
         * @return String
         */
        
    public function execute() {
            
    $this->_counter += 1;
            return 
    join '', array (
                
    "<br />\n",
                
    $this->_counter,
                
    ": ",
                
    join '*'preg_split "/\\b/"$this->_string ) )
            ) );
        }


    PHP Code:
    /**
     * Class will hold the for loop count
     */
    class cLoop implements iStuff {
        
    /**
         * The number of times to execute this
         *
         * @var Integer
         */
        
    private $_counts;

        
    /**
         * The object we want to loop
         *
         * @var iStuff
         */
        
    private $_object;

        
    /**
         * Constructor: Takes in the count to loop.
         *
         * @param unknown_type $object The object we must loop.
         * @param Integer $count How many times to loop this object.
         */
        
    public function __construct(iStuff $object$count 10) {
            
    $this->_counts $count;
            
    $this->_object $object;
        }

        
    /**
         * Decreases the count by one, and runs the object we must run.
         *
         * @return unknown Whatever the passed in object returns.
         */
        
    public function execute() {
            
    $this->_counts -= 1;
            return 
    $this->_object->execute ();
        }

        
    /**
         * Return the current count.
         *
         * @return Integer
         */
        
    public function getCount() {
            return 
    $this->_counts;
        }

    PHP Code:
    /**
     * Class will do something special on every loop count.
     */
    class cSpecialLoop extends cLoop implements iStuff {

        
    /**
         * Decreases the count by one, and runs the object we must run.
         *
         * @return unknown Whatever the passed in object returns.
         */
        
    public function execute() {
            echo 
    "<br />\nMy special loop ran!";
            return 
    parent::execute ();
        }


    PHP Code:
    /**
     * Controller class to loop all the loops
     *
     */
    class cFor implements iStuff {
        
    /**
         * The object we want to loop
         *
         * @var cLoop
         */
        
    private $_object;

        
    /**
         * Holds our output buffer
         *
         * @var String
         */
        
    private $_outputBuffer '';

        
    /**
         * Constructor: Takes in the Looper object to use, if it should automatically
         * execute it, and if it should automatically print out the result.
         *
         * @param cLoop $object Loop object to use.
         * @param Boolean $autorun If we should execute automatically.
         * @param Boolean $autoPrint If we should print out the result automatically.
         */
        
    public function __construct(cLoop $object$autorun false$autoPrint false) {
            
    $this->_object $object;
            if (
    $autorun) {
                
    $this->execute $autoPrint );
            }
        }

        
    /**
         * Executes the loop code.
         *
         * @param Boolean $autoPrint If we should automatically print out the buffer.
         */
        
    public function execute($autoPrint false) {
            if (
    $this->_object->getCount () > 0) {
                
    $this->_outputBuffer .= $this->_object->execute ();
                
    $this->execute $autoPrint );
            } else if (
    $autoPrint) {
                
    $this->printBuffer ();
            }
        }

        
    /**
         * Prints out the output buffer.
         */
        
    public function printBuffer() {
            echo 
    $this->_outputBuffer;
        }

    And you can simply use it like this:

    PHP Code:
    # Create a new for loop, run it and print it, of 5 loops executing the cStringStuff object.
    $myLooper = new cFor ( new cSpecialLoop ( new cStringStuff "Do something with this string" ), ), truetrue ); 
    Which will output:
    Code:
    My special loop ran!
    My special loop ran!
    My special loop ran!
    My special loop ran!
    My special loop ran!
    1: *Do* *something* *with* *this* *string*
    2: *Do* *something* *with* *this* *string*
    3: *Do* *something* *with* *this* *string*
    4: *Do* *something* *with* *this* *string*
    5: *Do* *something* *with* *this* *string*
    Or of course like:
    PHP Code:
    $string = new cStringStuff "Do something with this other string" );
    $loop = new cSpecialLoop($string10);
    $for = new cFor($loop);
    $for->printBuffer(); 
    Which will output:
    Code:
    My special loop ran!
    My special loop ran!
    My special loop ran!
    My special loop ran!
    My special loop ran!
    My special loop ran!
    My special loop ran!
    My special loop ran!
    My special loop ran!
    My special loop ran!
    1: *Do* *something* *with* *this* *other* *string*
    2: *Do* *something* *with* *this* *other* *string*
    3: *Do* *something* *with* *this* *other* *string*
    4: *Do* *something* *with* *this* *other* *string*
    5: *Do* *something* *with* *this* *other* *string*
    6: *Do* *something* *with* *this* *other* *string*
    7: *Do* *something* *with* *this* *other* *string*
    8: *Do* *something* *with* *this* *other* *string*
    9: *Do* *something* *with* *this* *other* *string*
    10: *Do* *something* *with* *this* *other* *string*
    Remember kids, OOP is good for you, sometimes...

  6. #106
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sounds like a good plan to me, a regular thread

  7. #107
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by awright418 View Post
    Yes! That would be a lot of fun!

    Note for all Americans: 'Heath Robinson' is what we call 'Rube Goldberg'. I had to look it up.
    LOL, yes sorry I did intend adding that but got distracted
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #108
    SitePoint Enthusiast reflash's Avatar
    Join Date
    Jul 2008
    Location
    Pocono Mtns.
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    Alot of people have alot of spare time and some great ideas!

    How would everyone feel about a regular 'Heath Robinson' thread with simple questions and complex, over worked answers?
    I'd like it - I enjoyed this thread immensely!

  9. #109
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for ($i = 0; $i < 1; $i++) {
    $temp = tmpfile();
    fwrite($temp, "0,1,2,3,4,5,6,7,8,9");
    fseek($temp, 0);
    fpassthru($temp)
    fclose($temp); // this removes the file
    }

    It does technically use a for loop......

  10. #110
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no one has used any semantic markup here ... tut tut ... how will the OP be able to SEO his page to be top listing to show his tutor?

    Code PHP:
    echo "<ol>";
    for($n=0;$n<9;$n++) {
    $isthispointless = "yes";
    while($isthispointless == "yes") {
    $rand = rand(0,100);
    if($rand == $n) {
    echo "<li>&nbsp;</li>";
    $isthispointless = "no";
    }
    }
    }
    echo "</ol>";

  11. #111
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
     
    class RequestController
    {
     
    public function execute()
    {
     
    $command = new NumberDisplayCommand();
    $command->execute();
     
    }
     
    }
     
    class NumberDisplayCommand
    {
     
    public function execute()
    {
     
    $view = new NumberDisplayView();
    $view->render(1, 2, 3, 4, 5, 6, 7, 8, 9);
     
    }
     
    }
     
    class NumberDisplayView
    {
     
    public function Render()
    {
     
    $args = func_get_args();
     
    foreach ($args as $arg)
    {
    echo '<div>' . $arg . '</div>';
    }
     
    }
     
    }
     
    $requestController = new RequestController();
    $requestController->execute();

  12. #112
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did anyone post this altorithm? (I didn't read all of the new posts)

    PHP Code:
    $i 1000000000;
    while (
    true) {
        
    $s = (string)$i;
        ++ 
    $i;
        for (
    $position 0$position 8; ++$position) {
            if ((int)(
    $s{$position}) != (int)($s{$position+1} + 1) {
                
    $s false;
                break;
            }
        }
        if (!
    is_string($s)) {
            continue;
        }
    }
    echo 
    implode(' 'array_reverse(str_split($s))); 

  13. #113
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Code:
      for(${             ""}="";
     ${    ''            }     <
     ''     +   0x54>>   3;print
     ''     +                  (
       ${""               }++));
    Salathe
    Software Developer and PHP Manual Author.

  14. #114
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have a good one buddy.

    Code PHP:
    $rand =rand(0,9999999999*34-43+55*123-574373748543-34234+32+1+1+1+1*55+122+12+43+12-11000101010);
    for($i=0; $i<=9-56+23+455+214+$rand+355-12443-233-122+44342+343334+$rand+$rand+$rand/$rand+466-5868-4123-444-1243435-3434234*$rand*4-33432355234; $i++ )
    {
    	echo $i;
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •