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

質問編集履歴

5

修正

2019/03/22 04:21

投稿

ariiiiiga
ariiiiiga

スコア66

title CHANGED
File without changes
body CHANGED
@@ -54,34 +54,39 @@
54
54
  $whereSql = " where ".implode(' OR ', $where);
55
55
  }
56
56
 
57
- // 件数取得
58
- $sql = $pdo->prepare('select COUNT(*) AS "TOTAL" from hosts '.$whereSql);
59
-
60
57
  // 表示用
61
- $sql = $pdo->prepare('select * from hosts '.$whereSql.' order by post_id desc limit :limit offset :offset');
58
+ $sql = $pdo->prepare('select * from hosts '.$whereSql.' order by post_id desc limit :limit offset :offset');
62
59
 
63
- $sql->bindValue(":offset", $offset, PDO::PARAM_INT);
60
+ $sql->bindValue(":offset", $offset, PDO::PARAM_INT);
64
- $sql->bindValue(":limit", PAGE_LIMIT, PDO::PARAM_INT);
61
+ $sql->bindValue(":limit", PAGE_LIMIT, PDO::PARAM_INT);
65
- if ( $name ) {
62
+ if ( $name ) {
66
- $sql->bindValue(":name", "%" . addcslashes($name, '\_%') . "%", PDO::PARAM_STR);
63
+ $sql->bindValue(":name", "%" . addcslashes($name, '\_%') . "%", PDO::PARAM_STR);
67
- }
64
+ }
68
- if ( $shop ) {
65
+ if ( $shop ) {
69
- $sql->bindValue(":shop", "%" . addcslashes($shop, '\_%') . "%", PDO::PARAM_STR);
66
+ $sql->bindValue(":shop", "%" . addcslashes($shop, '\_%') . "%", PDO::PARAM_STR);
70
- }
67
+ }
71
- if ( $user_id ) {
68
+ if ( $user_id ) {
72
- $sql->bindValue(":user_id", addcslashes($user_id, '\_%') , PDO::PARAM_STR);
69
+ $sql->bindValue(":user_id", addcslashes($user_id, '\_%') , PDO::PARAM_STR);
73
- }
70
+ }
74
- $sql->execute();
71
+ $sql->execute();
75
72
 
76
- // 検索結果取得
73
+ // 検索結果取得
77
- $result = $sql->fetchAll();
74
+ $result = $sql->fetchAll();
78
- // Total件数
79
75
 
80
- $all = array();
76
+ // 件数取得
77
+ $totalRow = $pdo->query('select COUNT(*) AS "TOTAL" from hosts '.$whereSql);
81
- if ( $result ) {
78
+ if ( $name ) {
82
- $all['TOTAL'] = $result[0]['TOTAL'];
79
+ $totalRow->bindValue(":name", "%" . addcslashes($name, '\_%') . "%", PDO::PARAM_STR);
83
- }
80
+ }
81
+ if ( $shop ) {
82
+ $totalRow->bindValue(":shop", "%" . addcslashes($shop, '\_%') . "%", PDO::PARAM_STR);
83
+ }
84
+ if ( $user_id ) {
85
+ $totalRow->bindValue(":user_id", addcslashes($user_id, '\_%') , PDO::PARAM_STR);
86
+ }
84
87
 
88
+ $totalRow->execute();
89
+ $all = $totalRow->fetch(PDO::FETCH_ASSOC);
85
90
  ?>
86
91
  <!DOCTYPE HTML>
87
92
  <html lang="ja">

4

修正

2019/03/22 04:21

投稿

ariiiiiga
ariiiiiga

スコア66

title CHANGED
File without changes
body CHANGED
@@ -19,7 +19,7 @@
19
19
  // 取得件数
20
20
  define('PAGE_LIMIT', 10);
21
21
 
22
- $pdo = new PDO ( "pgsql:host=myhost;port=5432;dbname=mydb;user=myuser;password=*****" );
22
+ $pdo = new PDO ( "pgsql:host=myhost;port=5432;dbname=mydb;user=myuser;password=*****" );
23
23
  //カラム名を小文字にする
24
24
  $pdo ->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
25
25
  // SQL実行に失敗した場合、例外を発生させる

3

変更

2019/03/20 06:42

投稿

ariiiiiga
ariiiiiga

スコア66

title CHANGED
File without changes
body CHANGED
@@ -19,7 +19,7 @@
19
19
  // 取得件数
20
20
  define('PAGE_LIMIT', 10);
21
21
 
22
- $pdo = new PDO ( "pgsql:host=ec2-50-19-109-120.compute-1.amazonaws.com;port=5432;dbname=d59sri1j1hlv0b;user=myeojtgygingbl;password=a3a14973f16546823bd88fb405ae36b0143db905ba9598c07acc9eeb47c1e4cb" );
22
+ $pdo = new PDO ( "pgsql:host=mmyhost;port=5432;dbname=mydb;user=myuser;password=*****" );
23
23
  //カラム名を小文字にする
