1. Trang chủ
  2. » Công Nghệ Thông Tin

Thiết kế mạng xã hội với PHP - 14

10 10 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 5,62 MB

Nội dung

Tham khảo tài liệu ''thiết kế mạng xã hội với php - 14'', công nghệ thông tin, đồ họa - thiết kế - flash phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Friends and Relationships addTag( 'previous', '' ); } else { $this->registry->getObject('template')->getPage()-> addTag( 'first', "First page" ); $this->registry->getObject('template')->getPage()-> addTag( 'previous', "Previous page" ); } 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" ); $this->registry->getObject('template')->getPage()-> addTag( 'previous', "Last page" ); } } } 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: DINO SPACE! Members List

{name}

Keeper of {dino_name} a {dino_gender} {dino_breed}

[ 112 ] Download from Wow! eBook Chapter

Viewing page {page_number} of {num_pages}

{first} {previous} {next} {last}

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 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 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() == ) { 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 Chapter 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', "First page" ); $this->registry->getObject('template')->getPage()-> addTag( 'previous', "Previous page" ); } 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" ); $this->registry->getObject('template')->getPage()-> addTag( 'previous', "Last page" ); } } } [ 115 ] Download from Wow! eBook 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: DINO SPACE! Members List {letter}

{name}

Keeper of {dino_name} a {dino_gender} {dino_breed}

Viewing page {page_number} of {num_pages}

{first} {previous} {next} {last}

A B C D E F G H I J K L M N O P Q R S T U V W X [ 116 ] Download from Wow! eBook Chapter Y Z

Now if we go to http://ourwebsite/members/ we should see the following: Searching for users The user lists in themselves are primarily useful if a user spots someone they know of, or if the results display some information about them that the user can relate to, for example, the breed of their dinosaur, which may make them think "they also have a T-Rex, I'll connect with them!" We should also have a search feature, so that our users can search for other users: /** * Search for members based on their name * @param String $filter name * @param int $offset the offset * @return Object pagination object */ public function filterMembersByName( $filter='', $offset=0 ) { $filter = ( $this->registry->getObject('db')-> sanitizeData( urldecode( $filter ) ) ); require_once( FRAMEWORK_PATH 'lib/pagination/pagination.class.php'); $paginatedMembers = new Pagination( $this->registry ); $paginatedMembers->setLimit( 25 ); [ 117 ] Download from Wow! eBook Friends and Relationships $paginatedMembers->setOffset( $offset ); $query = "SELECT u.ID, u.username, p.name, p.dino_name, p.dino_gender, p.dino_breed FROM users u, profiles p WHERE p.user_id=u.ID AND u.active=1 AND u.banned=0 AND u.deleted=0 AND p.name LIKE'%".$filter."%' ORDER BY p.name ASC"; $paginatedMembers->setQuery( $query ); $paginatedMembers->setMethod( 'cache' ); $paginatedMembers->generatePagination(); return $paginatedMembers; } Our controller now needs a method to process the search request One important thing to note is that if we have performed a search, we can paginate because the search field is encoded with urlencode and passed in the URL, whereas when we first search, the name is in the name POST field We need to detect which is which, and process accordingly We can use an extra parameter in the method to indicate where the name data is: private function searchMembers( $search=true, $name='', $offset=0 ) { require_once( FRAMEWORK_PATH 'models/members.php'); $members = new Members( $this->registry ); if( $search == true ) { If we are searching, take the name from the POST data: // we are performing the search $pagination = $members->filterMembersByName( urlencode( $_POST['name'] ), $offset ); $name = urlencode( $_POST['name'] ); } else { If we are not searching, take the name from the URL (passed to this method directly): // we are paginating search results $pagination = $members->filterMembersByName( $name, $offset ); } if( $pagination->getNumRowsPage() == ) { $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'members/invalid.tpl.php' , 'footer.tpl.php'); } [ 118 ] Download from Wow! eBook Chapter else { $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'members/search.tpl.php' , 'footer.tpl.php'); $this->registry->getObject('template')->getPage()-> addTag( 'members', array( 'SQL', $pagination->getCache() ) ); $this->registry->getObject('template')->getPage()-> addTag( 'public_name', urldecode( $name ) ); $this->registry->getObject('template')->getPage()-> addTag( 'encoded_name', $name ); $this->registry->getObject('template')->getPage()-> addTag( 'page_number', $pagination->getCurrentPage() ); $this->registry->getObject('template')->getPage()-> addTag( 'num_pages', $pagination->getNumPages() ); Our pagination links require a reference to the name we are searching for: 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', "First page" ); $this->registry->getObject('template')->getPage()-> addTag( 'previous', "Previous page" ); } 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" ); $this->registry->getObject('template')->getPage()-> addTag( 'previous', "Last page" ); } } } In our controllers constructor, we need to perform our detection (if a search is being performed or not) and pass a suitable $search parameter to the searchMembers method: case 'search': $this->searchMembers( true, '', ); break; case 'search-results': $this->searchMembers( false, $urlBits[2] , intval( isset( $urlBits[3] ) ? $urlBits[3] : ) ); break; We also need a search box in our main members' list page, and a new template showing the results of the search: Search for a member Their name We now have a fully working search feature, as shown below: [ 120 ] Download from Wow! eBook Chapter Custom relationships Before we can connect to a user as a friend, we need to define the types of relationship our social network will support Within Dino Space, we should have relationships for: • Friends: For users who are friends with one another • Colleagues: For users who are colleagues with one another • Jogging buddies: For users who take their dinosaurs to the same morning jogging group We may, of course, wish to extend this as the site grows and changes as time goes on To facilitate these relationships we are going to need two database tables, one to maintain a list of types of relationships, and one to maintain a list of relationships between users Relationship types To represent the relationship types in the database, we could use the following database structure, for a relationship_types table: Field Type Description ID Integer, Auto Increment, Primary Key A unique ID for the relationship type Name Varchar The name of the relationship type, for example, friend Plural_name Varchar Plural version of the relationship type, for example, friends Active Boolean If this relationship type is active, and should users be able to form such relationships? Mutual Boolean Does this relationship require it to be a mutual connection, or can users connect without the permission of the other? [ 121 ] Download from Wow! eBook ... 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( )-> ... $this->registry->getObject('template' )-> getPage( )-> addTag( 'letter', " - Letter: " $alpha ); $this->registry->getObject('template' )-> getPage( )-> addTag( 'page_number', $pagination->getCurrentPage() ); $this->registry->getObject('template' )-> getPage( )-> ... $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( )->

Ngày đăng: 08/05/2021, 17:32

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w