teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

誤記の修正

2018/09/25 08:24

投稿

madoka9393
madoka9393

スコア994

answer CHANGED
@@ -51,7 +51,7 @@
51
51
  $w = $_POST['param1'];
52
52
  foreach($list as $i){
53
53
  if(stripos($i['name_ja'], $w) !== FALSE){ //←ここ
54
- $b[] = $i;
54
+ $b[] = $i['name_ja'];
55
55
  }
56
56
  }
57
57
  echo json_encode($b);

2

誤認識

2018/09/25 08:24

投稿

madoka9393
madoka9393

スコア994

answer CHANGED
@@ -22,55 +22,41 @@
22
22
  何れにせよ`o`の中身を見てみると良いかもです。
23
23
 
24
24
  ######追記
25
- 参考までに当方の方で動作が確認できているソースを記載しておきます。(MySQL、FuelPHP)
25
+ ~~参考までに当方の方で動作が確認できているソースを記載しておきます。(MySQL、FuelPHP)~~
26
26
 
27
+ 見当違いの回答だったっぽいので不要なコードは消し消し。
27
- base.php
28
+ 恐らく以下が問題になっているのではないかと。
28
- ```FuelPHP
29
+ ```PHP
29
- public function get_record(){
30
- $headers = array(
31
- 'Content-type' => 'application/json',
30
+ if(isset($_POST['param1'])){
32
- );
33
-
34
- $connect = Model_Hoge::connection();
35
- try{
36
- $records = DB::select('name')
31
+ $w = $_POST['param1'];
37
- ->from('hoge')
38
- ->where('deleted_at',null)
39
- ->distinct()
40
- ->execute($connect)->as_array();
41
- $result = array_values($records[0]);
42
- }catch (Exception $e){
32
+ foreach($list as $i){
43
- $error = array("error" => "検索に失敗しました");
44
- $data = Format::forge($error)->to_json();
45
- return Response::forge($data, 200, $headers);
33
+ if(stripos($i, $w) !== FALSE){ //←ここ
34
+ $b[] = $i;
35
+ }
46
36
  }
47
- $success = array("success" => $result);
48
- $data = Format::forge($success)->to_json();
49
- return Response::forge($data, 200, $headers);
37
+ echo json_encode($b);
50
- }
38
+ }
39
+ else{
40
+ echo json_encode($b);
41
+ }
51
42
  ```
43
+
52
- html
44
+ `$list`から取り出した要素`$i`は`array`なので、
45
+ ここで`$w`(=`$_POST['param1']`)との比較に失敗してしまっていると考えられます。
46
+
47
+ コメントにも書きましたが以下のように変更すれば解消できるのではないでしょうか。
48
+
53
- ```jQuery
49
+ ```PHP
54
- $("#list").autocomplete({
50
+ if(isset($_POST['param1'])){
55
- source: function(req, resp){
56
- $.ajax({
57
- url: "<?php echo URI::base();?>/base/record/",
58
- dataType: "json",
51
+ $w = $_POST['param1'];
59
- success: function (res){
52
+ foreach($list as $i){
60
- if(!res.error){
61
- let result = res.success;
62
- resp(result);
63
- }
64
- },
65
- error: function(xhr, textStatus, errorThrown) {
53
+ if(stripos($i['name_ja'], $w) !== FALSE){ //←ここ
66
- res([]);
54
+ $b[] = $i;
67
55
  }
68
- });
69
- },
56
+ }
70
- delay: 500,
71
- minLength:0,
72
- autoFocus: false
73
- }).focus(function(){
57
+ echo json_encode($b);
74
- $(this).autocomplete("search", "");
75
- });
58
+ }
59
+ else{
60
+ echo json_encode($b);
61
+ }
76
62
  ```

1

追記

2018/09/25 08:12

投稿

madoka9393
madoka9393

スコア994

answer CHANGED
@@ -19,4 +19,58 @@
19
19
  ~後略
20
20
  ```
21
21
 
22
- 何れにせよ`o`の中身を見てみると良いかもです。
22
+ 何れにせよ`o`の中身を見てみると良いかもです。
23
+
24
+ ######追記
25
+ 参考までに当方の方で動作が確認できているソースを記載しておきます。(MySQL、FuelPHP)
26
+
27
+ base.php
28
+ ```FuelPHP
29
+ public function get_record(){
30
+ $headers = array(
31
+ 'Content-type' => 'application/json',
32
+ );
33
+
34
+ $connect = Model_Hoge::connection();
35
+ try{
36
+ $records = DB::select('name')
37
+ ->from('hoge')
38
+ ->where('deleted_at',null)
39
+ ->distinct()
40
+ ->execute($connect)->as_array();
41
+ $result = array_values($records[0]);
42
+ }catch (Exception $e){
43
+ $error = array("error" => "検索に失敗しました");
44
+ $data = Format::forge($error)->to_json();
45
+ return Response::forge($data, 200, $headers);
46
+ }
47
+ $success = array("success" => $result);
48
+ $data = Format::forge($success)->to_json();
49
+ return Response::forge($data, 200, $headers);
50
+ }
51
+ ```
52
+ html
53
+ ```jQuery
54
+ $("#list").autocomplete({
55
+ source: function(req, resp){
56
+ $.ajax({
57
+ url: "<?php echo URI::base();?>/base/record/",
58
+ dataType: "json",
59
+ success: function (res){
60
+ if(!res.error){
61
+ let result = res.success;
62
+ resp(result);
63
+ }
64
+ },
65
+ error: function(xhr, textStatus, errorThrown) {
66
+ res([]);
67
+ }
68
+ });
69
+ },
70
+ delay: 500,
71
+ minLength:0,
72
+ autoFocus: false
73
+ }).focus(function(){
74
+ $(this).autocomplete("search", "");
75
+ });
76
+ ```