PDA

View Full Version : vote/rate add-on query


sabret00the
11-28-2003, 09:39 AM
if i wanted to add a voting/rating process to something, how would i go about doing it?

i was thinking just set up an additional table

confession id | rate | voter (bbusername)

and then just join that to the other table via left joins?

sabret00the
11-28-2003, 11:16 AM
lol, since this morning i've dummified, so how would i go about this after i set up the tables lol

Dean C
11-28-2003, 03:22 PM
Moved to the correct forum :)

assassingod
11-28-2003, 04:10 PM
You would do something like:

$DB_site->query("UPDATE table SET rate = '$oldrate + 1', voter = '$bbuserinfo[username]'");


And use another query to get $oldrate

That's the way I did it for my vB3 point system (I think :dumbass: )

sabret00the
11-28-2003, 04:41 PM
but wouldn't that just add or whatever to the rate was already there instead of working out an average rate?

assassingod
11-28-2003, 04:44 PM
but wouldn't that just add or whatever to the rate was already there instead of working out an average rate?
You didn't say anything about working out the average rate, but to work out the average you would use

$avgrate = $DB_site->query("SELECT avg(rate) FROM table");

sabret00the
11-28-2003, 05:23 PM
thanks i'll try implementing this code :)

sabret00the
11-28-2003, 11:25 PM
ok in setting up the table would this be right?
CREATE TABLE confession_rate (
rateid MEDIUM INT UNSIGNED NOT NULL AUTO_INCREMENT,
confessionid BIGINT(20) ( SIGNED NOT NULL,
userid INT(10) SIGNED NOT NULL,
timestamp INT(11) NOT NULL,
rate SMALLINT(2) SIGNED NOT NULL,
PRIMARY KEY (rateid)
);?
confessionid is set via php $confessionid
user_id is set by $bbuserinfo[userid]
timestamp is set when a confession is voted for
rate is what you're rating the confession (1-10)
and i set the primary key to by the confession id but that wouldn't work so i've decided i need another row for rateid.

sabret00the
11-29-2003, 12:41 AM
figured out i don't need the left joins but would be great if you can tell me if i set that SIGNED and UNSIGNED stuff right.

assassingod
11-29-2003, 07:31 AM
This would be a better table structure:

CREATE TABLE confession_rate (
rateid TINYINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
confessionid TINYINT(5) NOT NULL,
userid TINYINT(5) NOT NULL,
time TIMESTAMP(14) NOT NULL,
rate SMALLINT(5) NOT NULL,
PRIMARY KEY (rateid)
);

sabret00the
11-29-2003, 11:14 AM
thanks assassin, you've been a great help, i've got to final stages of this end of things, which means it'll be fully usable soon, all bar one thing, in my script, i'm processing the ratings via if (isset'confess_rate' && $rate && $confessionid != '' && $bbuserinfo[userid] > 0) {

$flood = $DB_site->query("SELECT timestamp FROM confessions ORDER BY timestamp DESC LIMIT 1");
$check = $DB_site->fetch_array($flood);

if ((time() - $check[timestamp]) <= 10) {
eval("standarderror(\"".gettemplate("confession_error_vote")."\");");
}
if ($bbuserinfo[userid] == $DB_site->query("SELECT userid FROM confession_rate WHERE confessionid = $confessionid")) {
eval("standarderror(\"".gettemplate("confession_error_novotetwice")."\");");
} else {
$DB_site->query("INSERT INTO confession_rate SET
confessionid = '$confessionid',
userid = '$bbuserinfo[userid]',
rate = '$rate',
timestamp = '".time ()."'");
}
header("Location: $PHP_SELF?s=");
} else {
header("Location: $PHP_SELF?s=");
}but it won't submit to the database, what am i doing wrong?

assassingod
11-29-2003, 11:18 AM
If you're implenting this into vB, you'd want to do something like:

####### display form #######
if (!isset($_POST['process']) or $_POST['process'] == '')
{
//display vb and form stuff
}

// ###### start insert ######
if (isset($_POST['process']) and $_POST['process'] != '' and $_POST['process'] == 'go')
{
//vb stuff and insert into DB stuff
}


Not sure if that'll be a great deal of help, used it from my mailing list hack

sabret00the
11-29-2003, 11:37 AM
sorry assassin, that was experimental code, heres what i was doing properly
case "voting":
// this conditional handles the rating
if ('confess_rate' && $rate && $confessionid != '' && $bbuserinfo[userid] > 0) {
$flood = $DB_site->query("SELECT timestamp FROM confessions ORDER BY timestamp DESC LIMIT 1");
$check = $DB_site->fetch_array($flood);
if ((time() - $check[timestamp]) <= 10) {
eval("standarderror(\"".gettemplate("confession_error_vote")."\");");
}
if ($bbuserinfo[userid] == $DB_site->query("SELECT userid FROM confession_rate WHERE confessionid = $confessionid")) {
eval("standarderror(\"".gettemplate("confession_error_novotetwice")."\");");
} else {
$DB_site->query("INSERT INTO confession_rate SET
confessionid = '$confessionid',
userid = '$bbuserinfo[userid]',
rate = '$rate',
timestamp = '".time ()."'");
}
header("Location: $PHP_SELF?s=");
} else {
header("Location: $PHP_SELF?s=");
}
break;

but i'll try the using the super globals :)

ps. this is just the form processing, the actual form display elsewhere in the document.

heres the link to where this is all happening http://www.ebslive.com/confessions/index.php?confessionid=49

sabret00the
11-29-2003, 01:32 PM
<form method="post" name="confess_rate" action="?do=voting">
<input type="hidden" name="$confessionid">
<input type="hidden" name="$bbusername">
<table width="80%">
<tr>
<td align="center" colspan="2" nowrap>
<smallfont>rate how good you think this confession is</smallfont><br />
<input type="radio" name="rate" value="1" onclick="document.confess_rate.submit()">
<input type="radio" name="rate" value="2" onclick="document.confess_rate.submit()">
<input type="radio" name="rate" value="3" onclick="document.confess_rate.submit()">
<input type="radio" name="rate" value="4" onclick="document.confess_rate.submit()">
<input type="radio" name="rate" value="5" onclick="document.confess_rate.submit()">
<input type="radio" name="rate" value="6" onclick="document.confess_rate.submit()">
<input type="radio" name="rate" value="7" onclick="document.confess_rate.submit()">
<input type="radio" name="rate" value="8" onclick="document.confess_rate.submit()">
<input type="radio" name="rate" value="9" onclick="document.confess_rate.submit()">
<input type="radio" name="rate" value="10"onclick="document.confess_rate.submit()">
</td>
</tr>
<tr>
<td align="left" width="50%"><smallfont>Saint</smallfont></td>
<td align="right" width="50%"><smallfont>Sinner</smallfont></td>
</tr>
<tr>
<td align="center" colspan="2"><br>
<smallfont>this confession currently has a rating of $confession_avg_rate</smallfont>
</td>
</tr>
</table>
</form>

is the form that submits the data :)

sabret00the
11-29-2003, 07:30 PM
lol, i was standing in the kebab shop when i finally got what you meant, thanks steven, took me all day but i got there in the end, gonna try it now :)

assassingod
11-29-2003, 07:33 PM
lol, i was standing in the kebab shop when i finally got what you meant, thanks steven, took me all day but i got there in the end, gonna try it now :)hehe, nothing like food the make you think:D

