PHP答题类应用接口实例
程序员文章站
2024-02-04 13:11:22
本文实例讲述了php答题类应用接口的实现方法。分享给大家供大家参考。具体实现方法如下:
question_get.php文件如下:
复制代码 代码如下:...
本文实例讲述了php答题类应用接口的实现方法。分享给大家供大家参考。具体实现方法如下:
question_get.php文件如下:
复制代码 代码如下:
<?php
session_cache_expire(60);
session_start();
if(!isset($_session['zaszh_user_id'])){
echo json_encode(array('status'=>'error','msg'=>'连接超时,请重新打开页面。'));
exit;
}
$user_id = $_session['zaszh_user_id'];
// $user_id = 1; // 测试用
// 随机获取5道问题
$question_id = array();
while(sizeof($question_id)<5){
$num_rand = mt_rand(1,114);
if(!in_array($num_rand, $question_id))
$question_id[] = $num_rand;
}
require('connect_database.php');
// 答题次数
$mysqli->query("update zaszh_user set answer_surplus=answer_surplus-1 where id={$user_id} and answer_surplus>0");
if($mysqli->affected_rows){
// 有剩余次数
}else{
// 无剩余次数
echo json_encode(array('status'=>'error','msg'=>'今日答题剩余次数已用完,明天再来哦~'));
$mysqli->close();
exit;
}
// 题目
if($stmt = $mysqli->prepare("select question,a,b,c,d,answer from zaszh_question where id in(?,?,?,?,?)")){
$stmt->bind_param('iiiii',$question_id[0],$question_id[1],$question_id[2],$question_id[3],$question_id[4]);
$stmt->execute();
$stmt->bind_result($question,$a,$b,$c,$d,$answer);
$rows = array();
while($stmt->fetch()){
$rows[] = array(
'question'=>$question,
'a'=>$a,
'b'=>$b,
'c'=>$c,
'd'=>$d,
'answer'=>$answer
);
}
// 答题记录
if($stmt = $mysqli->prepare("insert into zaszh_answer(user_id,question1,question2,question3,question4,question5,create_date) values(?,?,?,?,?,?,unix_timestamp(now()))")){
$stmt->bind_param('iiiiii',$user_id,$question_id[0],$question_id[1],$question_id[2],$question_id[3],$question_id[4]);
$stmt->execute();
if($answer_id = $stmt->insert_id){
$param = array(
'answer_id'=>$answer_id
);
echo json_encode(array_merge($rows,$param));
}else{
echo json_encode(array('status'=>'error','msg'=>'系统出错。'));
}
}
$stmt->close();
}
$mysqli->close();
session_cache_expire(60);
session_start();
if(!isset($_session['zaszh_user_id'])){
echo json_encode(array('status'=>'error','msg'=>'连接超时,请重新打开页面。'));
exit;
}
$user_id = $_session['zaszh_user_id'];
// $user_id = 1; // 测试用
// 随机获取5道问题
$question_id = array();
while(sizeof($question_id)<5){
$num_rand = mt_rand(1,114);
if(!in_array($num_rand, $question_id))
$question_id[] = $num_rand;
}
require('connect_database.php');
// 答题次数
$mysqli->query("update zaszh_user set answer_surplus=answer_surplus-1 where id={$user_id} and answer_surplus>0");
if($mysqli->affected_rows){
// 有剩余次数
}else{
// 无剩余次数
echo json_encode(array('status'=>'error','msg'=>'今日答题剩余次数已用完,明天再来哦~'));
$mysqli->close();
exit;
}
// 题目
if($stmt = $mysqli->prepare("select question,a,b,c,d,answer from zaszh_question where id in(?,?,?,?,?)")){
$stmt->bind_param('iiiii',$question_id[0],$question_id[1],$question_id[2],$question_id[3],$question_id[4]);
$stmt->execute();
$stmt->bind_result($question,$a,$b,$c,$d,$answer);
$rows = array();
while($stmt->fetch()){
$rows[] = array(
'question'=>$question,
'a'=>$a,
'b'=>$b,
'c'=>$c,
'd'=>$d,
'answer'=>$answer
);
}
// 答题记录
if($stmt = $mysqli->prepare("insert into zaszh_answer(user_id,question1,question2,question3,question4,question5,create_date) values(?,?,?,?,?,?,unix_timestamp(now()))")){
$stmt->bind_param('iiiiii',$user_id,$question_id[0],$question_id[1],$question_id[2],$question_id[3],$question_id[4]);
$stmt->execute();
if($answer_id = $stmt->insert_id){
$param = array(
'answer_id'=>$answer_id
);
echo json_encode(array_merge($rows,$param));
}else{
echo json_encode(array('status'=>'error','msg'=>'系统出错。'));
}
}
$stmt->close();
}
$mysqli->close();
希望本文所述对大家的php程序设计有所帮助。