質問編集履歴

4

プログラムを修正しました。

2016/10/05 16:25

投稿

usuallyi
usuallyi

スコア41

test CHANGED
File without changes
test CHANGED
@@ -96,15 +96,15 @@
96
96
 
97
97
 
98
98
 
99
- debug($data=$this->User->find('all',array('conditions' => array('or' => array( array("User.name like '%$searchname%'"),array("User.username like '%$searchname%'"))))));
99
+ $data=$this->User->find('all',array('conditions' => array('or' => array( array("User.name like '%$searchname%'"),array("User.username like '%$searchname%'")))));
100
-
101
-
102
-
100
+
101
+
102
+
103
- debug($follows = $this->Follow->find('all',array('conditions' => array('follow' => AuthComponent::user('username')))));
103
+ $follows = $this->Follow->find('all',array('conditions' => array('Follow.follow' => AuthComponent::user('username'))));
104
-
105
-
106
-
104
+
105
+
106
+
107
- debug($da = $this->User->find('all',array('conditions' => array('User.username'=>$follows['Follow']['follow']))));
107
+ //$da = $this->User->find('all',array('conditions' => array('User.username'=>$follows['follow'])));
108
108
 
109
109
 
110
110
 
@@ -134,9 +134,9 @@
134
134
 
135
135
 
136
136
 