24
24
  $pdo ->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
25
25
  // SQL実行に失敗した場合、例外を発生させる

2

PHP,SQ文の変更

2019/03/20 06:40

投稿

ariiiiiga
ariiiiiga

スコア66

title CHANGED
File without changes
body CHANGED
@@ -14,16 +14,15 @@
14
14
 
15
15
  ```ここに言語名を入力
16
16
  <?php
17
+ ini_set( 'display_errors', 1 );
18
+ ini_set( 'error_reporting', E_ALL );
19
+ // 取得件数
20
+ define('PAGE_LIMIT', 10);
17
21
 
18
- ini_set( 'display_errors', 1 );
19
- ini_set( 'error_reporting', E_ALL );
20
- // 取得件数
21
- define('PAGE_LIMIT', 10);
22
-
23
-
24
- $pdo = new PDO ( "pgsql:host=localhost;port=5432;dbname=,mydb;user=name;password=*****" );
25
-
22
+ $pdo = new PDO ( "pgsql:host=ec2-50-19-109-120.compute-1.amazonaws.com;port=5432;dbname=d59sri1j1hlv0b;user=myeojtgygingbl;password=a3a14973f16546823bd88fb405ae36b0143db905ba9598c07acc9eeb47c1e4cb" );
23
+ //カラム名を小文字にする
26
24
  $pdo ->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
25
+ // SQL実行に失敗した場合、例外を発生させる
27
26
  $pdo->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
28
27
  $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
29
28
 
@@ -54,13 +53,13 @@
54
53
  if ( count($where) > 0 ) {
55
54
  $whereSql = " where ".implode(' OR ', $where);
56
55
  }
57
-          
58
-      // 件数取得
59
- $sql = $pdo->prepare('select COUNT(*) AS "TOTAL" from hosts '.$whereSql);
60
56
 
61
- // 表示用
57
+ // 件数取得
62
- $sql = $pdo->prepare('select * from hosts '.$whereSql.' order by post_id desc limit :limit offset :offset');
58
+ $sql = $pdo->prepare('select COUNT(*) AS "TOTAL" from hosts '.$whereSql);
63
59
 
60
+ // 表示用
61
+ $sql = $pdo->prepare('select * from hosts '.$whereSql.' order by post_id desc limit :limit offset :offset');
62
+
64
63
  $sql->bindValue(":offset", $offset, PDO::PARAM_INT);
65
64
  $sql->bindValue(":limit", PAGE_LIMIT, PDO::PARAM_INT);
66
65
  if ( $name ) {
@@ -70,18 +69,19 @@
70
69
  $sql->bindValue(":shop", "%" . addcslashes($shop, '\_%') . "%", PDO::PARAM_STR);
71
70
  }
72
71
  if ( $user_id ) {
73
- $sql->bindValue(":user_id", addcslashes($user_id, '\_%') , PDO::PARAM_STR);
72
+ $sql->bindValue(":user_id", addcslashes($user_id, '\_%') , PDO::PARAM_STR);
74
73
  }
75
74
  $sql->execute();
76
75
 
77
76
  // 検索結果取得
78
77
  $result = $sql->fetchAll();
79
-
80
- $all = array();
81
- if ( $result ) {
78
+ // Total件数
82
- $all['TOTAL'] = $result[0]['TOTAL'];
83
- }
84
79
 
80
+ $all = array();
81
+ if ( $result ) {
82
+ $all['TOTAL'] = $result[0]['TOTAL'];
83
+ }
84
+
85
85
  ?>
86
86
  <!DOCTYPE HTML>
87
87
  <html lang="ja">
@@ -89,14 +89,39 @@
89
89
  <meta charset="UTF-8">
90
90
  <meta http-equiv="Content-Style-Type" content="text/css">
91
91
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
92
+ <link rel="stylesheet" as="style" href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css" media="all" onload="this.rel='stylesheet'">
93
+ <link rel="stylesheet" as="style" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css" media="all" onload="this.rel='stylesheet'">
92
94
  </head>
93
95
  <body>
94
96
 
97
+ <div class="make">
98
+ <style type="text/css">
99
+ .make{
100
+ text-align: center;
101
+ font-size:15px;
102
+ font-weight:bold;
103
+ font-size:20px;
104
+ }
105
+ input{
106
+ padding:10px 10px ;
107
+ border-radius:30px;
108
+ margin:10px 0;
109
+ }
110
+ </style>
111
+ <input type="button" onclick="location.href='user_register.php'"value="MyIDを発行・確認">
112
+ <input type="button" onclick="location.href='post_new_review.php'"value="NEWLOGを書く">
113
+ </div>
114
+
115
+ <style type="text/css">
116
+ h3 {
117
+ text-align: center;
118
+ }
119
+ </style>
95
120
  <h3>検索フォーム</h3>
96
121
  <div class="col-xs-12 well">
97
122
  <form method="get">
98
123
  <div class="form-group ">
99
- <label for="InputName">名</label>
124
+ <label for="InputName">ホスト名</label>
100
125
  <input name="name" class="form-control" id="InputName" value="<?= $name ?>">
101
126
  <label for="InputShop">店舗名</label>
102
127
  <input name="shop" class="form-control" id="InputShop" value="<?= $shop ?>">
@@ -116,9 +141,9 @@
116
141
  <thead>
117
142
  <tr>
118
143
  <th>名前</th>
119
-  <th>店舗名</th>
144
+ <th>店舗名</th>
120
-  <th>評価</th>
145
+ <th>評価</th>
121
-    <th>コメント</th>
146
+ <th>コメント</th>
122
147
  </tr>
123
148
  </thead>
124
149
  <tbody>
@@ -138,8 +163,19 @@
138
163
  <?php endforeach; ?>
139
164
  </tbody>
140
165
  </table>
141
-
166
+ <!-- ページネーション -->
167
+ <?php if ( $p > 0 ) : ?>
168
+ <a href="index.php?search=1&name=<?= $name ?>&shop=<?= $shop ?>&user_id=<?= $user_id ?>&p=<?= $p-1 ?>" class="pull-left">戻る</a>
169
+ <?php else : ?>
170
+ <span class="pull-left">最新</span>
171
+ <?php endif; ?>
142
172
 
173
+ <?php if ( $all['TOTAL'] - (PAGE_LIMIT * $p ) > PAGE_LIMIT ) : ?>
174
+ <a href="index.php?search=1&name=<?= $name ?>&shop=<?= $shop ?>&user_id=<?= $user_id ?>&p=<?= $p+1 ?>" class="pull-right">次へ</a>
175
+ <?php else : ?>
176
+ <span class="pull-right">末尾</span>
177
+ <?php endif; ?>
178
+
143
179
  <?php else : ?>
144
180
  <p class="alert alert-danger">検索対象は見つかりませんでした。</p>
145
181
  <?php endif; ?>
@@ -149,6 +185,7 @@
149
185
  </body>
150
186
  </html>
151
187
 
188
+
152
189
  ```
