Php q & A application interface instance and php Q & A interface instance. Php q & A application interface instance. php Q & A interface instance this article describes how to implement the php q & A application interface. Share it with you for your reference. The specific implementation method is as follows: q PHP q & A application interface instance and php q & A interface instance
This example describes how to implement the php q & A application interface. Share it with you for your reference. The specific implementation method is as follows:
The question_get.php file is as follows:
The code is as follows:
<? Php
Session_cache_expire (60 );
Session_start ();
If (! Isset ($ _ SESSION ['zaszh _ user_id ']) {
Echo json_encode (array ('status' => 'error', 'MSG '=>' connection timeout. open the page again. '));
Exit;
}
$ User_id = $ _ SESSION ['zaszh _ user_id '];
// $ User_id = 1; // used for testing
// Obtain 5 questions randomly
$ 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 ');
// Number of answers
$ Mysqli-> query ("update zaszh_user set answer_surplus = answer_surplus-1 where id = {$ user_id} and answer_surplus> 0 ");
If ($ mysqli-> affected_rows ){
// Remaining times
} Else {
// No remaining times
Echo json_encode (array ('status' => 'error', 'MSG '=>' the remaining answers are used up. come back tomorrow ~ '));
$ Mysqli-> close ();
Exit;
}
// Question
If ($ stmt = $ mysqli-> prepare ("select question, A, B, C, D, answer from zaszh_question where id in (?,?,?,?,?) ")){
$ Stmt-> bind_param ('iiiiiii ', $ 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' => $,
'B' => $ B,
'C' => $ C,
'D' => $ D,
'Answer' => $ answer
);
}
// Answer record
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 '=>' system error. '));
}
}
$ Stmt-> close ();
}
$ Mysqli-> close ();
I hope this article will help you with php programming.
Examples in this article describes how to implement the php q & A application interface. Share it with you for your reference. The specific implementation method is as follows: q...