Próbowałem wstawiać w różnych miejscach i cały czas się wykrzacza. Podrzucam większą część kodu. Pomożecie?
FUNCTION askquestion($sender) {
global $conn, $access_token, $payload, $input;
$senderqry = "select sender_id from curr_question where sender_id='$sender'";
$senderesult = mysqli_query ( $conn, $senderqry );
IF ($fetch = mysqli_fetch_array ( $senderesult )) {
$updatecurrquesqry = "select category from curr_question where sender_id='$sender'";
$res = mysqli_query ( $conn, $updatecurrquesqry );
$category = mysqli_fetch_array ( $res, MYSQLI_ASSOC );
$category = ucwords ( $category ['category'] );
} else {
$updques = "";
$updquesid = "";
$correctans = "";
//$category = ucwords ( $payload );
$category = $payload;
$updatecurrquesqry = "insert into curr_question(sender_id,curr_ques,ques_id,correct_answer,category)values('$sender','$updques','$updquesid','$correctans','$category')";
mysqli_query ( $conn, $updatecurrquesqry );
}
$fh = fopen ( "/testbot/log.txt", "a" );
$quesqry1 = "select * from csv where Category='$category' ORDER BY RAND()";
$result = mysqli_query ( $conn, $quesqry1 );
// $res1 = mysqli_fetch_array ( $result, MYSQLI_ASSOC );
/*
* $log = "[" . date ( 'Y-m-d h:i:s' ) . "] Queryy result " . $quesqry1 . "...".json_encode($res1)."\n\n";
* fwrite ( $fh, $log );
*/
while ( $res = mysqli_fetch_array ( $result, MYSQLI_ASSOC ) ) {
/*
* $log = "[" . date ( 'Y-m-d h:i:s' ) . "] Queryy result 1 " . $quesqry1 . "..." . json_encode ( $res ) . "\n\n";
* fwrite ( $fh, $log );
*/
// $askedquestionidArray = array ();
$quesidapdqry = "select ques_id from curr_question where sender_id='$sender'";
$quesidapdresult = mysqli_query ( $conn, $quesidapdqry );
$quesidapdres = mysqli_fetch_array ( $quesidapdresult, MYSQLI_ASSOC );
/*
* $log = "[" . date ( 'Y-m-d h:i:s' ) . "] question id append qry- " . $quesidapdqry . "...result ques append-" . json_encode ( $quesidapdres ) . "\n\n";
* fwrite ( $fh, $log );
*/
$arr = explode ( ',', $quesidapdres ['ques_id'] );
$c = 0;
$c = sizeof ( $arr );
IF ($c > 10) {
$scoreqry = "select * from curr_question where sender_id='$sender'";
$scoreresult = mysqli_query ( $conn, $scoreqry );
$resscore = mysqli_fetch_array ( $scoreresult, MYSQLI_ASSOC );
$message_to_reply = "Twój końcowy wynik to " . $resscore ['points'] . " na 10 punktów";
sendMessage ( $sender, $message_to_reply );
sleep(2);
pickCategory();
/*sleep(2);
$message_to_reply = "Wpisz'start' aby zacząć od nowa";
sendMessage ( $sender, $message_to_reply );*/
break;
}
IF (! in_array ( $res ['id'], $arr )) { /*
* if(in_array($quescurrid,$arr)){$message_to_reply = "A może kolejne pytanie?";
* sendMessage ( $sender, $message_to_reply );}
*/
$curr_ques_id=$res ['id'];
$updques = $res ['Question'];
$updquesid = $quesidapdres ['ques_id'] . $res ['id'] . ","; //
// $updquesid =$res ['id'];
$correctans = $res ['Correct_Answer'];
$updatecurrquesqry = "update curr_question set curr_ques_id='$curr_ques_id',curr_ques ='$updques',ques_id='$updquesid',correct_answer='$correctans',answered_status='0' where sender_id='$sender'";
/*
* $log = "[" . date ( 'Y-m-d h:i:s' ) . "] QUERY UPDATE- " . $updatecurrquesqry . "\n\n";
* fwrite ( $fh, $log );
*/
/*
* $updatecurrquesqry = "insert into curr_question (curr_ques,ques_id,correct_answer)values('$updques','$updquesid','$correctans') where sender_id='$sender'";
* // $askedquestionidArray [] = $res ['id'];
*/
mysqli_query ( $conn, $updatecurrquesqry );
// Make request TO Time API
ini_set ( 'user_agent', 'Mozilla/4.0 (compatible; MSIE 6.0)' );
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=' . $access_token;
$ch = curl_init ( $url );
$jsonData = '{
"recipient":{
"id":"' . $sender . '"
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"' . $res ['Question'] . '",
"image_url":"' . $res ['Picture'] . '",
"buttons":[
{
"type":"postback",
"title":"' . $res ['Answer_A'] . '",
"payload":"Answer_A-' . $res ['id'] . '"
},
{
"type":"postback",
"title":"' . $res ['Answer_B'] . '",
"payload":"Answer_B-' . $res ['id'] . '"
},
{
"type":"postback",
"title":"' . $res ['Answer_C'] . '",
"payload":"Answer_C-' . $res ['id'] . '"
}
]
}
]
}
}
}