153
190
 
154
191
  ### 試したこと

1

PHP,SQL部分の変更

2019/03/20 06:38

投稿

ariiiiiga
ariiiiiga

スコア66

title CHANGED
File without changes
body CHANGED
@@ -15,10 +15,10 @@
15
15
  ```ここに言語名を入力
16
16
  <?php
17
17
 
18
- ini_set( 'display_errors', 1 );
18
+ ini_set( 'display_errors', 1 );
19
- ini_set( 'error_reporting', E_ALL );
19
+ ini_set( 'error_reporting', E_ALL );
20
- // 取得件数
20
+ // 取得件数
21
- define('PAGE_LIMIT', 10);
21
+ define('PAGE_LIMIT', 10);
22
22
 
23
23
 
24
24
  $pdo = new PDO ( "pgsql:host=localhost;port=5432;dbname=,mydb;user=name;password=*****" );
@@ -42,7 +42,7 @@
42
42
  $whereSql = null;
43
43
 
44
44
  if ( $name ) {
45
- $where[] = "name like :name";
45
+ $where[] = "name like :name";
46
46
  }
47
47
  if ( $shop ) {
48
48
  $where[] = "shop like :shop";
@@ -55,9 +55,12 @@
55
55
  $whereSql = " where ".implode(' OR ', $where);
56
56
  }
57
57
           
58
-      //Postgres用に変更した
58
+      // 件数取得
59
- $sql = $pdo->prepare('select *, COUNT(*) OVER() AS "TOTAL" from hosts '.$whereSql.' order by post_id desc limit :limit offset :offset');
59
+ $sql = $pdo->prepare('select COUNT(*) AS "TOTAL" from hosts '.$whereSql);
60
60
 
61
+ // 表示用
62
+ $sql = $pdo->prepare('select * from hosts '.$whereSql.' order by post_id desc limit :limit offset :offset');
63
+
61
64
  $sql->bindValue(":offset", $offset, PDO::PARAM_INT);
62
65
  $sql->bindValue(":limit", PAGE_LIMIT, PDO::PARAM_INT);
63
66
  if ( $name ) {
@@ -71,10 +74,13 @@
71
74
  }
72
75
  $sql->execute();
73
76
 
74
- //Postgres用に変更した
77
+ // 検索結果取得
75
78
  $result = $sql->fetchAll();
79
+
80
+ $all = array();
76
- //Postgres用に変更した
81
+ if ( $result ) {
77
- $all = $sql->fetch(PDO::FETCH_ASSOC);
82
+ $all['TOTAL'] = $result[0]['TOTAL'];
83
+ }
78
84
 
79
85
  ?>
80
86
  <!DOCTYPE HTML>