Created
January 18, 2012 11:52
-
-
Save nike-17/1632676 to your computer and use it in GitHub Desktop.
test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Students { | |
protected $_server = 'localhost'; | |
protected $_username = 'username'; | |
protected $_password = 'password'; | |
protected $_database_name = 'database_name'; | |
public function __construct() { | |
mysql_connect($this->_server, $this->_username, $this->_password); | |
mysql_select_db($this->_database_name); | |
} | |
public function getLikedStudent() { | |
$student_Ids = $this->_getLikedStudentsIds(); | |
return $this->_getStudents($student_Ids); | |
} | |
public function getLikesOfUnlikers() { | |
$where = ' liked_ID NOT IN (SELECT DISTINCT(like_ID) from Likes)'; | |
$student_Ids = $this->_getLikedStudentsIds('like_id', $where); | |
return $this->_getStudents($student_Ids); | |
} | |
public function getFaggots() { | |
$student_Ids = $this->_getLikeAndLikersIds(); | |
return $this->_getStudents($student_Ids, 'NOT IN'); | |
} | |
protected function _getLikeAndLikersIds() { | |
$sql = "SELECT DISTINCT | |
least( like_ID, liked_ID ) AS least, | |
greatest( like_ID, liked_ID ) AS greatest | |
FROM Likes"; | |
$query = mysql_query($sql); | |
$likes = array(); | |
while ($row = mysql_fetch_array($query)) { | |
if (!in_array($row['least'], $likes)) { | |
$likes[] = $row['least']; | |
} | |
if (!in_array($row['greatest'], $likes)) { | |
$likes[] = $row['greatest']; | |
} | |
} | |
return $likes; | |
} | |
protected function _getStudents(array $student_Ids, $inOrNotIn = 'IN') { | |
if (count($student_Ids) == 0) { | |
return null; | |
} | |
return $this->_getStudentsByIds($student_Ids, $inOrNotIn); | |
} | |
protected function _getStudentsByIds(array $liked_Ids, $inOrNotIn = 'IN') { | |
$ids = implode(', ', $liked_Ids); | |
$sql = "SELECT name,grade from Students WHERE id {$inOrNotIn} ({$ids})"; | |
$query = mysql_query($sql); | |
$students_grade = array(); | |
while ($row = mysql_fetch_array($query)) { | |
$students_grade[] = array( | |
'name' => $row['name'], | |
'grade' => $row['grade'] | |
); | |
} | |
return $students_grade; | |
} | |
protected function _getLikedStudentsIds($field = 'liked_ID', $where = 1) { | |
$sql = "SELECT DISTINCT({$field}) from Likes WHERE {$where}"; | |
$query = mysql_query($sql); | |
$liked_Ids = array(); | |
while ($row = mysql_fetch_array($query)) { | |
$liked_Ids[] = (int) $row['liked_ID']; | |
} | |
return $liked_Ids; | |
} | |
} | |
$students = new Students(); | |
var_dump($students->getLikedStudent()); | |
var_dump($students->getLikesOfUnlikers()); | |
var_dump($students->getFaggots()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment