Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.62 MB, 10 trang )
Friends and Relationships <strong>{name}</strong> Keeper of <strong>{dino_name}</strong> a Viewing page {page_number} of {num_pages} {first} {previous} {next} {last} <strong>{name}</strong> Keeper of <strong>{dino_name}</strong> a Viewing page {page_number} of {num_pages} {first} {previous} {next} {last}
addTag( 'previous', '' );
}
else
{
$this->registry->getObject('template')->getPage()->
addTag( 'first', "<a href='members/list/'>First page</a>" );
$this->registry->getObject('template')->getPage()->
addTag( 'previous', "Previous page</a>"
);
}
if( $pagination->isLast() )
{
$this->registry->getObject('template')->getPage()->
addTag( 'next', '' );
$this->registry->getObject('template')->getPage()->
addTag( 'last', '' );
}
else
{
$this->registry->getObject('template')->getPage()->
addTag( 'first', "Next page</a>" );
$this->registry->getObject('template')->getPage()->
addTag( 'previous', "Last page</a>" );
}
}
}
To actually display the results of the lookup to the user, we need a template to form
our members' list view. This is essentially a copy of the main template file, with a
template loop for the members' information. This is saved in the views/default/
templates/members/list.tpl.php file:
<div id="main">
<div id="rightside">
</div>
<div id="content">DINO SPACE! Members List
<!-- START members -->
<strong>{dino_gender} {dino_breed}</strong>
[ 112 ]
Download from Wow! eBook <www.wowebook.com>
Chapter 4
<hr />
<!-- END members -->
</div>
</div>
This then displays our users list, which currently only contains me, as shown below!:
Paginated users by letter
As the site grows, but before we have such a large user base we need to consider
listing more relevant users, we are going to end up with a paginated list, which isn't
particularly easy to navigate. For example, if we have 20 users listed on each page
and 100 pages of results, if our user wants to quickly jump to users with a surname
beginning with P, it may take them several attempts. To make this easier, we can also
provide filtering by alphabetical character, so the user can click on P and be taken to
a list of users with surnames beginning with P. These lists may also be long, so they
too should be paginated.
The required model method to do this takes an additional parameter, which is
the letter the surname should start with. This is sanitized and then passed to the
query—the query works slightly differently by searching for spaces in the user's
name, then taking the word before the final space and comparing the first letter of
this word to the letter passed:
/**
*
*
*
*
Generated paginated members list by surname
@param String $letter
@param int $offset the offset
@return Object pagination object
[ 113 ]
Download from Wow! eBook <www.wowebook.com>
Friends and Relationships
*/
public function listMembersByLetter( $letter='A', $offset=0 )
{
$alpha = strtoupper( $this->registry->getObject('db')->
sanitizeData( $letter ) );
require_once( FRAMEWORK_PATH .
'lib/pagination/pagination.class.php');
$paginatedMembers = new Pagination( $this->registry );
$paginatedMembers->setLimit( 25 );
$paginatedMembers->setOffset( $offset );
$query = "SELECT u.ID, u.username, p.name, p.dino_name,
p.dino_gender, p.dino_breed FROM users u, profile p WHERE
p.user_id=u.ID AND u.active=1 AND u.banned=0 AND u.deleted=0
AND SUBSTRING_INDEX(p.name,' ', -1)LIKE'".$alpha."%' ORDER BY
SUBSTRING_INDEX(p.name,' ', -1) ASC";
$paginatedMembers->setQuery( $query );
$paginatedMembers->setMethod( 'cache' );
$paginatedMembers->generatePagination();
return $paginatedMembers;
}
Let's take a look at how this code fits together in our controller. The only real
difference is the method we call in our model:
private function listMembersAlpha( $alpha='A', $offset=0 )
{
Require and create our members model:
require_once( FRAMEWORK_PATH . 'models/members.php');
$members = new Members( $this->registry );
Call the listMembersByLetter method to get our pagination object:
$pagination = $members->listMembersByLetter( $alpha, $offset );
if( $pagination->getNumRowsPage() == 0 )
{
If there are no members, show that view:
$this->registry->getObject('template')->
buildFromTemplates('header.tpl.php', 'members/invalid.tpl.php'
, 'footer.tpl.php');
}
else
{
[ 114 ]
Download from Wow! eBook <www.wowebook.com>
Chapter 4
If there are members, show that view, and insert the appropriate data into the view:
$this->registry->getObject('template')->
buildFromTemplates('header.tpl.php', 'members/list.tpl.php',
'footer.tpl.php');
$this->registry->getObject('template')->getPage()->
addTag( 'members', array( 'SQL', $pagination->getCache() ) );
$this->registry->getObject('template')->getPage()->
addTag( 'letter', " - Letter: " . $alpha );
$this->registry->getObject('template')->getPage()->
addTag( 'page_number', $pagination->getCurrentPage() );
$this->registry->getObject('template')->getPage()->
addTag( 'num_pages', $pagination->getNumPages() );
if( $pagination->isFirst() )
{
$this->registry->getObject('template')->getPage()->
addTag( 'first', '');
$this->registry->getObject('template')->getPage()->
addTag( 'previous', '' );
}
else
{
$this->registry->getObject('template')->getPage()->
addTag( 'first', "<a href='members/alpha/".$alpha."/'>First
page</a>" );
$this->registry->getObject('template')->getPage()->
addTag( 'previous', "Previous
page</a>" );
}
if( $pagination->isLast() )
{
$this->registry->getObject('template')->getPage()->
addTag( 'next', '' );
$this->registry->getObject('template')->getPage()->
addTag( 'last', '' );
}
else
{
$this->registry->getObject('template')->getPage()->
addTag( 'first', "Next page</a>" );
$this->registry->getObject('template')->getPage()->
addTag( 'previous', "Last page</a>" );
}
}
}
[ 115 ]
Download from Wow! eBook <www.wowebook.com>
Friends and Relationships
We need to slightly update our members' list template; it needs a template
variable to display the currently active letter, and the letters A – Z as links
to filter down the list:
<div id="main">
<div id="rightside">
</div>
<div id="content">DINO SPACE! Members List {letter}
<!-- START members -->
<strong>{dino_gender} {dino_breed}</strong>
<hr />
<!-- END members -->
<a href="members/alpha/A/">A</a>
<a href="members/alpha/B/">B</a>
<a href="members/alpha/C/">C</a>
<a href="members/alpha/D/">D</a>
<a href="members/alpha/E/">E</a>
<a href="members/alpha/F/">F</a>
<a href="members/alpha/G/">G</a>
<a href="members/alpha/H/">H</a>
<a href="members/alpha/I/">I</a>
<a href="members/alpha/J/">J</a>
<a href="members/alpha/K/">K</a>
<a href="members/alpha/L/">L</a>
<a href="members/alpha/M/">M</a>
<a href="members/alpha/N/">N</a>
<a href="members/alpha/O/">O</a>
<a href="members/alpha/P/">P</a>
<a href="members/alpha/Q/">Q</a>
<a href="members/alpha/R/">R</a>
<a href="members/alpha/S/">S</a>
<a href="members/alpha/T/">T</a>
<a href="members/alpha/U/">U</a>
<a href="members/alpha/V/">V</a>
<a href="members/alpha/W/">W</a>
<a href="members/alpha/X/">X</a>
[ 116 ]
Download from Wow! eBook <www.wowebook.com>
Chapter 4
<a href="members/alpha/Y/">Y</a>
<a href="members/alpha/Z/">Z</a>