Guest User

Untitled

a guest
Jan 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. json_decode() expects parameter 1 to be string
  2.  
  3. public function actionUpdateVoteOnce(){
  4.  
  5. $postdata = '[
  6. {"mobile": "145682796533",
  7. "answers" : [{"id" : "104", "is_mcq" : "1", "option" : "voteCount4", "freeText" : ""},
  8. {"id" : "105", "is_mcq" : "1", "option" : "voteCount4", "freeText" : ""},
  9. {"id" : "106", "is_mcq" : "1", "option" : "voteCount4", "freeText" : ""},
  10. {"id" : "109", "is_mcq" : "0", "option" : "", "freeText" : "Test loop 2"}]
  11. }]';
  12.  
  13. $records = json_decode($postdata);
  14.  
  15. $mobile = $records[0]->mobile;
  16.  
  17.  
  18. foreach ($records[0]->answers as $rec) {
  19.  
  20.  
  21. $is_mcq = $rec->{'is_mcq'};
  22.  
  23. if ($is_mcq == 1){
  24. $id = $rec->{'id'};
  25. $ans = $rec->{'option'};
  26.  
  27. $voteData = array();
  28.  
  29. $record = Vote::model()->findByPk($id);
  30.  
  31. if ($record) {
  32. $record->total = ($record->total) + 1;
  33. $record->$ans = ($record->$ans) + 1;
  34. $record->save();
  35. }
  36.  
  37. array_push($voteData, $record);
  38.  
  39.  
  40. $outObj = array('success' => true, 'data' => $voteData, 'info' => 'Ok');
  41. echo CJSON::encode($outObj);
  42.  
  43.  
  44. } elseif($is_mcq == 0){
  45.  
  46. $freeAns = $rec->{'freeText'};
  47. $id = $rec->{'id'};
  48.  
  49. $rec = new VoteFreetoType;
  50. $rec->q_id = $id;
  51. $rec->answer = $freeAns;
  52. $rec->mobile = $mobile;
  53. $rec->date = date("Y-m-d H:i:s");
  54. $rec->save();
  55.  
  56.  
  57. $outObj = array('success' => true, 'info' => 'Ok');
  58. echo CJSON::encode($outObj);
  59. }
  60.  
  61. }
  62.  
  63. }
  64.  
  65. $postdata= json_decode($_REQUEST);
Add Comment
Please, Sign In to add comment