Forum Moderators: open
I have a mysql database with a table containing reviews about products. Each review has a rating from 1 to 5.I want to choose 5 reviews randomly, and display them in a way that the top rated ones appear first.How should the query look like?
SELECT * FROM reviews WHERE product_id='$id'....(what's next?)
Thanks!
if you're using php, look at [php.net ]
I tried a different approach with the array_multisort command but I get a lot of errors.Here is my code:
$query= "SELECT ReviewSource,Date,Nick,Location,Rating,RevSummary,ReviewText FROM reviews WHERE ProductID='$id' AND ProductType='$product_type' ORDER BY ReviewSource,RAND() LIMIT 5";
}
$result = mysql_query($query) or die("display_db_query:" . mysql_error());
// Sort Reviews By Rating
$i=1;
while ($row_rev=mysql_fetch_assoc($result))
{
$data[$i]=$row_rev;
$i=$i+1;
}
foreach ($data as $key => $row) {
$Rating[$key] = $row['Rating'];
array_multisort($Rating,SORT_DESC,$data);
}
Why doesn't it work?
if your field is really called Rating, this might just work:
function cmp($a, $b)
{
if ($a['Rating'] == $b['Rating']) {
return 0;
}
return ($a['Rating'] < $b['Rating']) ? -1 : 1;
}usort($data, "cmp");