SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 28 of 28
  1. #26
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to hijack this thread, but I've toyed with the example code brainpipe gave and created this for my blog:
    PHP Code:
    <?php
     
    class EntryGateway {
         var 
    $db;
     
         function 
    EntryGateway (&$db) {
             
    $this->db = &$db;
         }
     
         function &
    getById ($id) {
             
    $sql 'SELECT id, categoryid, title, entry, UNIX_TIMESTAMP(date) FROM entries WHERE id = ' $id;
             
    $result $this->db->Execute($sql);
     
             
    $entry = &new Entry;
             
    $entry->setId($result->fields['id']);
             
    $entry->setCategoryId($result->fields['categoryid']);
             
    $entry->setTitle($result->fields['title']);
             
    $entry->setEntry($result->fields['entry']);
             
    $entry->setDate($result->fields['date']);
     
             return 
    $entry;
         }
     
         function &
    getAll () {
             
    $sql 'SELECT id, categoryid, title, entry, UNIX_TIMESTAMP(date) as date FROM entries';
             
    $result $this->db->Execute($sql);
     
             
    $entries = array();
             while (!
    $result->EOF) {
                 
    $entry = &new Entry;
                 
    $entry->setId($fields['id']);
                 
    $entry->setCategoryId($fields['categoryid']);
                 
    $entry->setTitle($fields['title']);
                 
    $entry->setEntry($fields['entry']);
                 
    $entry->setDate($fields['date']);
                 
    $entries[] = &$entry;
             }
     
             return 
    $entries;
         }
     
         function &
    getFromCategory ($categoryId) {
             
    $sql 'SELECT id, categoryid, title, entry, UNIX_TIMESTAMP(date) as date FROM entries WHERE categoryid = ' $categoryId;
             
    $result $this->db->Execute($sql);
     
             
    $entries = array();
             while (!
    $result->EOF) {
                 
    $entry = &new Entry;
                 
    $entry->setId($fields['id']);
                 
    $entry->setTitle($fields['title']);
                 
    $entry->setEntry($fields['entry']);
                 
    $entry->setDate($fields['date']);
                 
    $entries[] = &$entry;
             }
     
             return 
    $entries;
         }
     
     }
     
    ?>
    I was wondering if I am going about this the right way or not. Also, how would I implement a way of grabbing say 10 entries?
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  2. #27
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could write a method with a "limit" parameter and an optional start parameter.
    PHP Code:
    function &getAFew($limit$start=0) {} 
    Better yet, you could take all the SQL out of the finder, put it in a separate DAO class, and use the DAO's interface.
    PHP Code:
    class EntryDao
    {
        function 
    setLimit($limit) {}
        function 
    setOffset($offset) {}
        function 
    orderBy($column) {}
        function 
    addWhere($column$operator$value) {}
        function 
    find() {}

    I think there is a world market for maybe five computers.
    - Thomas Watson, chairman of IBM, 1943.

  3. #28
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote:

    PHP Code:
    $entries = array();
            while (!
    $result->EOF) {
                
    $entry = &new Entry;
                
    $entry->setId($fields['id']);
                
    $entry->setCategoryId($fields['categoryid']);
                
    $entry->setTitle($fields['title']);
                
    $entry->setEntry($fields['entry']);
                
    $entry->setDate($fields['date']);
                
    $entries[] = &$entry;
            } 
    A much better way would be to some how seperate the Entries array from this class yes ?

    Alas... I done this for you with a short example The Process class method in my last post processes the resultset and creates an Archive for you.

    Have a look, see

    http://www.sitepointforums.com/showthread.php?t=133691

    Hope this gives you some ideas anyway ? Ask away btw and for now, merry xmas everyone.


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
  •