Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] losowe sortowanie wyników z wiersza tabeli
Forum PHP.pl > Forum > Przedszkole
jodello
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.
Tomplus
Tam gdzie tworzysz listę pytań możesz stworzyć coś takiego:
  1. $answer = [$col['Answer_A'], $col['Answer_B'], $col['Answer_C']];
  2. shuffle($answer);


potem dodajesz do "buttons" i masz.

To uproszczone, bo twojego kodu tworzyący JSONa nie znamy.
jodello
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. }

nospor
Przed
$jsonData = '{

daj:
  1. $answers = array(
  2. $res['Answer_A'],
  3. $res['Answer_B'],
  4. $res['Answer_C']
  5. );
  6. shuffle($answers);


A potem w tekscie
nie $res['Answer_A'] a $answers[0]
nie $res['Answer_B'] a $answers[1]
nie $res['Answer_C'] a $answers[2]
jodello
Finalnie zadziałało jak dałem:

  1. /********************* Answer Shuffle ************************/
  2. $payloadval = array();
  3. $titleval = array();
  4. $answerArray =array("A","B","C");
  5. shuffle($answerArray);
  6. foreach($answerArray as $stringa){
  7. if($stringa=='A'){
  8. $title = $res ['Answer_A'];
  9. $payload = 'Answer_A-'.$res ['id'];
  10. }
  11. if($stringa=='B'){
  12. $title = $res ['Answer_B'];
  13. $payload = 'Answer_B-'.$res ['id'];
  14. }
  15. if($stringa=='C'){
  16. $title = $res ['Answer_C'];
  17. $payload = 'Answer_C-'.$res ['id'];
  18. }
  19. $payloadval[] = $payload;
  20. $titleval[] = $title;
  21. }
  22. /***************************************************************/
  23.  
  24. $jsonData = '{
  25. "recipient":{
  26. "id":"' . $sender . '"
  27.  
  28. },
  29. "message":{
  30. "attachment":{
  31. "type":"template",
  32. "payload":{
  33. "template_type":"generic",
  34. "elements":[
  35. {
  36. "title":"' . $res ['Question'] . '",
  37. "image_url":"' . $res ['Picture'] . '",
  38. "buttons":[
  39. {
  40. "type":"postback",
  41. "title":"' . $titleval[0] . '",
  42. "payload":"' . $payloadval[0] . '"
  43. },
  44. {
  45. "type":"postback",
  46. "title":"' . $titleval[1] . '",
  47. "payload":"' . $payloadval[1] . '"
  48. },
  49. {
  50. "type":"postback",
  51. "title":"' . $titleval[2] . '",
  52. "payload":"' . $payloadval[2] . '"
  53. }
  54. ]
  55. }
  56. ]
  57. }
  58. }
  59. }
  60.  


Wrzucam dla potomnych smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.