Hi, I have this PHP/MYSQL code which returns records from a table ordered by their ratings, from highest rated to lowest rated:
<table width="95%">
<tr>
<?php
if (isset($_GET['p'])) {
$current_page = $_GET['p'];
} else {
$current_page = 1;
}
$cur_category = $_GET['category'];
$jokes_per_page = 40;
$offset = ($current_page - 1) * $jokes_per_page;
$result = mysql_query("
select jokedata.id as joke_id, jokedata.datesubmitted as datesubmitted,
jokedata.joketitle as joke_title, sum(ratings.rating)/count(ratings.rating) as average
from jokedata inner join ratings
on ratings.content_type = 'joke' and ratings.relative_id = jokedata.id
WHERE jokecategory = '$cur_category'
group by jokedata.id
order by average desc
limit $offset, $jokes_per_page
");
$cell = 1;
while ($row = mysql_fetch_array($result)) {
if ($cell == 5) {
echo "</tr><tr class=\"rowpadding\"><td></td></tr><tr>";
$cell = 1;
}
$joke_id = $row['joke_id'];
$joke_title = $row['joke_title'];
$joke_average = round($row['average'], 2);
echo "<td><strong><a class=\"joke_a\" href=\"viewjoke.php?id=$joke_id\">$joke_title</a></strong> -average rating $joke_average.</td>";
$cell++;
}
?>
</tr>
<tr class="rowpadding"><td></td></tr>
</table>
It works perfectly but there is one problem - if an item does not have at least one rating, it will not be selected by the query at all!
How can I remedy this? Thanks.