Array naar lijst
Ik heb een script gemaakt die waarden uit een SQL database haalt en deze in een array plaatst.
Me array ziet er als volgt uit:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
Array
(
[0] => Array
(
[serie] => Fringe
[season] => Season 5
[episode] => Fringe S05E02.mkv
)
[1] => Array
(
[serie] => Last Resort
[season] => Season 2
[episode] => Last Resort S02E01.mkv
)
[2] => Array
(
[serie] => Go on
[season] => Season 1
[episode] => Go on S01E01.mp4
)
[3] => Array
(
[serie] => Go on
[season] => Season 1
[episode] => Go on S01E02.mp4
)
[4] => Array
(
[serie] => Go on
[season] => Season 1
[episode] => Go on S01E03.mp4
)
[5] => Array
(
[serie] => Go on
[season] => Season 1
[episode] => Go on S01E04.mp4
)
[6] => Array
(
[serie] => Go on
[season] => Season 1
[episode] => Go on S01E05.mp4
)
[7] => Array
(
[serie] => Last Resort
[season] => Season 1
[episode] => Last Resort S01E01.mp4
)
[8] => Array
(
[serie] => Last Resort
[season] => Season 1
[episode] => Last Resort S01E02.mp4
)
[9] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E01.avi
)
[10] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E02.avi
)
[11] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E03.avi
)
[12] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E04.avi
)
[13] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E05.avi
)
[14] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E06.avi
)
[15] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E07.avi
)
[16] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E08.avi
)
[17] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E08.srt
)
[18] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E09.avi
)
[19] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E10.avi
)
[20] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E10.srt
)
[21] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E11.avi
)
[22] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E11.srt
)
[23] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E12.avi
)
[24] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E12.srt
)
)
(
[0] => Array
(
[serie] => Fringe
[season] => Season 5
[episode] => Fringe S05E02.mkv
)
[1] => Array
(
[serie] => Last Resort
[season] => Season 2
[episode] => Last Resort S02E01.mkv
)
[2] => Array
(
[serie] => Go on
[season] => Season 1
[episode] => Go on S01E01.mp4
)
[3] => Array
(
[serie] => Go on
[season] => Season 1
[episode] => Go on S01E02.mp4
)
[4] => Array
(
[serie] => Go on
[season] => Season 1
[episode] => Go on S01E03.mp4
)
[5] => Array
(
[serie] => Go on
[season] => Season 1
[episode] => Go on S01E04.mp4
)
[6] => Array
(
[serie] => Go on
[season] => Season 1
[episode] => Go on S01E05.mp4
)
[7] => Array
(
[serie] => Last Resort
[season] => Season 1
[episode] => Last Resort S01E01.mp4
)
[8] => Array
(
[serie] => Last Resort
[season] => Season 1
[episode] => Last Resort S01E02.mp4
)
[9] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E01.avi
)
[10] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E02.avi
)
[11] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E03.avi
)
[12] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E04.avi
)
[13] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E05.avi
)
[14] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E06.avi
)
[15] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E07.avi
)
[16] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E08.avi
)
[17] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E08.srt
)
[18] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E09.avi
)
[19] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E10.avi
)
[20] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E10.srt
)
[21] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E11.avi
)
[22] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E11.srt
)
[23] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E12.avi
)
[24] => Array
(
[serie] => Leverage
[season] => Season 1
[episode] => Leverage S01E12.srt
)
)
Nu lukt het mij enkel niet om de waarde op de juiste manier weer te geven op de pagina.
Wat ik graag wil is:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
* Serie1
* Season 1
* Aflevering 1
* Aflevering 2
* Aflevering 3
* Season 2
* Aflevering 1
* Aflevering 2
* Aflevering 3
* Serie2
* Season 1
* Aflevering 1
* Aflevering 2
* Aflevering 3
* Season 2
* Aflevering 1
* Aflevering 2
* Aflevering 3
* Season 1
* Aflevering 1
* Aflevering 2
* Aflevering 3
* Season 2
* Aflevering 1
* Aflevering 2
* Aflevering 3
* Serie2
* Season 1
* Aflevering 1
* Aflevering 2
* Aflevering 3
* Season 2
* Aflevering 1
* Aflevering 2
* Aflevering 3
Heeft iemand een idee hoe dit mogelijk is?
Hier de code die ik gebruik:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//declare the SQL statement that will query the database
$query = "SELECT * FROM episode WHERE seen=0";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h4>Gevonden afleveringen: " . $numRows . "</h4>";
//display the results
while($row = mssql_fetch_array($result))
{
$serie_array[] = array('serie'=>$row["serie"],'season'=>$row["season"],'episode'=>$row["episode"],);
//Tijdelijke echo
echo "<li>" . $row["episode"] . "</li>";
}
//Echo array
}
$query = "SELECT * FROM episode WHERE seen=0";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h4>Gevonden afleveringen: " . $numRows . "</h4>";
//display the results
while($row = mssql_fetch_array($result))
{
$serie_array[] = array('serie'=>$row["serie"],'season'=>$row["season"],'episode'=>$row["episode"],);
//Tijdelijke echo
echo "<li>" . $row["episode"] . "</li>";
}
//Echo array
}
Alvast bedankt!
edit:
Code tussen code tag gezet
Gewijzigd op 17/10/2012 15:43:19 door Jos Vink
zet je code tussen [code]-tags
Offtopic:
Bedenk goed of je in het engels of in het nederlands wilt scripten, aflevering en season staat niet...
Quote:
* Serie1
* Season 1
* Aflevering 1
...
* Season 1
* Aflevering 1
...
Welke informatie je nodig hebt; de volgorde; de velden, ... moet je regelen in mysql.
Wanneer je bezig bent deze dingen te regelen in php; met array, sort, filter-dingen ...
dan ben je fout bezig.
Begin hier al eens mee (Eventueel aanvullen met de extra velden die je nodig hebt):
Code (php)
1
2
3
4
2
3
4
SELECT serie, season, episode
FROM episode
WHERE seen=0
ORDER BY serie, season, episode
FROM episode
WHERE seen=0
ORDER BY serie, season, episode
Dan staat alles al mooi in de juiste volgorde. Je zal merken hoeveel gemakkelijker een antwoord op je vraag dan wordt
(PS. Vind je het niet verwarrend om een tabel te hebben die de zelfde naam heeft als 1 van haar velden? Ik zou dat vermijden)
@Kris Peeters Ik zal me SQL script hier op aanpassen, Maar hoe dan verder? stel ik gebruik geen array hoe kan ik het dan op die manier weer geven op mijn site? Mijn php skills zijn niet de beste!
Michien had ik dit even moeten melden op het begin maar ik maak gebruik van een Microsoft SQL 2008 R2 database.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$serie = -1;
$season = -1;
while ($row = $stm->fetch(PDO::FETCH_ASSOC) {
if ($row['serie'] != $serie) {
//begin een nieuwe lijst
$serie = $row['serie']; //reset .pointer
}
if ($row['season'] != $season) {
//begin een nieuwe sublijst
$sesion = $row['season']; //reset de andere pointer
}
//hier de lijst vullen
}
?>
$serie = -1;
$season = -1;
while ($row = $stm->fetch(PDO::FETCH_ASSOC) {
if ($row['serie'] != $serie) {
//begin een nieuwe lijst
$serie = $row['serie']; //reset .pointer
}
if ($row['season'] != $season) {
//begin een nieuwe sublijst
$sesion = $row['season']; //reset de andere pointer
}
//hier de lijst vullen
}
?>
Mijn excuses als er wat fout is met de PDO syntax, ik werk hier nooit mee.
Gewijzigd op 17/10/2012 18:27:49 door Ger van Steenderen