Uitbreiding van mijn script.
Ik heb een script voor een photo rating systeem alleen in mijn script zit niet dat mensen maar 1 keer kunnen stemmen op een foto.
Kan iemand dit hierin bouwen???
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
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
<?php
#--> connecting to mysql
@mysql_connect("localhost", "mijn naam", "mijn wachtwoord") or die (mysql_error());
mysql_select_db("mijn database") 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("photos/Cartoons/");
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 /><br /><span style="color:#ccc; font-size:10px;">'.$data['votes'].' x gestemd';
}
else {
$msg = 'Geen stemmen!';
}
?>
#--> connecting to mysql
@mysql_connect("localhost", "mijn naam", "mijn wachtwoord") or die (mysql_error());
mysql_select_db("mijn database") 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("photos/Cartoons/");
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 /><br /><span style="color:#ccc; font-size:10px;">'.$data['votes'].' x gestemd';
}
else {
$msg = 'Geen stemmen!';
}
?>
<!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>Rate a Picture - See what is the best picture</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">
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
#--> echo img en stats
echo '<img class="img" src="photos/Cartoons/'.$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";
?>
#--> echo img en stats
echo '<img class="img" src="photos/Cartoons/'.$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>
<br />
<br />
<i style="color:#aaa; font-size:11px;"><u>Rate a picture || ©opyright by Bmxonline</u></i>
<br />
<br />
</body>
</html>
Of maak het zelf of zoek he zelf uit. Wij zijn hier op PHPhulp.nl en niet op PHPwiljijhetvoormijmaken.nl.