Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] losowe sortowanie wyników z wiersza tabeli
jodello
post
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 17.11.2016

Ostrzeżenie: (0%)
-----


Cześć,

jestem w trakcie robienia prostego Quizu chatbota dla Facebook Messengera i napotkałem na problem w ostatniej fazie jego tworzenia. Pytania dla użytkowników są wyświetlanie w nastepującej formie:

  1. "message":{
  2. "attachment":{
  3. "type":"template",
  4. "payload":{
  5. "template_type":"generic",
  6. "elements":[
  7. {
  8. "title":"' . $res ['Question'] . '",
  9. "image_url":"' . $res ['Picture'] . '",
  10. "buttons":[
  11. {
  12. "type":"postback",
  13. "title":"' . $res ['Answer_A'] . '",
  14. "payload":"Answer_A-' . $res ['id'] . '"
  15. },
  16. {
  17. "type":"postback",
  18. "title":"' . $res ['Answer_B'] . '",
  19. "payload":"Answer_B-' . $res ['id'] . '"
  20. },
  21. {
  22. "type":"postback",
  23. "title":"' . $res ['Answer_C'] . '",
  24. "payload":"Answer_C-' . $res ['id'] . '"
  25. }



Baza pytań i odpowiedzi jest zaciągana z bazy SQL, która bazuje na csv o następującym schemacie:

  1. INSERT INTO `csv` (`id`, `Category`, `Question`, `Answer_A`, `Answer_B`, `Answer_C`, `Correct_Answer`, `Picture`) VALUES
  2. (1, 'Warszawa', 'Jak nazywa się™ fort znajdują…cy się™ w Parku Żeromskiego?', 'Sokolnickiego', 'Gieorgija', 'Traugutta', 'a', ''),


Chciałbym, aby wyświetlane odpowiedzi (Answer_A;Answer_B,Answer_C) wyświetlały się losowo.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
jodello
post
Post #2





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 17.11.2016

Ostrzeżenie: (0%)
-----


Próbowałem wstawiać w różnych miejscach i cały czas się wykrzacza. Podrzucam większą część kodu. Pomożecie?

  1. FUNCTION askquestion($sender) {
  2. global $conn, $access_token, $payload, $input;
  3.  
  4. $senderqry = "select sender_id from curr_question where sender_id='$sender'";
  5. $senderesult = mysqli_query ( $conn, $senderqry );
  6.  
  7. IF ($fetch = mysqli_fetch_array ( $senderesult )) {
  8.  
  9. $updatecurrquesqry = "select category from curr_question where sender_id='$sender'";
  10. $res = mysqli_query ( $conn, $updatecurrquesqry );
  11. $category = mysqli_fetch_array ( $res, MYSQLI_ASSOC );
  12. $category = ucwords ( $category ['category'] );
  13. } else {
  14. $updques = "";
  15. $updquesid = "";
  16. $correctans = "";
  17. //$category = ucwords ( $payload );
  18. $category = $payload;
  19. $updatecurrquesqry = "insert into curr_question(sender_id,curr_ques,ques_id,correct_answer,category)values('$sender','$updques','$updquesid','$correctans','$category')";
  20. mysqli_query ( $conn, $updatecurrquesqry );
  21. }
  22.  
  23. $fh = fopen ( "/testbot/log.txt", "a" );
  24. $quesqry1 = "select * from csv where Category='$category' ORDER BY RAND()";
  25. $result = mysqli_query ( $conn, $quesqry1 );
  26.  
  27. // $res1 = mysqli_fetch_array ( $result, MYSQLI_ASSOC );
  28. /*
  29. * $log = "[" . date ( 'Y-m-d h:i:s' ) . "] Queryy result " . $quesqry1 . "...".json_encode($res1)."\n\n";
  30. * fwrite ( $fh, $log );
  31. */
  32.  
  33. while ( $res = mysqli_fetch_array ( $result, MYSQLI_ASSOC ) ) {
  34. /*
  35. * $log = "[" . date ( 'Y-m-d h:i:s' ) . "] Queryy result 1 " . $quesqry1 . "..." . json_encode ( $res ) . "\n\n";
  36. * fwrite ( $fh, $log );
  37. */
  38. // $askedquestionidArray = array ();
  39. $quesidapdqry = "select ques_id from curr_question where sender_id='$sender'";
  40. $quesidapdresult = mysqli_query ( $conn, $quesidapdqry );
  41. $quesidapdres = mysqli_fetch_array ( $quesidapdresult, MYSQLI_ASSOC );
  42. /*
  43. * $log = "[" . date ( 'Y-m-d h:i:s' ) . "] question id append qry- " . $quesidapdqry . "...result ques append-" . json_encode ( $quesidapdres ) . "\n\n";
  44. * fwrite ( $fh, $log );
  45. */
  46. $arr = explode ( ',', $quesidapdres ['ques_id'] );
  47. $c = 0;
  48. $c = sizeof ( $arr );
  49. IF ($c > 10) {
  50. $scoreqry = "select * from curr_question where sender_id='$sender'";
  51. $scoreresult = mysqli_query ( $conn, $scoreqry );
  52. $resscore = mysqli_fetch_array ( $scoreresult, MYSQLI_ASSOC );
  53. $message_to_reply = "Twój końcowy wynik to " . $resscore ['points'] . " na 10 punktów";
  54. sendMessage ( $sender, $message_to_reply );
  55. sleep(2);
  56. pickCategory();
  57. /*sleep(2);
  58. $message_to_reply = "Wpisz'start' aby zacząć od nowa";
  59. sendMessage ( $sender, $message_to_reply );*/
  60. break;
  61. }
  62. IF (! in_array ( $res ['id'], $arr )) { /*
  63. * if(in_array($quescurrid,$arr)){$message_to_reply = "A może kolejne pytanie?";
  64. * sendMessage ( $sender, $message_to_reply );}
  65. */
  66. $curr_ques_id=$res ['id'];
  67. $updques = $res ['Question'];
  68. $updquesid = $quesidapdres ['ques_id'] . $res ['id'] . ","; //
  69. // $updquesid =$res ['id'];
  70.  
  71. $correctans = $res ['Correct_Answer'];
  72. $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'";
  73.  
  74.  
  75.  
  76. /*
  77. * $log = "[" . date ( 'Y-m-d h:i:s' ) . "] QUERY UPDATE- " . $updatecurrquesqry . "\n\n";
  78. * fwrite ( $fh, $log );
  79. */
  80. /*
  81. * $updatecurrquesqry = "insert into curr_question (curr_ques,ques_id,correct_answer)values('$updques','$updquesid','$correctans') where sender_id='$sender'";
  82. * // $askedquestionidArray [] = $res ['id'];
  83. */
  84. mysqli_query ( $conn, $updatecurrquesqry );
  85.  
  86.  
  87.  
  88. // Make request TO Time API
  89. ini_set ( 'user_agent', 'Mozilla/4.0 (compatible; MSIE 6.0)' );
  90.  
  91. $url = 'https://graph.facebook.com/v2.6/me/messages?access_token=' . $access_token;
  92. $ch = curl_init ( $url );
  93. $jsonData = '{
  94. "recipient":{
  95. "id":"' . $sender . '"
  96.  
  97. },
  98.  
  99.  
  100. "message":{
  101. "attachment":{
  102. "type":"template",
  103. "payload":{
  104. "template_type":"generic",
  105. "elements":[
  106. {
  107. "title":"' . $res ['Question'] . '",
  108. "image_url":"' . $res ['Picture'] . '",
  109. "buttons":[
  110. {
  111. "type":"postback",
  112. "title":"' . $res ['Answer_A'] . '",
  113. "payload":"Answer_A-' . $res ['id'] . '"
  114. },
  115. {
  116. "type":"postback",
  117. "title":"' . $res ['Answer_B'] . '",
  118. "payload":"Answer_B-' . $res ['id'] . '"
  119. },
  120. {
  121. "type":"postback",
  122. "title":"' . $res ['Answer_C'] . '",
  123. "payload":"Answer_C-' . $res ['id'] . '"
  124. }
  125. ]
  126. }
  127. ]
  128. }
  129. }
  130. }

Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.10.2025 - 02:12