How To: Add Random Star Ratings w/o Reviews
Posted: Mon Jan 02, 2012 8:57 pm
If you'd like to play around with adding random star ratings for your products without the need to create reviews as well, I've come up with the following script. I really only wanted to add star ratings (like how many carts will allow customers to just rate the product without forcing a review), but it looked like I was going to have to add bogus reviews as well. After struggling with automating that, I cleared the review table to start over, only to find the star ratings were still there. So I tweaked the script to first add bogus reviews to isc_reviews, add the rating details to isc_products based on what's in isc_reviews and then delete all the records in isc_reviews. So you are just left with ratings for all of your products. You can tweak it to only work on a few products or all of them. I tested it on a db with 7k products (added a rating to all of them).
*It's up to you how you use this. It has a few more uses than adding your own star ratings to a live site.
*It's up to you how you use this. It has a few more uses than adding your own star ratings to a live site.
Code: Select all
<?php
// Make a MySQL Connection
mysql_connect("localhost", "your db username", "your db password") or die(mysql_error());
mysql_select_db("your db name") or die(mysql_error());
//grab some product ids from isc_products <-- you could probably limit how many products here
$result = mysql_query("SELECT productid FROM isc_products WHERE prodvisible = '1'")
or die(mysql_error());
// for each of those product ids, run the function below
while($row = mysql_fetch_array( $result )) {
$prodid = $row['productid'];
RandomRatings($prodid);
}
// This query runs last and removes everything from isc_reviews
$result4 = mysql_query("DELETE FROM isc_reviews WHERE revdate = '0'")
or die(mysql_error());
// This function inserts sample reviews in isc_reviews with a random rating between 3-5 and already approved. Then it inserts rating data into isc_products
function RandomRatings ($prodid){
$ranrate = rand(3,5);
$result1 = mysql_query("INSERT INTO isc_reviews SET revproductid = '$prodid', revrating = '$ranrate', revstatus = '1'")
or die(mysql_error());
$result2 = mysql_query("SELECT revrating FROM isc_reviews WHERE revproductid = '$prodid'")
or die(mysql_error());
while($row2 = mysql_fetch_array( $result2 )) {
$result3 = mysql_query("UPDATE isc_products SET prodnumratings = prodnumratings+1, prodratingtotal = prodratingtotal+'".(int)$row2['revrating']."' WHERE productid = '$prodid'")
or die(mysql_error());
}
}
?>