Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts

    Looping through a results set in OOP?

    Hello. in my OOP code I've passed a query into my constructor and held the results in a property called $this->results_set

    PHP Code:
    class myclass {

      private 
    $query;
      private 
    $results_set;

      public function 
    __construct($query) {
        
    $this->query $query;
        
    $result mysql_query($this->query);
        while(
    $rowmysql_fetch_array($result)) {
          
    $this->results_set[] = $row;
          
    //print_r($this->results_set);
        
    }
      }


    PHP Code:
    $a = new myclass ("Select name, age FROM table");
    //show each of the results in a foreach? 
    Now I want to loop (iterate) through this results set by implementing implements iterator

    Is this a sensible approach? If so how could it be achieved? Thank you

  • #2
    New Coder
    Join Date
    Dec 2011
    Posts
    84
    Thanks
    5
    Thanked 13 Times in 13 Posts
    This is what I do--->
    Suppose you have a Column in your database called post_date in one table and update in another table? Instead of writing a whole bunch of if statements, simply create a constructor class.
    PHP Code:
    <?php
    class BlogPosts extends BlogPost {
        
         public function  
    __construct($page) {
             foreach (
    $page as $key => $value)
             {            
                
    $pos strpos($key'date'); // Find the word date in array
                
    if ( $pos !== false $value date('F j, Y g:i A'strtotime$value )); // Format MySQL date to proper format
                
    $this->$key $value// Assign value to variable in object. 
             
    }
         }
    }
    *Note - the if statement seems a little weird, but what you are basically saying is if the statement is not false then perform the formatting of the date in order to make it readable for a human. It doesn't work the way you think it would....I know I tried.

    Then all you have to do is something like the following (different class):
    PHP Code:
     public function blogPostRecords()
        {
            
    $database parent::connect(); //Connects to the mysqli Database
             
            
    $query "SELECT id, blog_name, content, new_blog_date, update_date, username FROM pages ORDER by id DESC";
            
    $result $database->query($query);
            
            while (
    $page $result->fetch_array(MYSQLI_ASSOC)) {
                
    /* This makes it an array of an object, also note */
               /* if you just want an object then just omit the   */
               /* [] braces from $this->send_posts for example   */                   
               
    $this->send_posts[] = new BlogPosts($page);                      
            }
            
    /* free result set */
            
    $result->free();

             
    $databaseClose parent::close_connection();              
            
            return 
    $this->send_posts;
            
        } 
    I personally find this approach easier for you can reuse this code over and over again, without or with minimal changes to the code. However, everyone codes differently.

    Then for example this is one way of doing it.

    PHP Code:
     $blog_post =  new BlogRetrievalClass//  New instance;
     
    $blogs $blog_post->blogs(); // retrieve array of objects

     
    foreach ($blogs as $key => $blog) {
          echo 
    '<p>' $blog->username '</p>';
     } 
    With little modifications to the database class , you could then do this for a single object and forgo the foreach loop.
    Last edited by Strider64; 04-23-2013 at 02:34 PM.

  • Users who have thanked Strider64 for this post:

    Oatley (04-23-2013)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    class myclass implements IteratorAggregate {

      private 
    $query;
      private 
    $results_set;

      public function 
    __construct($query) {
        
    $this->query $query;
        
    $result mysql_query($this->query);
        while(
    $rowmysql_fetch_array($result)) {
          
    $this->results_set[] = $row;
          
    //print_r($this->results_set);
        
    }
      }
        public function 
    getIterator()
        {
            return new 
    ArrayIterator($this->results_set);
        }

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Oatley (04-23-2013)

  • #4
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts
    Thanks you guys that solves my problem. Just for my reference, when is a good real world example of where I could implement iterator?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You would implement Iterator on any custom collection which doesn't have an underlying storage that is either an iterator type or cannot be aggregated for an iterator. So if you use an array, a mysqli_result (as of 5.4), pdo, or any spl_iterator type, than you can simply poll the storage for its iterator.
    An example would be a custom linked list which only stores reference to next. Since these are not in a collection of their own as the linked list is its own collection, you'd implement the iterator to handle jumps between the objects.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Oatley (04-26-2013)

  • #6
    Regular Coder
    Join Date
    May 2012
    Location
    USA
    Posts
    102
    Thanks
    0
    Thanked 7 Times in 7 Posts
    I don't think that it is a proper approach, you should sort out other ways of doing it.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by annaharris View Post
    I don't think that it is a proper approach, you should sort out other ways of doing it.
    Do you have a better suggestion for converting an existing object into a Traversable type?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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