i have made a small system where registered users may add places through the city's downtown and actually give them thumbs up or down, like video rating on youtube.
The conditions:
- a registered user may add as many places as they want
- a registered user may vote for each/any place only once - by giving a thumb up or down...
I have performed it with help of explode()
1.I created a cell in users table called "voted_for"
2.when users votes for some place he:
a.explode() the "voted_for" string,using | as separator
b.compare each chunk with the id of the place - if it exists within the results of exploded "voted_for" - forbid user to vote again
if it's not -do the voting and adds that place's id to his "voted_for" with a |
I did get everything to work properly, thus question is - how to make this work with relations db?
Here is the code:
Code:
if(isset($_REQUEST['sted'])) {
$sted = $_REQUEST['sted'];
$kanlide = $_REQUEST['kanlide'];
$get_places_que = mysql_query("SELECT * FROM users WHERE id='$sid'");
$places_array = mysql_fetch_array($get_places_que);
$all_places = $places_array['votedfor'];
$place = explode("|", $all_places);
$bedom = "yes";
for($i = 0; $i <= count($place); $i++) {
if($place["$i"] == $sted) {
echo "<span>Du har bedømt den valgte forslag.</span>";
$bedom = "no";
}
}
if($bedom == "yes" && $kanlide == "yes") {
$get_yes_votes = mysql_query("SELECT * FROM steder WHERE id='$sted'");
$yes_votes_array = mysql_fetch_array($get_yes_votes);
$newrate = $yes_votes_array['sted_good'] + 1;
$addvote = $all_places."|$sted";
$place_que = mysql_query("UPDATE steder SET sted_good='$newrate' WHERE id='$sted'");
$user_que = mysql_query("UPDATE users SET votedfor='$addvote' WHERE id='$sid'");
}
elseif($bedom == "yes" && $kanlide == "no") {
$get_no_votes = mysql_query("SELECT * FROM steder WHERE id='$sted'");
$no_votes_array = mysql_fetch_array($get_no_votes);
$newrate = $no_votes_array['sted_bad'] + 1;
$addvote = $all_places."|$sted";
$place_que = mysql_query("UPDATE steder SET sted_bad='$newrate' WHERE id='$sted'");
$user_que = mysql_query("UPDATE users SET votedfor='$addvote' WHERE id='$sid'");
}
}