$_GET id = met naam
Ik ben bezig met een website, waar ik content kan toevoegen en dat anderen het kunnen zien. Ik heb nu een page dat heet category.php, ik heb daar een aantal categorieen als voorbeeld neem ik de categorie 'Test' aan, 'Test' heeft als id nummer 1 nu heb ik als link: category.php?nameID=1 ik wil dit graag naar ?nameID=Test. In deze categorie zitten ook wat berichten, ik neem als voorbeeld 'brood' als ik naar deze pagina wil gaan is de url; category.php?nameid=1&id=1 dit wil ik echter ook een andere naam geven: ?nameID=Test&id=brood . Mijn doel is om friendly urls te beschikken over mijn site, echter is de voorbeeld wat ik gaf niet friendly genoeg. Ik wil van die url de volgende maken: site.com/Test/RANDOMnaam
http://pastebin.com/Vy3vtxUs staat mijn code ook op;
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
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
<?php
// BEGIN OF SHOWING CONTENT PAGE
if (isset($_GET['id'])){
$naamID = mysql_real_escape_string($_GET['nameID']);
$id = mysql_real_escape_string($_GET['id']);
$idnext = $id + 1;
$gn = (" SELECT * FROM category WHERE nameID='".$naamID."'") or die(mysql_error());
$go = (" SELECT * FROM post WHERE id='".$id."'") or die(mysql_error());
$gnn = mysql_query($gn) or die(mysql_error());
$goo = mysql_query($go) or die(mysql_error());
$gnnn = mysql_fetch_array($gnn);
$gooo = mysql_fetch_array($goo);
?>
<?php
echo '<p>';
if(empty($gooo['youtube'])){
} else {
?> <h1> <?php echo htmlspecialchars($gooo["title"]); ?> </h1><br />
<?php
$fullurl1 = $gooo['youtube'];
$videoid1=substr($fullurl1,-11);
?>
<?php
echo '<p><a href="/editpost.php?id='.$gooo['id'].'"><i>Edit this post</i></a><br />';
echo '<iframe width="560" height="315" src="//www.youtube.com/embed/'.$videoid1.'" frameborder="0" allowfullscreen></iframe><br />';
echo '</p>';
}
if(empty($gooo['pic'])){
} else {
?> <h1> <?php echo htmlspecialchars($gooo["title"]); ?> </h1><br />
<?php
echo '<p><a href="/editpost.php?id='.$gooo['id'].'"><i>Edit this post</i></a><br />';
echo '<img src="'.$gooo["pic"].'" style="max-height: auto; max-width: 600px;"/><br></p>';
}
}
echo '</p>'; ?>
<?php
// END OF SHOWING CONTENT PAGE
// BEGIN category page
if(isset($_GET['nameID'])){
// PAGE BEGIN
$count_query = mysql_query("SELECT * FROM post ORDER BY date ASC");
$nr = mysql_num_rows($count_query);
if(isset($_GET['page'])){
$page = preg_replace('#[^0-9]#i','',$_GET['page']);
}else{
$page = 1;
}
$perPage = 5;
$lastPage = ceil($nr / $perPage);
if($page < 1) {
$page = 1;
} else if ($page > $lastPage){
$page = $lastPage;
}
$limit = 'LIMIT ' . ($page - 1) * $perPage .',' .$perPage;
$query = ("SELECT * FROM post WHERE cat='".$nameID."' ORDER BY date DESC ".$limit."");
if($lastPage != 1){
if($page != $lastPage){
$naamID2 = mysql_real_escape_string($_GET['nameID']);
$next = $page + 1;
$pagination .= '<a href="/category.php?nameID='.$naamID2.'&page='.$next.'">Next.. </a>';
}
if($page != 1){
$naamID1 = mysql_real_escape_string($_GET['nameID']);
$prev = $page - 1;
$pagination .= '<a href="/category.php?nameID='.$naamID1.'&page='.$prev.'"> ..Prev</a>';
}
}
// PAGE END
?>
<?php
$nameID = mysql_real_escape_string($_GET['nameID']);
$na = (" SELECT * FROM category WHERE nameID='".$nameID."'") or die(mysql_error());
$nb = (" SELECT * FROM post WHERE cat='".$nameID."' ORDER BY date DESC ".$limit."") or die(mysql_error());
$naa = mysql_query($na) or die(mysql_error());
$nbb = mysql_query($nb) or die(mysql_error());
$naaa = mysql_fetch_array($naa);
?>
<h1> <?php echo htmlspecialchars($naaa["name"]); ?></h1><br />
<i> <?php echo htmlspecialchars($naaa["description"]); ?> </i><br />
<?php
echo '<p><a href="category.php"><i>back to all categoties</i></a></p>';
while($nbbb=mysql_fetch_array($nbb, MYSQL_ASSOC)){
$fullurl = $nbbb['youtube'];
$videoid=substr($fullurl,-11);
// showing youtube video when there is no picture
if(empty($nbbb['youtube'])){
} else {
if( $nbbb['NSFW'] == 0) {
echo '<h1><a href="category.php?nameid='.$naaa['name'].'&id='.$nbbb['id'].'">';?><?php echo htmlspecialchars($nbbb["title"]); ?></a></h1><br><?php
echo '<p><iframe width="560" height="315" src="//www.youtube.com/embed/'.$videoid.'" frameborder="0" allowfullscreen></iframe>';
echo '</p>';
}
if( $nbbb['NSFW'] == 1) {
echo '<h1><a href="category.php?nameid='.$naaa['nameID'].'&id='.$nbbb['id'].'">';?><?php echo htmlspecialchars($nbbb["title"]); ?></a></h1><br><?php
echo '<p><a href="category.php?nameid='.$naaa['nameID'].'&id='.$nbbb['id'].'"><img src="http://i.imgur.com/w88amwc.png"/></a><br>';
echo '</p>';
}
}
// showing picture when there is no youtube
if(empty($nbbb['pic'])){
} else {
if( $nbbb['NSFW'] == 0) {
echo '<h1><a href="category.php?nameid='.$naaa['nameID'].'&id='.$nbbb['id'].'">'; ?><?php echo htmlspecialchars($nbbb["title"]); ?> <?php echo '</a></h1><br>'; ?>
<?php
echo '<p><img src="'.$nbbb["pic"].'" style="max-height: auto; max-width: 600px;"/>';
echo '</p>';
}
if( $nbbb['NSFW'] == 1) {
echo '<h1><a href="category.php?nameid='.$naaa['nameID'].'&id='.$nbbb['id'].'">'; ?><?php echo htmlspecialchars($nbbb["title"]); ?><?php echo '</a></h1><br>'; ?>
<?php
echo '<p><a href="category.php?nameid='.$naaa['nameID'].'&id='.$nbbb['id'].'"/><img src="http://i.imgur.com/w88amwc.png"/></a><br>';
echo '</p>';
}
}
}
echo '<p>';
echo '<b><i>'.$pagination.'</b></i>';
echo '</p>';
}// END OF SHOWING MAIN PAGE OF CATEGORYS
?>
// BEGIN OF SHOWING CONTENT PAGE
if (isset($_GET['id'])){
$naamID = mysql_real_escape_string($_GET['nameID']);
$id = mysql_real_escape_string($_GET['id']);
$idnext = $id + 1;
$gn = (" SELECT * FROM category WHERE nameID='".$naamID."'") or die(mysql_error());
$go = (" SELECT * FROM post WHERE id='".$id."'") or die(mysql_error());
$gnn = mysql_query($gn) or die(mysql_error());
$goo = mysql_query($go) or die(mysql_error());
$gnnn = mysql_fetch_array($gnn);
$gooo = mysql_fetch_array($goo);
?>
<?php
echo '<p>';
if(empty($gooo['youtube'])){
} else {
?> <h1> <?php echo htmlspecialchars($gooo["title"]); ?> </h1><br />
<?php
$fullurl1 = $gooo['youtube'];
$videoid1=substr($fullurl1,-11);
?>
<?php
echo '<p><a href="/editpost.php?id='.$gooo['id'].'"><i>Edit this post</i></a><br />';
echo '<iframe width="560" height="315" src="//www.youtube.com/embed/'.$videoid1.'" frameborder="0" allowfullscreen></iframe><br />';
echo '</p>';
}
if(empty($gooo['pic'])){
} else {
?> <h1> <?php echo htmlspecialchars($gooo["title"]); ?> </h1><br />
<?php
echo '<p><a href="/editpost.php?id='.$gooo['id'].'"><i>Edit this post</i></a><br />';
echo '<img src="'.$gooo["pic"].'" style="max-height: auto; max-width: 600px;"/><br></p>';
}
}
echo '</p>'; ?>
<?php
// END OF SHOWING CONTENT PAGE
// BEGIN category page
if(isset($_GET['nameID'])){
// PAGE BEGIN
$count_query = mysql_query("SELECT * FROM post ORDER BY date ASC");
$nr = mysql_num_rows($count_query);
if(isset($_GET['page'])){
$page = preg_replace('#[^0-9]#i','',$_GET['page']);
}else{
$page = 1;
}
$perPage = 5;
$lastPage = ceil($nr / $perPage);
if($page < 1) {
$page = 1;
} else if ($page > $lastPage){
$page = $lastPage;
}
$limit = 'LIMIT ' . ($page - 1) * $perPage .',' .$perPage;
$query = ("SELECT * FROM post WHERE cat='".$nameID."' ORDER BY date DESC ".$limit."");
if($lastPage != 1){
if($page != $lastPage){
$naamID2 = mysql_real_escape_string($_GET['nameID']);
$next = $page + 1;
$pagination .= '<a href="/category.php?nameID='.$naamID2.'&page='.$next.'">Next.. </a>';
}
if($page != 1){
$naamID1 = mysql_real_escape_string($_GET['nameID']);
$prev = $page - 1;
$pagination .= '<a href="/category.php?nameID='.$naamID1.'&page='.$prev.'"> ..Prev</a>';
}
}
// PAGE END
?>
<?php
$nameID = mysql_real_escape_string($_GET['nameID']);
$na = (" SELECT * FROM category WHERE nameID='".$nameID."'") or die(mysql_error());
$nb = (" SELECT * FROM post WHERE cat='".$nameID."' ORDER BY date DESC ".$limit."") or die(mysql_error());
$naa = mysql_query($na) or die(mysql_error());
$nbb = mysql_query($nb) or die(mysql_error());
$naaa = mysql_fetch_array($naa);
?>
<h1> <?php echo htmlspecialchars($naaa["name"]); ?></h1><br />
<i> <?php echo htmlspecialchars($naaa["description"]); ?> </i><br />
<?php
echo '<p><a href="category.php"><i>back to all categoties</i></a></p>';
while($nbbb=mysql_fetch_array($nbb, MYSQL_ASSOC)){
$fullurl = $nbbb['youtube'];
$videoid=substr($fullurl,-11);
// showing youtube video when there is no picture
if(empty($nbbb['youtube'])){
} else {
if( $nbbb['NSFW'] == 0) {
echo '<h1><a href="category.php?nameid='.$naaa['name'].'&id='.$nbbb['id'].'">';?><?php echo htmlspecialchars($nbbb["title"]); ?></a></h1><br><?php
echo '<p><iframe width="560" height="315" src="//www.youtube.com/embed/'.$videoid.'" frameborder="0" allowfullscreen></iframe>';
echo '</p>';
}
if( $nbbb['NSFW'] == 1) {
echo '<h1><a href="category.php?nameid='.$naaa['nameID'].'&id='.$nbbb['id'].'">';?><?php echo htmlspecialchars($nbbb["title"]); ?></a></h1><br><?php
echo '<p><a href="category.php?nameid='.$naaa['nameID'].'&id='.$nbbb['id'].'"><img src="http://i.imgur.com/w88amwc.png"/></a><br>';
echo '</p>';
}
}
// showing picture when there is no youtube
if(empty($nbbb['pic'])){
} else {
if( $nbbb['NSFW'] == 0) {
echo '<h1><a href="category.php?nameid='.$naaa['nameID'].'&id='.$nbbb['id'].'">'; ?><?php echo htmlspecialchars($nbbb["title"]); ?> <?php echo '</a></h1><br>'; ?>
<?php
echo '<p><img src="'.$nbbb["pic"].'" style="max-height: auto; max-width: 600px;"/>';
echo '</p>';
}
if( $nbbb['NSFW'] == 1) {
echo '<h1><a href="category.php?nameid='.$naaa['nameID'].'&id='.$nbbb['id'].'">'; ?><?php echo htmlspecialchars($nbbb["title"]); ?><?php echo '</a></h1><br>'; ?>
<?php
echo '<p><a href="category.php?nameid='.$naaa['nameID'].'&id='.$nbbb['id'].'"/><img src="http://i.imgur.com/w88amwc.png"/></a><br>';
echo '</p>';
}
}
}
echo '<p>';
echo '<b><i>'.$pagination.'</b></i>';
echo '</p>';
}// END OF SHOWING MAIN PAGE OF CATEGORYS
?>
- Aar -:
Gelieve in het vervolg bij code de [code][/code]-tags gebruiken.
Alvast bedankt!
Alvast bedankt!
Gewijzigd op 04/04/2014 23:10:51 door - Ariën -
Ivo P op 04/04/2014 23:08:09:
Ik heb het al voor elkaar gekregen, echter heb ik nu een andere probleem met de spatie, %20 ik probeer dit te veranderen in - maar dan ontvang ik niks meer van uit het mysql ik gebruik deze code;
friendly urls en rewriterules zijn toch wel twee wezenlijk verschillende dingen.
Haddi:
Ik vind persoonlijk dat friendly urls het beste gecombineerd kunnen worden met een PHP framework. Simpel omdat de functionaliteit dan al is ingebouwd. Je zou eens kunnen kijken naar codeigniter of cakephp. er zijn betere frameworks maar die zijn moeilijker onder de knie te krijgen.
wat betreft het directe antwoord op je twee vragen zou je eens op 'slug' kunnen googlen.