photo-rating-system
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
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
<?php
#--> connecting to mysql
@mysql_connect("localhost", "...", "...") or die (mysql_error());
mysql_select_db("...") or die(mysql_error());
#--> updaten of inserten als er ge-vote is op een plaatje
if(isset($_POST['rated']))
{
#--> eerst kijken of record al in DB bestaat
$sql = "SELECT `votes`, `score` FROM `photo_rating` WHERE `img_name`='$_POST[img]'";
$res = @mysql_query($sql) or die(mysql_error());
#--> what's the sql going to be, insert or update
$sql = (mysql_num_rows($res) == 0) ? "INSERT INTO `photo_rating` SET `img_name`='$_POST[img]', `votes`='1', `score`='$_POST[rated]'" : "UPDATE `photo_rating` SET `votes`=(votes + 1), `score`=(score + '$_POST[rated]') WHERE `img_name`='$_POST[img]'";
$res = @mysql_query($sql) or die(mysql_error());
}
#--> allowed img extentions
$alowed_files = array(".gif", ".jpg", ".png");
#--> opendir, read files, make array
$handle = opendir("images/");
while ($file = readdir($handle))
{
$len = strlen($file);
$ext = substr($file, $len -4, $len);
if(in_array($ext, $alowed_files)) {
$files[] = $file;
}
}
closedir($handle);
#--> counting array
$num = count($files);
#--> fetching get[img] if its there, otherwise rand img
$img = isset($_GET['img']) && is_numeric($_GET['img']) && isset($files[$_GET['img'] -1]) ? $_GET['img'] : rand(1, $num);
#--> pulling data form db where img_name is
$sql = "SELECT `votes`, `score` FROM `photo_rating` WHERE `img_name`='".$files[$img-1]."'";
$res = mysql_query($sql) or die(mysql_error());
#--> opmaken van de stats van de foto
if(mysql_num_rows($res) != 0)
{
$data = mysql_fetch_assoc($res);
$score = round($data['score'] / $data['votes'], 1);
$score = strlen($score) < 2 ? $score .= '.0' : $score;
$s_img = explode('.', $score);
switch($s_img[1])
{
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
$s_img = $s_img[0].'_5';
break;
case 8 :
case 9 :
$s_img = ($s_img[0] + 1).'_0';
break;
default :
$s_img = $s_img[0].'_0';
break;
}
$msg ='<img src="rating/'.$s_img.'.gif" alt="'.$s_img.'" /><br /><span style="color:#ccc; font-size:10px;">'.$data['votes'].' stemmen<br />score : '.$score.'</span>';
}
else
{
$msg = 'no votes yet';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Photo rating system - by nano</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
body {
font-family:verdana;
font-size:12px;
background-color:#eee;
text-align:center;}
.img {
border:1px solid #000;}
.photo {
text-align:center;
width:440px;
margin:0px auto;
padding:40px;
background-color:#fff;
border:1px solid #000;}
</style>
</head>
<body>
<br />
<br />
<div class="photo">
<?php
#--> echo img en stats
echo '<img class="img" src="images/'.$files[$img-1].'" alt="img" />'."\n<br />\n<br />\n".$msg."\n";
#--> echo-ing form
echo '<br /><br /><form action="'.$_SERVER['PHP_SELF'].(($img < $num) ? '?img='.($img + 1) : '?img=1').'" method="post">'."\n";
echo '<input type="hidden" name="img" value="'.$files[$img-1].'" />'."\n";
for($i = 0; $i <= 5; $i++)
{
echo $i.'<input type="radio" name="rated" value="'.$i.'" onclick="submit();" onmouseover="this.checked=true;" onmouseout="this.checked=false;" style="cursor:pointer;" /> '."\n";
}
echo "</form>\n";
?>
</div>
</body>
</html>
#--> connecting to mysql
@mysql_connect("localhost", "...", "...") or die (mysql_error());
mysql_select_db("...") or die(mysql_error());
#--> updaten of inserten als er ge-vote is op een plaatje
if(isset($_POST['rated']))
{
#--> eerst kijken of record al in DB bestaat
$sql = "SELECT `votes`, `score` FROM `photo_rating` WHERE `img_name`='$_POST[img]'";
$res = @mysql_query($sql) or die(mysql_error());
#--> what's the sql going to be, insert or update
$sql = (mysql_num_rows($res) == 0) ? "INSERT INTO `photo_rating` SET `img_name`='$_POST[img]', `votes`='1', `score`='$_POST[rated]'" : "UPDATE `photo_rating` SET `votes`=(votes + 1), `score`=(score + '$_POST[rated]') WHERE `img_name`='$_POST[img]'";
$res = @mysql_query($sql) or die(mysql_error());
}
#--> allowed img extentions
$alowed_files = array(".gif", ".jpg", ".png");
#--> opendir, read files, make array
$handle = opendir("images/");
while ($file = readdir($handle))
{
$len = strlen($file);
$ext = substr($file, $len -4, $len);
if(in_array($ext, $alowed_files)) {
$files[] = $file;
}
}
closedir($handle);
#--> counting array
$num = count($files);
#--> fetching get[img] if its there, otherwise rand img
$img = isset($_GET['img']) && is_numeric($_GET['img']) && isset($files[$_GET['img'] -1]) ? $_GET['img'] : rand(1, $num);
#--> pulling data form db where img_name is
$sql = "SELECT `votes`, `score` FROM `photo_rating` WHERE `img_name`='".$files[$img-1]."'";
$res = mysql_query($sql) or die(mysql_error());
#--> opmaken van de stats van de foto
if(mysql_num_rows($res) != 0)
{
$data = mysql_fetch_assoc($res);
$score = round($data['score'] / $data['votes'], 1);
$score = strlen($score) < 2 ? $score .= '.0' : $score;
$s_img = explode('.', $score);
switch($s_img[1])
{
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
$s_img = $s_img[0].'_5';
break;
case 8 :
case 9 :
$s_img = ($s_img[0] + 1).'_0';
break;
default :
$s_img = $s_img[0].'_0';
break;
}
$msg ='<img src="rating/'.$s_img.'.gif" alt="'.$s_img.'" /><br /><span style="color:#ccc; font-size:10px;">'.$data['votes'].' stemmen<br />score : '.$score.'</span>';
}
else
{
$msg = 'no votes yet';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Photo rating system - by nano</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
body {
font-family:verdana;
font-size:12px;
background-color:#eee;
text-align:center;}
.img {
border:1px solid #000;}
.photo {
text-align:center;
width:440px;
margin:0px auto;
padding:40px;
background-color:#fff;
border:1px solid #000;}
</style>
</head>
<body>
<br />
<br />
<div class="photo">
<?php
#--> echo img en stats
echo '<img class="img" src="images/'.$files[$img-1].'" alt="img" />'."\n<br />\n<br />\n".$msg."\n";
#--> echo-ing form
echo '<br /><br /><form action="'.$_SERVER['PHP_SELF'].(($img < $num) ? '?img='.($img + 1) : '?img=1').'" method="post">'."\n";
echo '<input type="hidden" name="img" value="'.$files[$img-1].'" />'."\n";
for($i = 0; $i <= 5; $i++)
{
echo $i.'<input type="radio" name="rated" value="'.$i.'" onclick="submit();" onmouseover="this.checked=true;" onmouseout="this.checked=false;" style="cursor:pointer;" /> '."\n";
}
echo "</form>\n";
?>
</div>
</body>
</html>