程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> PHP實現Google plus的好友拖拽分組效果

PHP實現Google plus的好友拖拽分組效果

編輯:PHP綜合

你一直在尋找google pls的好友拖拽分組功能嗎?google實現的太帥了!我已經使用PHP和jQuery實現了同樣的好友拖拽添加分組的應用。本篇PHP教程就來告訴你如何實現,希望我的示例會對你的社交網站項目有所幫助。

效果如下:

示例數據庫包含三個表,即用戶和用戶組之間的關系。

用戶表Members

表包含成員(用戶)數據,如member_id,member_image等。

CREATE TABLE IF NOT EXISTS `members` ( 
`member_id` int(9) NOT NULL AUTO_INCREMENT, 
`member_name` varchar(220) NOT NULL, 
`member_image` text NOT NULL, 
`dated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
PRIMARY KEY (`member_id`) 
); 

用戶組Groups

CREATE TABLE IF NOT EXISTS `groups` ( 
`group_id` int(9) AUTO_INCREMENT, 
`group_name` varchar(220), 
`sort` int(9), 
`date` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
PRIMARY KEY (`group_id`), 
KEY `sort` (`sort`) 
); 

用戶組之間的關系User_group

用戶和用戶租表的關系表user_group包含user_id(memeber_id一樣),group_id,member_id()和sort(排序)字段。

CREATE TABLE IF NOT EXISTS `user_group` ( 
`id` int(9) NOT NULL AUTO_INCREMENT, 
`user_id` int(9) NOT NULL, 
`group_id` int(9) NOT NULL, 
`member_id` int(9) NOT NULL, 
`sort` int(9) NOT NULL, 
PRIMARY KEY (`id`) 
); 

Javascript腳本

我們對兩個類屬性:.members和.group運用拖拽。

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script> 
<script type="text/javascript" src="jquery.livequery.min.js"></script> 
<script type="text/javascript" > 
$(function() 
{ 
// Initiate draggable for public and groups 
var $gallery = $( ".members, .group" ); 
$( "img", $gallery ).live("mouseenter", function() 
{ 
var $this = $(this); 
if(!$this.is(':data(draggable)')) 
{ 
$this.draggable({ 
helper: "clone", 
containment: $( "#demo-frame" ).length ? "#demo-frame" : "document", 
cursor: "move" 
}); 
} 
}); 
// Initiate Droppable for groups 
// Adding members into groups 
// Removing members from groups 
// Shift members one group to another 
$(".group").livequery(function(){ 
var casePublic = false; 
$(this).droppable({ 
activeClass: "ui-state-highlight", 
drop: function( event, ui ) { 
var m_id = $(ui.draggable).attr('rel'); 
if(!m_id) 
{ 
casePublic = true; 
var m_id = $(ui.draggable).attr("id"); 
m_id = parseInt(m_id.substring(3)); 
} 
var g_id = $(this).attr('id'); 
dropPublic(m_id, g_id, casePublic); 
$("#mem"+m_id).hide(); 
$( "<li></li>" ).html( ui.draggable ).appendTo( this ); 
}, 
out: function(event, ui) { 
var m_id = $(ui.draggable).attr('rel'); 
var g_id = $(this).attr('id'); 
$(ui.draggable).hide("explode", 1000); 
removeMember(g_id,m_id); 
} 
}); 
}); 
// Add or shift members from groups 
function dropPublic(m_id, g_id,caseFrom) 
{ 
$.ajax({ 
type:"GET", 
url:"groups.php?m_id="+m_id+"&g_id="+g_id, 
cache:false, 
success:function(response){ 
$.get("groups.php?reload_groups", function(data){ 
$("#groupsall").html(data); 
$("#added"+g_id).animate({"opacity" : "10" },10); 
$("#added"+g_id).show(); 
$("#added"+g_id).animate({"margin-top": "-50px"}, 450); 
$("#added"+g_id).animate({"margin-top": "0px","opacity" : "0" }, 450); 
}); 
} 
}); 
} 
// Remove memebers from groups 
// It will restore into public groups or non grouped members 
function removeMember(g_id,m_id) 
{ 
$.ajax({ 
type:"GET", 
url:"groups.php?do=drop&mid="+m_id, 
cache:false, 
success:function(response){ 
$("#removed"+g_id).animate({"opacity" : "10" },10); 
$("#removed"+g_id).show(); 
$("#removed"+g_id).animate({"margin-top": "-50px"}, 450); 
$("#removed"+g_id).animate({"margin-top": "0px","opacity" : "0" }, 450); 
$.get("groups.php?reload", function(data){ $("#public").html(data); }); 
} 
}); 
} 
}); 
</script> 

groups.php

我們在這裡處理拖拽添加用戶組的數據處理功能。

<?php 
require_once("multipleDiv.inc.php"); 
// Initiate Object 
$obj = new Multiplediv(); 
// Add or Update Ajax Call 
if(isset($_GET['m_id']) and isset($_GET['g_id'])) 
{ 
$obj->addMembers((int)$_GET['m_id'], (int)$_GET['g_id']); 
exit; 
} 
// Remove Memebers from groups Ajax call 
if(isset($_GET['do'])) 
{ 
$obj->removeMember($_GET['mid']); 
exit; 
} 
// Reload groups each ajax call 
if(isset($_GET['reload'])){ echo $obj->getMembers_reload(); exit; } 
if(isset($_GET['reload_groups'])){ echo $obj->getmembergroups_reload(); exit; } 
// Initiate Groups and members 
$members = $obj->public_members(); 
$groups = $obj->groups(); 
?> 
Friends 
<div id="main_portion"> 
<div id="public"> 
<!-- Initiate members --> 
<?php 
if(!isset($members)) 
$members = $obj->public_members(); 
if($members) 
{ 
foreach($members as $member) 
{ 
extract($member); 
echo "<div class='members' id='mem".$member_id."'>\n"; 
echo "<img src='images/".$member_image."' rel='".$member_id."'>\n"; 
echo "<b>".ucwords($member_name)."</b>\n"; 
echo "</div>"; 
} 
} 
else 
echo "Members not available"; 
?> 
</div> 
<div id="groupsall"> 
Groups 
<!-- Initiate Groups --> 
<?php 
if(!isset($groups)) 
$groups = $obj->groups(); 
if($groups) 
{ 
foreach($groups as $group) 
{ 
extract($group); 
echo "<div id='".$group_id."' class='group'>\n"; 
echo ucwords($group_name); 
echo "<div id='added".$group_id."' class='add' style='display:none;' ><img src='images/green.jpg'></div>"; 
echo "<div id='removed".$group_id."' class='remove' style='display:none;' ><img src='images/red.jpg'></div>"; 
echo "<ul>\n"; 
echo $obj->updateGroups($group_id); 
echo "</ul></div>"; 
} 
} 
?> 
</div> 
</div> 

multipleDiv.inc.php

在這裡修改數據庫連接信息。

<?php 
// Database declaration's 
define("SERVER", "localhost"); 
define("USER", "username"); 
define("PASSWORD", "password"); 
define("DB", "database"); 
 
class Multiplediv 
{ 
........................ 
........................ 
......................... 
} 
?> 

到這裡,我們關於如何實現Google plus 的好友拖拽分組功能就完成了。以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved