PHP ve MySQL sorunları

1 Cevap php

Ben belli bir makale users_articles_id 3 Benim üyeleri tarafından derecelendirildi edilmiştir kaç kez örneğin değerlendirilir olmuştur kaç kez saymak çalışıyorum.

Ayrıca users_articles_id 3 ile ratings_id ile ratings veritabanı ile ilgilidir, örneğin bir ürün için belli bir kredi sayısı çalışıyorum puanınız toplam 13 olmalıdır.

Benim için hepsi yanlış görünüyor çünkü ben bu hakkı yapıyor olsaydı ben merak oldu? Bazı biri bana bunu düzeltmek yardımcı olabilir, ben umuyordum? Ve nerede benim kod tam olarak gitmeli?

PHP ve MySQL kullanarak ediyorum?

İşte benim MySQL masaları

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)
);



Veritabanı Girdi

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



İşte düzeltmek için çalışıyorum PHP kodudur.

// 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;
    }
}

1 Cevap

Peki ben birkaç konular burada olduğunu düşünüyorum.

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.

Burada şemasını değiştirmek yoksa ben bunu yapacağını nasıl:

<?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>';

}

?>

Sen bir CGI komut dosyası olarak çalıştırabilirsiniz. Bu sonuçlar bir tablo dönmek gerekir.

Bu yardımcı olur bana bildirin.