Created
September 29, 2012 06:03
-
-
Save allen501pc/3803326 to your computer and use it in GitHub Desktop.
幸運輪盤點名系統
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
<?php | |
/* 檔案名稱:class_db.php | |
* 作者:Jyun-Yao Huang | |
* 簡述:自行製作的資料庫Object,支援新增、刪除、修改等查詢功能。 | |
*/ | |
// 使用設定檔. | |
include_once('setting.php'); | |
class C_db | |
{ | |
public $m_num_of_rows,$m_b_link_state; | |
private $m_link_; | |
public $m_result_; | |
function __construct() | |
{ | |
$this->m_b_link_state=false; | |
$this->m_link_=mysql_pconnect(_DB_HOST,_DB_ACCOUNT,_DB_PASSWORD) or die("Error database connect!"); | |
if(!empty($this->m_link_)) | |
{ | |
if(!(mysql_select_db(_DB_DBNAME,$this->m_link_))) | |
{ | |
echo 'Unable to use any databases!'.mysql_error(); | |
} | |
else | |
{ | |
mysql_query("SET NAMES 'UTF8'",$this->m_link_); | |
mysql_query("SET CHARACTER SET=UTF8",$this->m_link_); | |
mysql_query("SET CHARACTER_SET_RESULTS=UTF8'",$this->m_link_); | |
$this->m_b_link_state=true; | |
} | |
} | |
} | |
private function clear_result() | |
{ | |
$this->m_num_of_rows=0; | |
} | |
public function query($query) | |
{ | |
$this->clear_result(); | |
if(!empty($query)) | |
{ | |
$this->m_result_=mysql_query($query,$this->m_link_); | |
if($this->m_result_) | |
$this->m_num_of_rows=mysql_num_rows($this->m_result_); | |
} | |
} | |
public function update_query($query) | |
{ | |
$this->clear_result(); | |
if(!empty($query)) | |
{ | |
$this->m_result_=mysql_query($query,$this->m_link_); | |
if($this->m_result_) | |
return true; | |
} | |
return false; | |
} | |
public function delete_query($query) | |
{ | |
return $this->update_query($query); | |
} | |
public function insert_query($query) | |
{ | |
if(!empty($query)) | |
{ | |
if(mysql_query($query,$this->m_link_)) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
// @Function : get_key() | |
// @Breif : get insert key | |
// @Input : none. | |
// @Output: insert query primary key. | |
public function get_key() | |
{ | |
return mysql_insert_id($this->m_link_); | |
} | |
public function fetch_result() | |
{ | |
return mysql_fetch_assoc($this->m_result_); | |
} | |
function __destruct() | |
{ | |
/* Close MySQL Connection */ | |
//mysql_close($this->link); | |
} | |
function close_db_link() | |
{ | |
mysql_close($this->link); | |
} | |
function create_db_link() | |
{ | |
$this->__construct(); | |
} | |
} | |
?> |
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
<?php | |
/* 檔案名稱:order.php | |
* 簡易功能描述:輸入點名人數與幸運值,產生點名表。 | |
* 製作人:Jyun-Yao Huang ([email protected]) | |
*/ | |
/* 定義標題 */ | |
define("_TITLE","幸運輪盤點名系統"); | |
/* 資料庫表格, ex: student */ | |
define("_TABLE_NAME","student"); | |
ob_start(); | |
session_start(); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<title><?php echo _TITLE; ?></title> | |
</head> | |
<body> | |
<?php | |
// 使用DB Object. | |
include_once("class_db.php"); | |
// 抽點的學生人數. | |
$Students = 15; | |
if(isset($_POST['students'])) | |
$Students = $_POST['students']; | |
$Number = 10; | |
if(isset($_POST['number'])) | |
$Number = $_POST['number']; | |
echo "<h1>"._TITLE."</h1>"; | |
echo <<<_HTML | |
<h3>本系統自動儲存已產生之點名名單,除非關閉瀏覽器或是按下「清除」。</h3> | |
<form action="order.php?action=clear" method="post"> | |
<div> | |
<input type='submit' value="清除已產生的點名名單" /> | |
</div> | |
</form> | |
<form action="order.php?action=run" method="post"> | |
<div> | |
幸運數字:<input name="number" value="$Number" /> | |
</div> | |
<div> | |
抽點學生人數:<input name="students" value="$Students" /> | |
</div> | |
<input type='submit' value="產生點名名單" /> | |
</form> | |
_HTML; | |
if(@$_GET['action']=="clear") | |
{ | |
unset($_SESSION['previous_stored']); | |
} | |
if(@$_GET['action']=="run") | |
{ | |
$ResultList = array(); | |
$DB = new C_db(); | |
srand($_POST['random']+time()); | |
// 獲得要點名的學生數量. | |
if(isset($_POST['students']) && !empty($_POST['students'])) | |
$Students = (int) $_POST['students']; | |
$CanExecute = true; | |
// 檢查剩下的學生數量 | |
if(isset($_SESSION['previous_stored'])) | |
{ | |
$QueryLang = "SELECT COUNT(*) as COUNT FROM `"._TABLE_NAME."` WHERE `no` NOT IN ('".implode("','",array_keys($_SESSION['previous_stored']))."')"; | |
$DB->query($QueryLang); | |
$Result = $DB->fetch_result(); | |
// 剩下的數量遠小於要點名的數量1.1倍,則不產生點名. | |
if($Result['COUNT'] < $Students*(1.1)) | |
{ | |
$CanExecute = false; | |
echo "<div style='color:blue;'>目前剩下的點名名額:".((int)$Result['COUNT'])."</div>"; | |
} | |
else | |
echo "<div style='color:blue;'>目前剩下的點名名額:".((int)$Result['COUNT']- $Students)."</div>"; | |
} | |
else | |
{ | |
$QueryLang = "SELECT COUNT(*) as COUNT FROM `"._TABLE_NAME."`"; | |
$DB->query($QueryLang); | |
$Result = $DB->fetch_result(); | |
echo "<div style='color:blue;'>目前剩下的點名名額:".((int)$Result['COUNT']- $Students)."</div>"; | |
} | |
for($Count=0;$CanExecute && $Count<$Students;++$Count) | |
{ | |
$Conflict= true; | |
while($Conflict) | |
{ | |
// 假設學生總人數為60人. | |
$RandomNumber = rand(0,59); | |
$QueryLang = "SELECT * FROM `"._TABLE_NAME."` LIMIT $RandomNumber,1"; | |
$DB->query($QueryLang); | |
$Result = $DB->fetch_result(); | |
$No = $Result['no']; | |
$Name = $Result['name']; | |
if(@empty($ResultList["$No"]) && @empty($_SESSION['previous_stored'][$No])) | |
{ | |
$ResultList[$No] = $Name; | |
$Conflict = false; | |
} | |
else | |
{ | |
$Conflict = true; | |
} | |
} | |
} | |
// 排序. | |
ksort($ResultList); | |
if(!isset($_SESSION['previous_stored'])) | |
$_SESSION['previous_stored'] = array(); | |
foreach($ResultList as $Key => $Name) | |
{ | |
echo $Key." ".$Name."<br />"; | |
$_SESSION['previous_stored'][$Key] = $Name; | |
//array_push($_SESSION['previous_stored'],array("$Key" =>$Name)); | |
} | |
// 若無法產生點名,則清除。 | |
if(!$CanExecute) | |
{ | |
echo "<div style='color:red;'>剩下的點名名額過少,系統不足以<b>即時列出</b>點名清單,請清除點名記錄!</div>"; | |
} | |
echo "<b>已產生的點名清單</b><br />"; | |
foreach($_SESSION['previous_stored'] as $Key => $Name) | |
{ | |
echo $Key." ".$Name."<br />"; | |
} | |
} | |
?> | |
</body> | |
</html> | |
<?php | |
ob_end_flush(); | |
?> |
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
<?php | |
/* ********************************************************************************************************** * | |
* 專案名稱: 幸運輪盤點名系統 * | |
* 功能撰寫人 : Jyun-Yao Huang ([email protected]) * | |
* 功能描述:設定檔 * | |
* ********************************************************************************************************** */ | |
/* ************************ 資料庫設定 ******************************* */ | |
/* 設定資料庫名稱 */ | |
define("_DB_DBNAME","your_db_name"); | |
/* 設定資料庫所在位置 */ | |
define("_DB_HOST","localhost"); | |
/* 設定資料庫帳號 */ | |
define("_DB_ACCOUNT","your_db_account"); | |
/* 設定資料庫密碼 */ | |
define("_DB_PASSWORD","your_db_password"); | |
/* ******************************************************************** */ | |
/* ************************* 網站功能設定 ***************************** */ | |
/* ************************** 一般設定 ************************************ */ | |
/* 關閉error report warning */ | |
error_reporting(E_ERROR); | |
/* ******************************************************************** */ | |
/* ***************************************************************************************************** */ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment