質問編集履歴
1
ソースを見やすいよう記述しなおしました
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,6 +15,7 @@
|
|
15
15
|
MySQL:5.5
|
16
16
|
|
17
17
|
【function.php】
|
18
|
+
```lang-php
|
18
19
|
// DBに登録されている値を取得
|
19
20
|
function getColums($table) {
|
20
21
|
$dbh = connectDb();
|
@@ -29,12 +30,84 @@
|
|
29
30
|
// var_dump($colum);
|
30
31
|
// }
|
31
32
|
}
|
33
|
+
```
|
32
34
|
|
33
35
|
【index.php】
|
36
|
+
```lang-php
|
34
37
|
<!-- このforeachでエラーが出力されます -->
|
35
38
|
<?php foreach ($columns as $column) : ?>
|
36
39
|
<? var_dump($column); ?>
|
37
40
|
<input type="radio" name="lens_name" value="<?php echo h($column['lens_name']); ?>" id="idnumber<?php echo h($idnumber); ?>">
|
38
41
|
<label for="idnumber<?php echo h($idnumber); ?>"><?php echo h($column['lens_name']); ?></label>
|
39
42
|
<?php $idnumber++; ?>
|
40
|
-
<?php endforeach; ?>
|
43
|
+
<?php endforeach; ?>
|
44
|
+
```
|
45
|
+
|
46
|
+
・追記
|
47
|
+
【test.php】を作成し、1つのファイルにまとめました。
|
48
|
+
|
49
|
+
```lang-php
|
50
|
+
|
51
|
+
<?php
|
52
|
+
ini_set('display_errors', 1);
|
53
|
+
|
54
|
+
require 'config.php';
|
55
|
+
|
56
|
+
// DBコネクト
|
57
|
+
function connectDb() {
|
58
|
+
try {
|
59
|
+
return new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD);
|
60
|
+
} catch (PDOException $e) {
|
61
|
+
echo $e->getMessage();
|
62
|
+
exit;
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
// クロスサイトスクリプティング対策
|
67
|
+
function h($s) {
|
68
|
+
return htmlspecialchars($s, ENT_QUOTES, "UTF-8");
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
// DBに登録されている値を取得
|
73
|
+
function getColums($table) {
|
74
|
+
$dbh = connectDb();
|
75
|
+
$columns = array();
|
76
|
+
$sql = "select * from $table order by created desc";
|
77
|
+
foreach ($dbh->query($sql) as $row) {
|
78
|
+
$columns[] = $row;
|
79
|
+
}
|
80
|
+
|
81
|
+
//出力される
|
82
|
+
var_dump($columns);
|
83
|
+
}
|
84
|
+
?>
|
85
|
+
|
86
|
+
<!DOCTYPE html>
|
87
|
+
<html lang="ja">
|
88
|
+
<head>
|
89
|
+
<meta charset="utf-8">
|
90
|
+
<title>Image Uploader</title>
|
91
|
+
</head>
|
92
|
+
<body>
|
93
|
+
<form action="" method="post" enctype="multipart/form-data">
|
94
|
+
<ul>
|
95
|
+
<li>
|
96
|
+
<?php
|
97
|
+
getColums("post");
|
98
|
+
$idnumber = 1;
|
99
|
+
// 出力されない
|
100
|
+
var_dump($columns);
|
101
|
+
?>
|
102
|
+
<?php foreach ($columns as $column) : ?>
|
103
|
+
<input type="radio" name="lens_name" value="<?php echo h($column['lens_name']); ?>" id="idnumber<?php echo h($idnumber); ?>">
|
104
|
+
<label for="idnumber<?php echo h($idnumber); ?>"><?php echo h($column['lens_name']); ?></label>
|
105
|
+
<?php $idnumber++; ?>
|
106
|
+
<?php endforeach; ?>
|
107
|
+
</li>
|
108
|
+
</ul>
|
109
|
+
</form>
|
110
|
+
|
111
|
+
</body>
|
112
|
+
</html>
|
113
|
+
```
|