Hi all,
I have a farily simple table that I am doing a query (with a sum and count that was using a static list of items that will not work for me). Here is the original post.
What I am trying to do is re-use am SESSION array that has the item key and SKU (name). So I changed the table and stored the SKU so I can just to a match on it in my query instead of the ID (which was from the static list and would no longer work for me). So far so good.
No the issue is, I cannot get the query to show the items when I do a foreach loop correctly (it shows 9 of each instead of 1 of each and I cannot see why).
Here is a print_r of the $products:
$products[$key]
Array
(
[0] => ES
[4] => ED
)
The table structure is: (and the 2 items in this example)
RatingID --- BallotID --- ProductID --- SKU --- Rating --- Date
----1-------------18-----------0----ES-------6---------07-28-2008
----2-------------18-----------4----ED-------10-------07-28-2008
And the Code:
$Count = array();
foreach($products as $key => $value) {
mysql_select_db ($dbname, $conn) or die (mysql_error());
$query = "SELECT SUM(ratings2.Rating) AS sum,
COUNT(ratings2.Rating) AS count, ProductID as ID,
ratings2.SKU as Name FROM ratings2, products WHERE
ratings2.SKU='" . $value . "' GROUP BY products.ID";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
$Count = $row['count'];
$Sum = $row['sum'];
$Name = $row['Name'];
$ID = $row['RatingID'];
$Total = (round($Sum/$Count,1)/2);
$tut_rating = round($Sum/$Count,1);
echo $Name . '<br />';
echo 'Total Votes = ' . $Count . '<br>Rating = ' . $Total . ' out of 5 stars.<br />';
echo tut_stars_image($tut_rating) . '<br /><br />';
}
}
Like I said, I cannot see why it gives me 9 sets of results.
As always, I appreciate the help.
Thanks,
Don