Getting sucked into a black hole

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Getting sucked into a black hole

Post by evilmonkey »

Hello ladies and gentlemen.

I am officially in a black hole. I am writing a user rating system script that is supposed to average out all the user ratings given to a certain row. Each of my rows has a field in it called 'rating'. I have a script that lloks like this:

Code: Select all

<?php
$db = mysql_connect("localhost","","");
mysql_select_db("dbthawowi_2",$db);;
$callsign = $HTTP_POST_VARS&#1111;'id']; 
$callsign = $_GET&#1111;'id'];
$query = "SELECT * FROM userjokes WHERE id='$id'";
$resultat = mysql_query($query) or die(mysql_error());
$row= mysql_fetch_object($resultat);
echo "Joke number ";
print"$row->id<br><br>";
print"$row->text<br><br>";
echo "Joke submitted by: ";
print"$row->name ";
echo "on ";
print "$row->date<br>";
?>
this script FOLLOWS this script:

Code: Select all

<?php
$db = mysql_connect("localhost","","");
mysql_select_db("dbthawowi_2",$db);;
$callsign = $HTTP_POST_VARS&#1111;'id']; 
$sql="SELECT id, joke FROM userjokes WHERE validation='yes' ORDER BY id";
$res=mysql_query($sql, $db);
while ($row = mysql_fetch_object ($res)) 
&#123;
 print "<td><a href="archive2.php?id=$row->id" target="new">" . $row->id . "</a>"; 
&#125;
?>
Now, I want to put the rating system in the second page (the code I posted first). I know how to make the radio buttons, but I have no idea even how to begin processing them, let alone find the average of all of the votes. Oh yeah. The ratings go from 1-10.

Now, I'm not asking for you people to write me a code. I'm asking you to point me in the right direction, give me an idea of how to start this. Please don't point me to the PHP manual, but something more specific. Thanks.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

let's assume an array

Code: Select all

$rates = array('best', 'good', 'average', 'bad', 'worst');
that is available to all the script parts and a table evaluation that has a tinyint-field rating.
Now you can create the form with something like

Code: Select all

echo '&lt;form method="post"&gt;';
foreach($rates as $key=&gt;$value)
	echo '&lt;input type="radio" name="rating" value="',$key,'" /&gt;', $value, '&lt;br/&gt;';
echo '&lt;/form&gt;';
to process a new vote you may use something like

Code: Select all

if ( isset($_POST&#1111;'rating']) // something sent?
		 AND isset($rates&#1111;$_POST&#1111;'rating']]) // and valid &lt;-&gt; in the rates-array?
	)
{
	// &lt;- database connection already established -&gt;
	$query = 'INSERT INTO evaluation (rating) VALUES ('.$_POST&#1111;'rating'].')';
	mysql_query($query, $conn)  or die(mysql_error());
}
to get the arithmetic mean you can query

Code: Select all

$query = 'SELECT SUM(rating)/count(rating) FROM evaluation';
$result = mysql_query($query) or die(mysql_error());
$average = mysql_fetch_row($result);
$average = $average&#1111;0];
and to output it something like

Code: Select all

echo 'average rating: ', $rates&#1111;round($average)], '(', $average, ')';
that's a very basic attempt and the arithmetic mean is not always the best choice - but anyway ;)
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

OK, so from what I understand there is another table where all the ratings are stored. That's ok, the only question is, how do I know which rating belongs to which row?

Also, I have never worked with arrays in PHP, but I have worked with arrays in C. Is it similar? I will have to look at the PHP manual for that.

Thanks Volka.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

since you 'only' asked for getting pointed in right the direction I didn't read your code but posted a small example how to do it for one vote.
The entries in your table are identified by userjokes.id
so you can create a second table that has a 1:m relation (1 joke-record, many votes) i.e.

Code: Select all

TABLE `evaluation` (
	`jokeId` tinyint(3) unsigned NOT NULL,
	`rating` tinyint(3) unsigned NOT NULL
)
now your form should contain a hidden-field with the id of the joke that is to be rated

Code: Select all

echo '&lt;form method="post"&gt;'; 
echo '&lt;input type="hidden" name="jokeId" value="', $row&#1111;'id'], '"/&gt;';
foreach($rates as $key=&gt;$value)
...
the insert-query looks like

Code: Select all

$query = 'INSERT INTO evaluation (jokeId,rating) VALUES ('.$_POST&#1111;'jokeId'].','.$_POST&#1111;'rating'].')';
(should check jokeId is valid before inserting)
and the average-select

Code: Select all

$query = 'SELECT SUM(rating)/count(rating) FROM evaluation WHERE jokeId='.$idOfJokeYouWantToKnowAbout;
php-arrays are more like an open list with overloaded []-operator in c++, where the parameter for [] can be mostly anything (number, string, etc.)

Code: Select all

$rates = array('best', 'good', 'average', 'bad', 'worst');
//is the same as 
$rates = array(0=&gt;'best', 1=&gt;'good', 2=&gt;'average', 3=&gt;'bad', 4=&gt;'worst');
echo $rates&#1111;2]; // --&gt; average

$rates = array('a'=&gt;'best', 'b'=&gt;'good', 'c'=&gt;'average', 'd'=&gt;'bad', 'whateveryouwantinhere'=&gt;'worst');
$index = 'c';
echo $rates&#1111;$index]; // --&gt; average
unset($rates&#1111;'c']);
echo $rates&#1111;$index]; // no output but an 'undefined index' warning
http://www.php.net/manual/en/language.types.array.php
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Thank you very much Volka, I now know where to start.
Post Reply