Xây dựng View lấy giá trị từ model truyền ra layout (views) Xây dựng Layout hiển thị các giá trị nhận được từ View (tmpl)

Một phần của tài liệu giáo trình joomla 1.5 Toàn tập (Trang 68 - 71)

- Xây dựng Layout hiển thị các giá trị nhận được từ View (tmpl) - Phân trang tại Front-End

a. Xây dng hàm ly d liu t Database (models)

Tạo tập tin categories.php trong thư mục /joomla/com_book/models đưa vào nội dung sau: sau:

class BookModelCategories extends JModel {

var $_data = null;

function __construct() { parent::__construct(); global $mainframe; } function getAllCategories() { if (empty($this->_data)) {

$query = "SELECT * FROM #__book_category WHERE published=1"; $this->_data = $this->_getList($query);

}

return $this->_data; }

}

b. Xây dng View ly giá tr t model truyn ra layout (views)

Tạo tập tin view.html.php trong thư mục /joomla/com_book/views/categories sửa hàm display() thành: display() thành:

function display($tpl = null) {

global $mainframe;

// Goi lop BookModelCategories $model = $this->getModel();

// Goi ham getAllCategories() trong lop BookModelCategories $lists = $model->getAllCategories();

//Gan và truyen gia trị ra ngoai layout

$this->assignRef('rows', $lists);

parent::display($tpl); }

Giảng viên: Phạm Vũ Khánh 3 Email: vukhanh2212@gmail.com

Tạo tập tin default.php trong thư mục /joomla/com_book/views/categories/tmpl sửa hàm display() thành: display() thành: <style> .book-category{ float: left; width: 50%; font-size: 14px; font-weight: bold;

text-align: center !important; line-height: 16px; padding: 5px 0px; } .div-clear{ clear: both; } </style> <?php

foreach ($this->rows as $key => $info){

$link = "index.php?option=com_book&view=category&id=" . $info->id;

echo '<div class="book-category"><a href="' . $link . '">' . $info->category . '</a></div>';

} ?>

<div class="div-clear"></div>

d. Phân trang ti Front-End

Tạo tập tin categories.php trong thư mục /joomla/com_book/models thay đổi một số nội dung: dung:

Thay đổi nội dung hàm __contruct() để lấy các giá trị phân trang và đưa vào biến toàn cục của Model cục của Model

class BookModelCategories extends JModel { var $_data = null; (adsbygoogle = window.adsbygoogle || []).push({});

function __construct() { parent::__construct(); global $mainframe;

$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit')); $this->setState('limit', $limit);

$limitstart = JRequest::getVar('limitstart', 0); $this->setState('limitstart', $limitstart);

}

Thay đổi nội dung hàm getAllCategories() để đưa giá trị $limit và $limitstart vào câu truy vấn hiển thị các category vấn hiển thị các category function getAllCategories() { if (empty($this->_data)) { $query = "SELECT * FROM #__book_category WHERE published=1 ";

Giảng viên: Phạm Vũ Khánh 4 Email: vukhanh2212@gmail.com $this->_data = $this->_getList($query, $this->getState('limitstart'), $this- >getState('limit'));

}

return $this->_data; }

Tạo hàm getTotal() đếm số record có trong database để thực hiện phân trang

class BookModelCategories extends JModel { var $_data = null;

var $_total = null;

function __construct() { //Code cũ } function getAllCategories() { //Code cũ } function getTotal() { $query = "SELECT * FROM #__book_category WHERE published=1 "; $this->_total = $this->_getListCount($query); return $this->_total; }

Tạo hàm phân trang getPagination()

class BookModelCategories extends JModel { var $_data = null;

var $_total = null;

var $_pagination = null;

function __construct() { //Code cũ } function getAllCategories() { //Code cũ } function getTotal() { //Code cũ } function getPagination() {

// Lets load the content if it doesn't already exist if (empty($this->_pagination)) {

jimport('joomla.html.pagination');

$this->_pagination = new JPagination($this->getTotal(), $this- >getState('limitstart'), $this->getState('limit'));

}

return $this->_pagination; }

Giảng viên: Phạm Vũ Khánh 5 Email: vukhanh2212@gmail.com

Gọi hàm phân trang trong Model vào View. Mở tập tin view.html.php trong thư mục

/joomla/com_book/views/categories sửa hàm display() thành:

<?php //Code cũ

class BookViewCategories extends JView { var $_total;

function display($tpl = null) { global $mainframe; $tmp = JRequest::getVar("Itemid"); $model = $this->getModel(); $lists = $model->getAllCategories(); $this->assignRef('rows', $lists); $pagining = $model->getPagination(); $linkPage = $pagining->getPagesLinks(); $this->assignRef('pagining', $linkPage); parent::display($tpl); } }

Hiển thị phân trang trong layout. Mở tập tin default.php trong thư mục

/joomla/com_book/views/categories/tmpl sửa thành: (adsbygoogle = window.adsbygoogle || []).push({});

//Code cũ <?php

foreach ($this->rows as $key => $info){

$link = "index.php?option=com_book&view=category&id=" . $info->id;

echo '<div class="book-category"><a href="' . $link . '">' . $info->category . '</a></div>';

} ?>

<div class="div-clear"></div>

<div style="text-align: center; padding: 4px;"><?php echo $this->pagining; ?></div>

2. Xây dựng chức năng hiển thị một category

Để xây dựng chức năng này chúng ta cần thực hiện những công việc sau:

Một phần của tài liệu giáo trình joomla 1.5 Toàn tập (Trang 68 - 71)