RE: [PHP-DB] hotornot functionality [resolved]
| From: | Matthew Horn | Date: | Fri, 30 May 2003 19:06:35 +0000 |
| Subject: | RE: [PHP-DB] hotornot functionality [resolved] | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-28183@lists.php.net to get a copy of this message | ||
My apologies for the large email, but I wanted to include all the source code for interested folks
and thank everyone who provided suggestions for implementing "hot or not"-like
functionality.
Here is what I ended up doing. I've stripped out alot of the formatting to make it as readable
as possible, but it's a little messy. Tried to comment it to help you see what is going on.
Basically, it builds a comma separated list of images that have been rated and using "NOT
IN" in the query, gets a random one that hasn't been seen yet.
Feel free to contact me off list if you want to see the beta page that implements this.
Matt
-------------------------------------------------------------------------------
<?php
@ $conn = getDBConn();
$total_num_rows = 4; // number of rows in table -- will not stay static, but for beta, just make
it 4
@ $action = $_POST['action']; //set by the form;
if ($action == "rateIt") {
@ $excludeList = $_POST['excludeList']; // comma-separated list of ID's; passed as
a hidden form field
@ $rating = $_POST['rating']; // the user-submitted rating
@ $honID = $_POST['honID']; //the ID for the image; passed as a hidden form field
$query = "update hotornot set numVotes=numVotes+1,
pointTotal=pointTotal+".$rating." where honID=" . $honID;
$result_rateIt = mysql_query($query);
//get last image plus statistics
$query = "select image, numVotes, pointTotal from hotornot where honID=" . $honID;
$result_getLastPic = mysql_query($query);
$row = mysql_fetch_array($result_getLastPic);
$lastImage = $row['image'];
$lastNumVotes = $row['numVotes'];
$lastPointTotal = $row['pointTotal'];
} else {
$excludeList = "0"; // reset or for first time visitor -- must init it as a 0, otherwise
db error if empty
}
//get new image
$query = "select honID, image from hotornot where honID not in (" . $excludeList .
") order by rand() limit 1";
$result_getPic = mysql_query($query);
$row = mysql_fetch_array($result_getPic);
$image = $row['image'];
$noMoreImages = FALSE;
if (!$image) { // identifies that query as not finding an image, which means it was the last image
$noMoreImages = TRUE;
}
$honID = $row['honID'];
if ($action == "rateIt") {
?>
<!-- this is the "Last Image" table, shows the one the user just rated and it's
statistics -->
<TABLE bgcolor="#9999CC">
<tr>
<td align=center><?php echo round($lastPointTotal/$lastNumVotes,1); ?><br>
based on <?php echo $lastNumVotes;?> votes<br>
You rated it: <?php echo $rating; ?> <br>
<img src="hotornot/<?php echo $lastImage; ?>" width=80 height=80 border=0
vspace=2 hspace=2>
</td>
</TR>
</TABLE>
<?php
} else { //else, the user just started and there is no "Last Image" box to show
echo '<img src="images/dot-white.gif" alt="" width="110"
height="1" border="0">';
}
?>
<form action="hotornot.php" method="post" target="_top">
<TABLE border="0" width="340" bgcolor="#FFFFFF"
cellpadding="0" cellspacing="0">
<?php
if ($noMoreImages) {
echo '<TR><TD><A HREF="hotornot.php">Start
Over</A></TD></TR>';
} else {
?>
<TR>
<TD colspan="12"><BR><div align="center"><h3>Rate from
1 through 10 to see the next picture</h3></div><BR></TD>
</TR>
<TR>
<TD></TD>
<!-- Horizontal array of radio buttons for the user to click. Clikcing on one immediately
submits the form with the onclick events. -->
<td bgcolor=#204080><div align="center"><input type=radio name=rating
value=1 onclick="this.form.submit()"><BR><font size=1
color="#FFFFFF">1</font></div></td>
<td bgcolor=#202F70><div align="center"><input type=radio name=rating
value=2 onclick="this.form.submit()"><BR><font size=1
color="#FFFFFF">2</font></div></td>
<td bgcolor=#3F2060><div align="center"><input type=radio name=rating
value=3 onclick="this.form.submit()"><BR><font size=1
color="#FFFFFF">3</font></div></td>
<td bgcolor=#5F2050><div align="center"><input type=radio name=rating
value=4 onclick="this.form.submit()"><BR><font size=1
color="#FFFFFF">4</font></div></td>
<td bgcolor=#7F1F4F><div align="center"><input type=radio name=rating
value=5 onclick="this.form.submit()"><BR><font size=1
color="#FFFFFF">5</font></div></td>
<td bgcolor=#90103F><div align="center"><input type=radio name=rating
value=6 onclick="this.form.submit()"><BR><font size=1
color="#FFFFFF">6</font></div></td>
<td bgcolor=#B0102F><div align="center"><input type=radio name=rating
value=7 onclick="this.form.submit()"><BR><font size=1
color="#FFFFFF">7</font></div></td>
<td bgcolor=#CF0F1F><div align="center"><input type=radio name=rating
value=8 onclick="this.form.submit()"><BR><font size=1
color="#FFFFFF">8</font></div></td>
<td bgcolor=#E0000F><div align="center"><input type=radio name=rating
value=9 onclick="this.form.submit()"><BR><font size=1
color="#FFFFFF">9</font></div></td>
<td bgcolor=#F00000><div align="center"><input type=radio name=rating
value=10 onclick="this.form.submit()"><BR><font size=1
color="#FFFFFF">10</font></div></td>
<TD></TD>
</tr>
<input type=hidden name="honID" value="<?php echo $honID; ?>">
<?php
$excludeList = $excludeList . "," . $honID; //add the current image/row to the
excludelist so you don't get duplicates until you cycle all the way through all the images
?>
<input type="hidden" name="excludeList" value="<?php echo
$excludeList; ?>">
<input type=hidden name="action" value="rateIt">
</form>
<TR><TD colspan="12"><div align="center">
<!-- here is the new image to rate -->
<IMG src="hotornot/<?php echo $image; ?>" width="250"
height="250" border="1"></div>
<?php
} //end no more images check
?>
</TD>
</TR>
</TABLE>