137
- else if(debug($this->Follow->save($this->request->data['Follow']))){
137
+ else if($this->Follow->save($this->request->data['Follow'])){
138
-
138
+
139
- $this->Comment->delete($this->request->data['Follow']);
139
+ //$this->requset->deleteAll($data['Follow']);
140
140
 
141
141
  $this->redirect(array('action'=>'find', $searchname));}
142
142
 
@@ -192,11 +192,11 @@
192
192
 
193
193
  if(empty($data)):
194
194
 
195
- echo print(h("データが見つかりませんでした。"));
195
+ echo print(h("データが見つかりませんでした。"));
196
196
 
197
197
  else:
198
198
 
199
- echo($data['User']['username']);
199
+ echo $this->HTML->link($data['User']['username'],array('action'=>'mypage'));
200
200
 
201
201
  echo("<br>");
202
202
 
@@ -208,21 +208,29 @@
208
208
 
209
209
  echo("<br>");
210
210
 
211
+ //if($follows !=NULL):
212
+
211
213
  $count = 0;
212
214
 
213
- //if($follows !=NULL):
214
-
215
215
  foreach ($follows as $searchcheck){
216
216
 
217
+
218
+
217
- if(($searchcheck['Follow']['follower']==$data['User']['username']) || ($data['User']['username']== $username['username'])):
219
+ if(($searchcheck['Follow']['follower']==$data['User']['username'] || ($data['User']['username'] == $username['username']))):
220
+
218
-
221
+ $count++;
219
-
220
222
 
221
223
  //if(($searchcheck['Follow']['follower']==$data['User']['username'])):
222
224
 
225
+ else:
226
+
227
+ //if($data['User']['username']==$searchcheck['Follow']['follow']):
228
+
223
229
 
224
230
 
225
- else:
231
+ endif;
232
+
233
+
226
234
 
227
235
  if($count == 0):
228
236
 
@@ -236,9 +244,7 @@
236
244
 
237
245
  $count++;
238
246
 
239
- endif;
247
+ endif;
240
-
241
- endif;
242
248
 
243
249
  }
244
250
 

3

問題点について具体的に追加

2016/10/05 16:25

投稿

usuallyi
usuallyi

スコア41

test CHANGED
File without changes
test CHANGED
@@ -46,7 +46,41 @@
46
46
 
47
47
 
48
48
 
49
+ cakephp2.×を仕様しています。
50
+
51
+
52
+
53
+ 理想は
54
+
55
+ viewで$data['User']['username']を値として渡せば、
56
+
57
+ わかる関数を作成し、帰ってきた値で、
58
+
59
+ このユーザーはfollowしているか、していないか。分かるのが一番理想です。
60
+
61
+
62
+
63
+ 現在のプログラムだと、
64
+
65
+ foreach文で、以下のプログラムで
66
+
67
+ followボタンを表示させています。
68
+
69
+
70
+
49
- 見やすい様に、しました。
71
+ その後、コントローラ
72
+
73
+ postされたら、データベースに保存し、
74
+
75
+ findにリダイレクトで返すプログラムにしているのですが、
76
+
77
+ 下記のプログラムが、リダイレクトした後も、saveし続けており、
78
+
79
+ $this->Follow->save($this->request->data['Follow'])
80
+
81
+ データベースには登録しているものの、
82
+
83
+ 確認してみてもfollowボタンが表示されているという状態です。
50
84
 
51
85
 
52
86
 

2

コードを見やすい様に修正

2016/10/05 12:38

投稿

usuallyi
usuallyi

スコア41

test CHANGED
File without changes
test CHANGED
@@ -44,7 +44,15 @@
44
44
 
45
45
  よろしくお願いいたします。
46
46
 
47
+
48
+
49
+ 見やすい様に、しました。
50
+
51
+
52
+
47
- --コントローラ--
53
+ ---contoroller---
54
+
55
+ ```cakephp
48
56
 
49
57
  public function find($searchname = NULL){
50
58
 
@@ -104,13 +112,17 @@
104
112
 
105
113
  }
106
114
 
107
-
115
+ ```
108
-
109
-
110
-
111
-
112
-
116
+
117
+
118
+
119
+
120
+
113
- --view--
121
+ --view find.ctp--
122
+
123
+ ```cakephp
124
+
125
+ <?php echo $searchname; ?>
114
126
 
115
127
  <?php if($searchname==NULL):?>
116
128
 
@@ -203,3 +215,5 @@
203
215
  }
204
216
 
205
217
  ?>
218
+
219
+ ```

1

コード追加

2016/10/05 09:31

投稿

usuallyi
usuallyi

スコア41

test CHANGED
File without changes
test CHANGED
@@ -33,3 +33,173 @@
33
33
  ユーザーネームと一番新しいtweetを表示しており、
34
34
 
35
35
  そこに、followボタンを設置している状況です。
36
+
37
+
38
+
39
+ 追記しました。
40
+
41
+ かなり、色々試したので、
42
+
43
+ コードがひどいと思いますが、
44
+
45
+ よろしくお願いいたします。
46
+
47
+ --コントローラ--
48
+
49
+ public function find($searchname = NULL){
50
+
51
+ $this->loadModel('Tweet');
52
+
53
+ $this->loadModel('Follow');
54
+
55
+
56
+
57
+ debug($data=$this->User->find('all',array('conditions' => array('or' => array( array("User.name like '%$searchname%'"),array("User.username like '%$searchname%'"))))));
58
+
59
+
60
+
61
+ debug($follows = $this->Follow->find('all',array('conditions' => array('follow' => AuthComponent::user('username')))));
62
+
63
+
64
+
65
+ debug($da = $this->User->find('all',array('conditions' => array('User.username'=>$follows['Follow']['follow']))));
66
+
67
+
68
+
69
+ if(empty($data)){
70
+
71
+ $this->Session->setFlash('対象のユーザーは見つかりません。');
72
+
73
+ }
74
+
75
+ $this->set('searchname', $searchname);
76
+
77
+ $this->set('username',$this->Auth->user());
78
+
79
+ $this->set('userdata',$data);
80
+
81
+ $this->set('follows',$follows);
82
+
83
+
84
+
85
+ if($this->request->is('post')){
86
+
87
+ if(isset($this->data['Search']['title'])){
88
+
89
+ $search = $this->request->data['Search']['title'];
90
+
91
+ $this->redirect(array('action'=>'find', $search));}
92
+
93
+
94
+
95
+ else if(debug($this->Follow->save($this->request->data['Follow']))){
96
+
97
+ $this->Comment->delete($this->request->data['Follow']);
98
+
99
+ $this->redirect(array('action'=>'find', $searchname));}
100
+
101
+
102
+
103
+ }
104
+
105
+ }
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+ --view--
114
+
115
+ <?php if($searchname==NULL):?>
116
+
117
+ 検索結果
118
+
119
+ <?php else: ?>
120
+
121
+ の検索結果
122
+
123
+ <?php endif; ?>
124
+
125
+ <br/>
126
+
127
+ <?php
128
+
129
+ echo $this->Form->create('Search');
130
+
131
+ echo $this->Form->input('title',array('label'=>''));
132
+
133
+ echo $this->Form->end('検索');
134
+
135
+ ?>
136
+
137
+
138
+
139
+ ユーザー名や名前で検索<br />
140
+
141
+ <?php echo $this->Session->flash(); ?>
142
+
143
+ <?php
144
+
145
+ foreach ($userdata as $data){
146
+
147
+ if(empty($data)):
148
+
149
+ echo print(h("データが見つかりませんでした。"));
150
+
151
+ else:
152
+
153
+ echo($data['User']['username']);
154
+
155
+ echo("<br>");
156
+
157
+ echo nl2br($data['joinTweet'][0]['tweet']);
158
+
159
+ echo("<br>");
160
+
161
+ echo($data['joinTweet'][0]['tweettime']);
162
+
163
+ echo("<br>");
164
+
165
+ $count = 0;
166
+
167
+ //if($follows !=NULL):
168
+
169
+ foreach ($follows as $searchcheck){
170
+
171
+ if(($searchcheck['Follow']['follower']==$data['User']['username']) || ($data['User']['username']== $username['username'])):
172
+
173
+
174
+
175
+ //if(($searchcheck['Follow']['follower']==$data['User']['username'])):
176
+
177
+
178
+
179
+ else:
180
+
181
+ if($count == 0):
182
+
183
+ echo $this->Form->create('Follow');
184
+
185
+ echo $this->Form->hidden('follow',array('value'=>$username['username']));
186
+
187
+ echo $this->Form->hidden('follower',array('value'=>$data['User']['username']));
188
+
189
+ echo $this->Form->end('follow');
190
+
191
+ $count++;
192
+
193
+ endif;
194
+
195
+ endif;
196
+
197
+ }
198
+
199
+ echo("<br>");
200
+
201
+ endif;
202
+
203
+ }
204
+
205
+ ?>