Page 1 of 1

Getting sucked into a black hole

Posted: Wed Oct 09, 2002 7:55 pm
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.

Posted: Wed Oct 09, 2002 8:48 pm
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 ;)

Posted: Thu Oct 10, 2002 12:00 pm
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.

Posted: Thu Oct 10, 2002 12:54 pm
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

Posted: Thu Oct 10, 2002 3:05 pm
by evilmonkey
Thank you very much Volka, I now know where to start.