fwapah Posted March 18, 2008 Share Posted March 18, 2008 i have a table that has multiple reviews tied to multiple reviewers. the columns for each row are reviewer, product id, product score. ed jones reviewed product 1 and product 2, and shirley smith reviewed product 2 and product 3. each reviewer/review combo has its own row. my question is, how do i display Product 1, Score Jones Product 2, Score Jones, Score Smith Product 3, Score Smith I am not sure how I can get the scores from each row to correlate with the product in one row. thanks so much for your help.. can't quite wrap my head around this one. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted March 18, 2008 Share Posted March 18, 2008 "GROUP BY product_id" maybe? I would open your phpMyAdmin or something and run test queries directly on the database to see what you get back. Quote Link to comment Share on other sites More sharing options...
fwapah Posted March 18, 2008 Author Share Posted March 18, 2008 Sorry, don't think I was clear on the first post. I have a table, reviews, that looks like this Reviewer Prod Num Score Jones12.1 Jones21.6 Smith21.5 Smith32.7 I'm trying to get it to output this: Prod Num Reviewer1 Score Reviewer2 Score 1Jones2.1 2Jones1.6Smith1.5 3Smith2.7 Thanks again for the help!! Quote Link to comment Share on other sites More sharing options...
fwapah Posted March 18, 2008 Author Share Posted March 18, 2008 I'm sure this is incredibly simple for anyone above basic php knowledge.. I've tried dozens of codes and nothing is working. Please have pity on this fool. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 18, 2008 Share Posted March 18, 2008 SELECT prod_num, GROUP_CONCAT(Reviewer, ' ', Score SEPARATOR ', ') as scores FROM reviews GROUP BY prod_num Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 18, 2008 Share Posted March 18, 2008 i guess i would query the database like this: SELECT * FROM reviews ORDER BY Reviewer, Prod; Then I would loop over the records, creating an array of arrays, $bigArray, with keys Reviewer and values an array for each reviewer, using the product number as key and score as value. $sql = "SELECT * FROM reviews ORDER BY Reviewer, Prod"; $result = mysql_query($sql) or die(mysql_error()); $bigArray = array(); $current_reviewer = ""; while (list($reviewer, $prodnum, $score) = mysql_fetch_row($result) { if ($reviewer != $current_reviewer) { if ($current_reviewer > "") { $bigArray["$current_reviewer"] = $reviewer_array; } $reviewer_array = array(); $current_reviewer = $reviewer; } $reviewer_array[$prodnum] = $score; } Then I would loop over the stored results... for ($i=0;$i<$number_of_products;$i++) { foreach ($bigArray AS $reviewer_name=>$prod_scores) { echo "$i $reviewer_name $prod_scores[$i] <BR>"; } } or something like that... yikes, barand has a possibly much simpler solution above me. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.