Hope it goes ok.

sabret00the
11-29-2003, 10:17 PM
got the code down from 25 lines to 17 but it's still not working, i think this is down to the actual form rather than the coding tho.
// this conditional handles the rating
if ($_POST['submit'] == 'confess_rate') {
$confessionid = intval($_POST['confessionid']);
$verify = $DB_site->query_first("SELECT userid FROM confession_rate WHERE confessionid = $confessionid AND userid = $bbuserinfo[userid]");
if ($verify) {
eval("standarderror(\"".gettemplate("confession_error_novotetwice")."\");");
} else {
$DB_site->query("INSERT INTO confession_rate SET
confessionid = '$confessionid',
userid = '$bbuserinfo[userid]',
rate = '$rate',
timestamp = '".time ()."'");

header("Location: $PHP_SELF?s="); //takes you back to main confession page
}
} else {
eval("standarderror(\"".gettemplate("confession_error_vote")."\");");
}

assassingod
11-29-2003, 10:27 PM
*takes time away from his new hack*

Try using:

if($action=="displayform")
{
// form code here
}
if ($HTTP_POST_VARS['action']=="processform")
{
// queries and stuff
}


Then for the form tag, you would use something like

<form action="pagehere.php" method="post"><input type="hidden" name="s" value="$session[dbsessionhash]">
<!-- form code -->
<input type="hidden" name="action" value="processform">
<input type="submit" class="bginput" name="Submit" value="Submit Tutorial" accesskey="s">

</form>


You'll need to edit that a bit but thats a better way of doing from what i said earlier

Also, use the standardredirect function instead of header("locaion:");

sabret00the
11-30-2003, 11:47 AM
i'm getting so close to giving up on this, i dunno what's wrong with it, i've tried so many methods, but it's not passing to the database, infact i get the error i set up to say the vote cannot be processed :(

if ($_POST['submit'] == "rate") {
$confessionid = intval($_POST['confessionid']);
$verify = $DB_site->query_first("SELECT userid FROM confession_rate WHERE confessionid = $confessionid AND userid = $bbuserinfo[userid]");
if ($verify) {
eval("standarderror(\"".gettemplate("confession_error_novotetwice")."\");");
} else {
$DB_site->query("INSERT INTO confession_rate SET
confessionid = '$confessionid',
userid = '$bbuserinfo[userid]',
rate = '$rate',
timestamp = '".time ()."'");

header("Location: $PHP_SELF?s="); //takes you back to main confession page
}
} else {
eval("standarderror(\"".gettemplate("confession_error_vote")."\");");
}

arghhh, i think i just found it, i tried to pass the username with the form and not the userid :mad: will check it now :)

sabret00the
11-30-2003, 12:00 PM
edit, it was such a stupid mistake
<input type="hidden" name="$confessionid">
<input type="hidden" name="$bbusername">

should have been
<input type="hidden" name="confessionid" value="$confessionid">
<input type="hidden" name="userid" value="$bbuserinfo[userid]">

:lol: it's still not working yet, but i'm a step closer :)

sabret00the
12-01-2003, 12:00 AM
got it all working, special thanks to Kura, NTLDR, Assassingod, Mist and the many other who tried to help me. :)