質問編集履歴

1

ソースを見やすいよう記述しなおしました

2015/11/12 06:12

投稿

junkboy
junkboy

スコア45

test CHANGED
File without changes
test CHANGED
@@ -32,6 +32,8 @@
32
32
 
33
33
  【function.php】
34
34
 
35
+ ```lang-php
36
+
35
37
  // DBに登録されている値を取得
36
38
 
37
39
  function getColums($table) {
@@ -60,10 +62,14 @@
60
62
 
61
63
  }
62
64
 
65
+ ```
66
+
63
67
 
64
68
 
65
69
  【index.php】
66
70
 
71
+ ```lang-php
72
+
67
73
  <!-- このforeachでエラーが出力されます -->
68
74
 
69
75
  <?php foreach ($columns as $column) : ?>
@@ -77,3 +83,143 @@
77
83
  <?php $idnumber++; ?>
78
84
 
79
85
  <?php endforeach; ?>
86
+
87
+ ```
88
+
89
+
90
+
91
+ ・追記
92
+
93
+ 【test.php】を作成し、1つのファイルにまとめました。
94
+
95
+
96
+
97
+ ```lang-php
98
+
99
+
100
+
101
+ <?php
102
+
103
+ ini_set('display_errors', 1);
104
+
105
+
106
+
107
+ require 'config.php';
108
+
109
+
110
+
111
+ // DBコネクト
112
+
113
+ function connectDb() {
114
+
115
+ try {
116
+
117
+ return new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD);
118
+
119
+ } catch (PDOException $e) {
120
+
121
+ echo $e->getMessage();
122
+
123
+ exit;
124
+
125
+ }
126
+
127
+ }
128
+
129
+
130
+
131
+ // クロスサイトスクリプティング対策
132
+
133
+ function h($s) {
134
+
135
+ return htmlspecialchars($s, ENT_QUOTES, "UTF-8");
136
+
137
+ }
138
+
139
+
140
+
141
+
142
+
143
+ // DBに登録されている値を取得
144
+
145
+ function getColums($table) {
146
+
147
+ $dbh = connectDb();
148
+
149
+ $columns = array();
150
+
151
+ $sql = "select * from $table order by created desc";
152
+
153
+ foreach ($dbh->query($sql) as $row) {
154
+
155
+ $columns[] = $row;
156
+
157
+ }
158
+
159
+
160
+
161
+ //出力される
162
+
163
+ var_dump($columns);
164
+
165
+ }
166
+
167
+ ?>
168
+
169
+
170
+
171
+ <!DOCTYPE html>
172
+
173
+ <html lang="ja">
174
+
175
+ <head>
176
+
177
+ <meta charset="utf-8">
178
+
179
+ <title>Image Uploader</title>
180
+
181
+ </head>
182
+
183
+ <body>
184
+
185
+ <form action="" method="post" enctype="multipart/form-data">
186
+
187
+ <ul>
188
+
189
+ <li>
190
+
191
+ <?php
192
+
193
+ getColums("post");
194
+
195
+ $idnumber = 1;
196
+
197
+ // 出力されない
198
+
199
+ var_dump($columns);
200
+
201
+ ?>
202
+
203
+ <?php foreach ($columns as $column) : ?>
204
+
205
+ <input type="radio" name="lens_name" value="<?php echo h($column['lens_name']); ?>" id="idnumber<?php echo h($idnumber); ?>">
206
+
207
+ <label for="idnumber<?php echo h($idnumber); ?>"><?php echo h($column['lens_name']); ?></label>
208
+
209
+ <?php $idnumber++; ?>
210
+
211
+ <?php endforeach; ?>
212
+
213
+ </li>
214
+
215
+ </ul>
216
+
217
+ </form>
218
+
219
+
220
+
221
+ </body>
222
+
223
+ </html>
224
+
225
+ ```