i just added a small Portlet to the Blog demo. It display one of many Images & a link randomly at each Page Request.
At first, we create the DB-Table:
CREATE TABLE IF NOT EXISTS `Banner` (
`id` int(11) NOT NULL auto_increment,
`image` varchar(80) NOT NULL,
`link` varchar(80) NOT NULL,
`comment` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
after that, we run the model and the crud Actions in the CLI to generate our needed classes.
After that, we create the components/BannerPortlet.php :
<?php
class BannerPortlet extends Portlet {
public function renderContent() {
$bannerList = Banner::model()->findAll();
$bannerCount = Banner::model()->count();
$randomnumber = (rand()%$bannerCount);
echo '<a target="_blank" href="' . $bannerList[$randomnumber]->link . '">' . $bannerList[$randomnumber]->comment . '<br><img src="' . $bannerList[$randomnumber]->image . '"></img></a>';
}
}
?>
Now, we can use our Portlet like this:
<?php $this->widget('BannerPortlet', array('title'=>'Advertise'));?>