Reputation: 15734
total_starsHere is the code, it is the best explanation of what I am trying to do:
$total_stars = array();
$query = "SELECT items_id FROM items";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$item_id = $row['items_id'];
$sql = "Select Count(item_id) FROM ratings WHERE item_id = '{$item_id}'";
$result = mysql_query($sql);
$total_stars[] = mysql_fetch_array($result);
}
// To see the output by entering the URL I want to print_r
print_r($total_stars);
// In the end, I am trying to JSON encode this and will later output in Java for Android
print(json_encode($total_stars));
(So as not to confuse, the items_id is in items table, item_id (without an 's') is in ratings table)
Now in plain English:
I am looping through each row in the items table. As I do that, in each loop I am referring to another table called "Ratings", and getting a count of how many ratings for that item.
For example I want an array to look like (65, 43, 55, 43, etc). Sample numbers of course. But each represent the total amount of ratings for each item in a table. How can I get print__r to display the results of the SQL statement inside the loop?
UPDATED CODE TRYING TO USE JOIN:
$query = "SELECT items_id FROM items";
$q = 'SELECT
items.items_id,
COUNT(ratings.item_id) AS rate
FROM `items`
LEFT JOIN ratings ON (ratings.item_id = items.items_id)
GROUP BY items_id';
$result = mysql_query($q);
while ($row = mysql_fetch_array($result)) {
$total_stars = mysql_fetch_array($result);
}
print_r($total_stars);
print_r outputs this: Array ( [0] => 783 [items_id] => 783 [1] => 0 [rate] => 0 )
Upvotes: 0
Views: 3706
Reputation: 7229
Why not use a JOIN as suggested in the comments? If you want an array with all the ratings of the items, then this is a way (didn't test the PHP db code, but i did test the query).
$total_stars = array();
$q = 'SELECT
items.item_id,
COUNT(ratings.item_id) AS rate
FROM `items`
LEFT JOIN ratings ON (ratings.item_id = items.item_id)
GROUP BY item_id';
$result = mysql_query($q);
$total_stars = mysql_fetch_array($result);
You should use MYSQLi or PDO_MYSQL instead. more info MYSQLi, more info PDO
And here you can find more info about JOINS: Info about MYSQL joins
Upvotes: 1
Reputation: 3034
$total_stars = array();
while ($row = mysql_fetch_array($result))
{
$item_id = $row['items_id'];
$sql = "Select Count(item_id) FROM ratings WHERE item_id = '{$item_id}'";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$total_stars[] = row[0];
}
This should do it.
Don't forget that mysql_fetch_array returns an array with index for the columns. So mysql_fetch_array($result) would be an array that you want the first element in (result from MySQL function COUNT()), not the count itself.
But as I said in my comment, use a join query instead. You will reduce the amount of queries to the mysql server from the number of rows in your first query+1 (which could be alot!) to just one, which is a huge improvement.
Update: Here's how to do it with a join query (query by Sven):
$q = 'SELECT
items.items_id,
COUNT(ratings.item_id) AS rate
FROM `items`
LEFT JOIN ratings ON (ratings.item_id = items.items_id)
GROUP BY items_id';
$result = mysql_query($q);
$total_stars = array();
while ($row = mysql_fetch_array($result))
{
$total_stars[] = $row['rate']; // Stars count is stored in $row['rate']
echo "Item id ".$row['items_id']." has rating ".$row['rate'];
}
Upvotes: 3