Welcome, Guest! Log In | Create Account

Blog View

From phpulse

Jump to: navigation, search

BLOG VIEW

<<Back | Next>>

The Blog view is a rather simple app. Now as we were talking about CRUD before, lets break apart the CRUD functionality of this app into our model and view subclasses :

  1. read the data from the database (TYPE MODEL CLASS)
  2. create,edit and delete data (AREA MODEL CLASS)
  3. render data (TEMPLATE)

Notice that I stated that we are going to put the function for reading the data in the TYPE MODEL class and not the AREA MODEL class. As you remember, GETTERS go into the TYPE MODEL class while SETTERS go into the AREA MODEL class. So lets first start off with the TYPE MODEL class then. We will need to define the table structure for the blog. Let's think about this... each entry will need:

  1. a primary key (good for indexing and lookups)
  2. a user id (as it needs to be associated with someone logged in)
  3. title
  4. text field
  5. a timestamp
  6. status (for setting it to pending or active)

So with those values, we can easily build the table:

CREATE TABLE `phpulse_blog` (
  `blog_id` int(11) NOT NULL auto_increment,
  `usr_id` int(11) NOT NULL,
  `title` varchar(50) NOT NULL,
  `entry` text NOT NULL,
  `creation_date` bigint(14) NOT NULL,
  `status` int(3) NOT NULL default '0',
  PRIMARY KEY  (`blog_id`),
  KEY `usr_id` (`usr_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

After we create our table, we will then want to add this to our 'phpulse' database.

Now we want to add the GETTER function to our TYPE MODEL class. So before we jump in, we need to ask two things:

  1. what variables need to be SENT
  2. what variables need to be RETURNED

For variables being SENT, since we defined in the table that each blog entry is associated with a user(usr_id), the getter functions should also be sent the usr_id. Let's also make it so we can pick one blog entry at a time by adding an OPTIONAL blog_id; this way we can choose to pick all blog entrys or one at a time.

For variables RETURNED, we need the blog_id, entry and creation date.

So lets take a look at how we would build that function:

protected function getBlogEntry($usr,$id=NULL){
    $entrys = array();
    $sql = "
        select
            B.blog_id,
            B.title,
            B.entry,
            B.creation_date,
            UI.usr_fname as fname,
            UI.usr_lname as lname
        from phpulse_blog as B left join usr_info as UI on B.usr_id=UI.usr_id
    ";
    $sql .= ($id) ? " where usr_id=".$usr." and blog_id='".$id."'" : " where usr_id=".$usr;
    $this->DB->query($sql);
    while($row=$this->DB->getRow()){
        $entrys[$row['blog_id']] = $row;
    }

    return $entrys;
}

Now that we have added the function to the TYPE MODEL, we need to be able to access it via the CONTROLLER. So let's create the CONTROLLER for our view TEMPLATE:

class VIEW_BLOG extends CONTROLLER{

    public function VIEW_BLOG($PULSE){
        $this->init($PULSE,"template_default.tpl");
    }

    protected function isAction(){
        // placeholder
    }

    protected function setProperties(){
        $this->TPL->assign('PAGE_BODY',"view_blog.tpl");
        if($_SESSION[DOC_TYPE]['usr']){
            $this->TPL->assign('entries',$this->getter("getBlogEntry",array($_SESSION[DOC_TYPE]['usr']['id'])));
        }
    }
}

Above in the CONTROLLER setProperties function, we first set the template to be used for the PAGE_BODY (which is the center section of the webpage). Then we use a callback method to call the 'getBlogEntry' function, return all entries associated with the logged in user, and assign them to a template variable which can be used by the template.

Finally, lets take a look at how we would implement this in the view TEMPLATE. As you know, the TEMPLATE makes use of the Smarty engine so if are are unused to some of the markup we are using, please familiarize yourself with the <a href=http://www.smarty.net/>Smarty Templating Engine</a>. The first thing we are going to do is load the 'entries' and loop through one at a time, rendering each blog entry on the page.

 {if $entries}
 {foreach from=$entries key=key item=val}
 <table width=100% cellspacing=20 cellpadding=0 border=0>
 <tr>
 <td width=75% height=250 valign=top>
 <div class="mid2_box">
 <p class=title>{$val.title|upper} by {$val.fname} {$val.lname}</p>
 <p>{$val.entry}</p>
 </div>
 </td>
 </tr>
 </table>
 {/foreach}
 {/if}

And there you have it! But how do we add entries? Well lets cover than in the next lesson...


<<Back | Next>>