tags:

views:

86

answers:

2

I'm trying to count how many times a certain article has been graded for example how many times have users_articles_id 3 been graded by my members.

I'm also trying to count the points for a certain article for example users_articles_id 3 is related to the ratings database by its ratings_id the rating points should be a total of 13.

I was wonder if I was doing this right because to me it looks all wrong? I was hoping if some one can help me fix this? And where should my code go exactly?

I'm using PHP and MySQL?

Here is my MySQL tables

CREATE TABLE articles_grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
ratings_id INT UNSIGNED NOT NULL,
users_articles_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id)
);


CREATE TABLE ratings (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
points FLOAT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);



Database Input

articles_ratings

id  ratings_id  users_articles_id   user_id     date_created
1   3           2                   32          2010-01-13 02:22:51
2   1           3                   3           2010-01-13 02:23:58
3   2           3                   45          2010-01-13 02:24:45

ratings

id      points
1       10
2       3
3       5



Here is the PHP code I'm trying to fix.

// function to retrieve rating
function getRating(){   
    $sql1 = "SELECT COUNT(*) 
                      FROM articles_ratings 
                      WHERE users_articles_id = '$page'";

    $result = mysql_query($sql1);
    $total_ratings = mysql_fetch_array($result);

    $sql2 = "SELECT COUNT(*) 
                            FROM ratings 
                            JOIN ratings ON ratings.id = articles_ratings.ratings_id
                            WHERE articles_ratings.users_articles_id = '$page'";

    $result = mysql_query($sql2);
    $total_rating_points = mysql_fetch_array($result);
    if(!empty($total_rating_points) && !empty($total_ratings)){
    // set the width of star for the star rating
    $rating = (round($total_rating_points / $total_ratings,1)) * 10; 
    echo $rating;
    } else {
        $rating = 100; 
        echo $rating;
    }
}
A: 

What's the question again? It's unclear to me

Timbop
read the first 2 sentences that is what i want to do.
iNeEdHeLp
+1  A: 

Well I think there are several issues here.

1) You are defining a table called articles_grades, not articles_ratings, as in your code. 2) Why do articles_grades and ratings need to be in separate tables? There is a one-to-one correspondence between those tables. 3) You need to do sum(points) in your second query. 4) You can combine both queries into a single query.

Here's how I would do it if you don't change the schema:

<?php

mysql_connect('localhost','root','fake123123');
mysql_select_db('test');

$result = mysql_query('SELECT users_articles_id,count(*),sum(r.points)
 FROM articles_grades ag,ratings r 
 WHERE ag.ratings_id = r.id
GROUP BY users_articles_id');

if (!$result)
  die('invalid');
else{

  echo '<table><tr><th>Article Id</th><th>#Ratings</th><th>#Points</th></tr>';
  $a = mysql_fetch_row($result);
  while($a){
    echo '<tr><td>'.implode('</td><td>',$a).'</td></tr>';

    $a = mysql_fetch_row($result);
  }
  echo '</table>';

}

?>

You can run this as a CGI script. It should return a table of the results.

Let me know if this helps.

Jieren