phatgreenbuds Posted January 9, 2007 Share Posted January 9, 2007 Ok so another hurdle. Reporting on a database has thrown me for a loop. Lets say I want to count the number of specific entries and display the count in report. for example:5 red apples6 green apples where there are 5 unique rows that include "red" and "apples" and 6 unigue rows that include "green" and "apples".I think i know the query part but the output is whats confusing me.[code]$result = mysql_query("SELECT count(*) FROM mydb GROUP BY color AND catagory");$num_rows = mysql_num_rows($result);//Now how do you display it?[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/ Share on other sites More sharing options...
taith Posted January 9, 2007 Share Posted January 9, 2007 the mysql_num_rows() shows you how many rows it returned...[code]$result = mysql_query("SELECT count(color) FROM mydb GROUP BY color AND catagory");$row = mysql_fetch_array($result);print_r($row); #depends on your database which field they'll go into :-)[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/#findComment-156784 Share on other sites More sharing options...
phatgreenbuds Posted January 9, 2007 Author Share Posted January 9, 2007 so where does the mysql_num_rows() fit in? Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/#findComment-156790 Share on other sites More sharing options...
craygo Posted January 9, 2007 Share Posted January 9, 2007 So do you want to just group items and output them with a count?? you are not just looking for green and red apples, correct??Ray Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/#findComment-156796 Share on other sites More sharing options...
phatgreenbuds Posted January 9, 2007 Author Share Posted January 9, 2007 I want to catagorize the unique entries where the uniqueness is keyed by two specific fields; color and catagory. Then output that to the screen with the total count of the rows matching."$count $color $catagory" is the expected output. Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/#findComment-156799 Share on other sites More sharing options...
craygo Posted January 9, 2007 Share Posted January 9, 2007 Here is something. Change table and field names to fit your table[code]<?php$table = "items";// Query database$sql = "SELECT count(ITEM) AS total, CATAGORY, ITEM, QTY_REQ, STILL_NEEDS FROM ".$table." ";$sql .= "GROUP BY CATAGORY, ITEM"; $res = mysql_query($sql); $num_rows = mysql_num_rows($res);if($num_rows > 1){ while ($rows = mysql_fetch_assoc($res)){// Print Database Detailsecho $rows['total']." -- ".$rows['ITEM']." -- ".$rows['CATAGORY']."<br />";}} else {// Print No data messageprint '<strong>NO ITEMS!!</strong>';}?>[/code]I have a table I use for testing the ITEM is the color for your tableRay Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/#findComment-156820 Share on other sites More sharing options...
phatgreenbuds Posted January 9, 2007 Author Share Posted January 9, 2007 well now that works...I wish i understood it...I will have to do some reading to understand what you have done here. Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/#findComment-156826 Share on other sites More sharing options...
craygo Posted January 9, 2007 Share Posted January 9, 2007 One problem you had with your query is that you used AND in your GROUP BY clause. The GROUP BY is seperated with comma's. Only the WHERE clause uses AND.Ray Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/#findComment-156829 Share on other sites More sharing options...
phatgreenbuds Posted January 9, 2007 Author Share Posted January 9, 2007 ok so I am playing around with this a little more and its starting to make sense. I am curious tho about the period in this line before the = sign. What does it do?$sql .= "GROUP BY CATAGORY, ITEM"; Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/#findComment-156834 Share on other sites More sharing options...
taith Posted January 10, 2007 Share Posted January 10, 2007 .= just adds the string onto the end of a preexisting string[code]<?$str='hello';$str.='world';echo $str; #hello world?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/#findComment-157238 Share on other sites More sharing options...
phatgreenbuds Posted January 10, 2007 Author Share Posted January 10, 2007 ah so its just like a kind of append type thing...Well your code cleared up some all of my confusion so thanks for the help. This forum has yet to let me down. Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/#findComment-157368 Share on other sites More sharing options...
HuggieBear Posted January 10, 2007 Share Posted January 10, 2007 Check out the first paragraph on this page: [url=http://uk.php.net/manual/en/language.operators.string.php]String Operators[/url]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33499-reporting/#findComment-157376 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.