21,886
社区成员
发帖
与我相关
我的任务
分享
$conn = mysql_connect("localhost", "test", "test");
mysql_select_db("test", $conn);
mysql_set_charset("utf8", $conn);
// 录取所有学生
function fetchAllStu()
{
// 志愿批次列表
$batch_list = array("first", "second", "third");
// 涉及多个更新操作,需要事务处理
mysql_query("start transaction");
$sql = "select * from hos";
$res = mysql_query($sql) or die(mysql_error());
while($info = mysql_fetch_array($res))
{
foreach ($batch_list as $val)
{
if(empty($info['same']))
{
// 不分男女录取
$need_cnt = $info['total'] - getStuCount($info['id']);
if(!fetchHosStu($val, $info['id'], $need_cnt))
{
mysql_query("rollback");
return false;
}
}
else
{
// 录取男生
$m_need_cnt = $info['men'] - getStuCount($info['id'], 'm');
if(!fetchHosStu($val, $info['id'], $m_need_cnt, 'm'))
{
mysql_query("rollback");
return false;
}
// 录取女生
$f_need_cnt = $info['wom'] - getStuCount($info['id'], 'f');
if(!fetchHosStu($val, $info['id'], $f_need_cnt, 'f'))
{
mysql_query("rollback");
return false;
}
}
}
}
mysql_query("commit");
return true;
}
// 录取指定医院的学生
function fetchHosStu($index, $hos_id, $num_limit, $sex=null)
{
$sub_condition = '';
if (isset($sex))
{
if(!isValidSex($sex))
{
return false;
}
$sub_condition = "where sex='{$sex}'";
}
// 只有人没录取满才能继续录取
if($num_limit > 0)
{
$cond = array();
$cond[] = "{$index}={$hos_id}";
// 拼接SQL
$condition = implode(' and ', $cond);
$sql = "update stu set result=1,get=1 where get=0 and {$condition} order by score desc limit {$num_limit}";
//echo $sql;
return mysql_query($sql);
}
return false;
}
// 判断是否是合法的性别
function isValidSex($sex)
{
return in_array(strtolower($sex), array('f', 'm'));
}
// 取得医院已录取学生数
function getStuCount($hos_id, $sex=null)
{
$cond = array();
if (isset($sex))
{
if(!isValidSex($sex))
{
return false;
}
$cond[] = "sex='{$sex}'";
}
$cond[] = "result={$hos_id}";
// 拼接SQL
$condition = "where ". implode(' and ', $cond);
$sql = "select count(1) as total from stu {$condition} for update";
//echo $sql;
$res = mysql_query($sql);
$info = mysql_fetch_array($res);
return $info['total'];
}
fetchAllStu();
if($_POST['cal'])
{
// 调用放这里
}
// 录取所有学生
function fetchAllStu()
{
// 志愿批次列表
$batch_list = array("first", "second", "third");
// 涉及多个更新操作,需要事务处理
mysql_query("start transaction");
$sql = "select * from hos";
$res = mysql_query($sql) or die(mysql_error());
while($info = mysql_fetch_array($res))
{
foreach ($batch_list as $val)
{
if(empty($info['same']))
{
// 不分男女录取
$need_cnt = $info['total'] - getStuCount($info['id']);
if(!fetchHosStu($val, $info['id'], $need_cnt))
{
mysql_query("rollback");
return false;
}
}
else
{
// 录取男生
$m_need_cnt = $info['men'] - getStuCount($info['id'], 'm');
if(!fetchHosStu($val, $info['id'], $m_need_cnt, 'm'))
{
mysql_query("rollback");
return false;
}
// 录取女生
$f_need_cnt = $info['wom'] - getStuCount($info['id'], 'f');
if(!fetchHosStu($val, $info['id'], $f_need_cnt, 'f'))
{
mysql_query("rollback");
return false;
}
}
}
}
mysql_query("commit");
return true;
}
// 录取指定医院的学生
function fetchHosStu($index, $hos_id, $num_limit, $sex=null)
{
global $field_list;
$sub_condition = '';
if (isset($sex))
{
if(!isValidSex($sex))
{
return false;
}
$sub_condition = "where sex='{$sex}'";
}
// 只有人没录取满才能继续录取
if($num_limit > 0)
{
$cond = array();
$cond[] = "id in (select id from stu {$sub_condition} order by score desc limit {$num_limit}) and get=0";
$cond[] = "{$index}={$hos_id}";
// 拼接SQL
$condition = "where ". implode(' and ', $cond);
$sql = "update stu set result={$hos_id},get=1 where {$condition}";
//echo $sql;
return mysql_query($sql);
}
return false;
}
// 判断是否是合法的性别
function isValidSex($sex)
{
return in_array(strtolower($sex), array('f', 'm'));
}
// 取得医院已录取学生数
function getStuCount($hos_id, $sex=null)
{
$cond = array();
if (isset($sex))
{
if(!isValidSex($sex))
{
return false;
}
$cond[] = "sex='{$sex}'";
}
$cond[] = "result={$hos_id}";
// 拼接SQL
$condition = "where ". implode(' and ', $cond);
$sql = "select count(1) as total from stu {$condition}";
//echo $sql;
$res = mysql_query($sql);
$info = mysql_fetch_array($res);
return $info['total'];
}
fetchAllStu();