mrt003003 Posted May 1, 2011 Share Posted May 1, 2011 Hi there i need to make an array with the result from a query (numbers 1-4) I need to calculate how many occurances of each 1-4 number is in the table. Im new to this and havent much experience with arrays but ive managed to create an array and echo the results: 2 Class 3 Class 1 Class 3 Class 3 Class 2 Class 2 Class 2 Class So i need to somehow Add the occurance of them and echo the results e.g. 4 Class 2, 1 Class 1, 3 Class 3. If someone could please point me in the right direct that would be great. $colname_Recordset1 = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query = sprintf("SELECT Class FROM ships WHERE PlayerName = %s", GetSQLValueString($colname_Recordset1, "text")); $result = mysql_query($query, $swb) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['Class']. " Class "; echo "<br />"; } mysql_free_result($result);?>; Thank You Quote Link to comment https://forums.phpfreaks.com/topic/235232-array-from-a-query/ Share on other sites More sharing options...
teddyb Posted May 1, 2011 Share Posted May 1, 2011 Something like this? <?php $colname_Recordset1 = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query = sprintf("SELECT Class FROM ships WHERE PlayerName = %s", GetSQLValueString($colname_Recordset1, "text")); $result = mysql_query($query, $swb) or die(mysql_error()); $number_of_results = array(); while($row = mysql_fetch_array($result)){ if (isset($number_of_results[$row['class']])) { $number_of_results[$row['class']]++; } else { $number_of_results[$row['class']] = 1; } } foreach ($number_of_results as $class => $number_of_class) { echo $value . " Class " . $class; echo "<br />"; } mysql_free_result($result);?>; Quote Link to comment https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1208845 Share on other sites More sharing options...
mrt003003 Posted May 1, 2011 Author Share Posted May 1, 2011 Hi there thanks for the reply, Ive used: mysql_select_db($database_swb, $swb); $query = sprintf("SELECT Class FROM ships WHERE PlayerName = %s", GetSQLValueString($colname_Recordset1, "text")); $result = mysql_query($query, $swb) or die(mysql_error()); while($row = mysql_fetch_array($result)){ if (isset($number_of_results[$row['Class']])) { $number_of_results[$row['Class']]++; } else { $number_of_results[$row['Class']] = 1; } } foreach ($number_of_results as $class => $number_of_class) { echo $value . " Class " . $class; echo "<br />"; } mysql_free_result($result);?>; Just changing the class to Class... However i get a notice error: Notice: Undefined variable: value in C:\wamp\www\SWB\test1.php on line 55. Other than that Class 2, Class 3 and Class 1 is outputted, there no Class 4 and also it doesnt say how many occurances of the Classes there are. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1208980 Share on other sites More sharing options...
mrt003003 Posted May 1, 2011 Author Share Posted May 1, 2011 I guess im clucthing at straws here but if i change the line: echo $value . " Class " . $class; to: echo $result . " Class " . $class; This is whats outputted: Resource id #4 Class 2 Resource id #4 Class 3 Resource id #4 Class 1 Somethings not right Quote Link to comment https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1208991 Share on other sites More sharing options...
Pikachu2000 Posted May 1, 2011 Share Posted May 1, 2011 Is this what you mean? "SELECT COUNT(Class) AS c, Class FROM ships WHERE PlayerName = '" . mysql_real_escape_string($colname_Recordset1) . "'" GROUP BY class ORDER BY c DESC Quote Link to comment https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1208995 Share on other sites More sharing options...
mrt003003 Posted May 1, 2011 Author Share Posted May 1, 2011 Hi there im not 100% sure because i cant get your Select code to work without error: ( ! ) Parse error: syntax error, unexpected T_STRING in C:\wamp\www\SWB\test1.php on line 39 In the Class field of my table there are 4 occurances of 2, 3 occurances of 3 and 1 occurance of 1. I just want to output what they are and how many of them there are. Is this possible? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1209005 Share on other sites More sharing options...
teddyb Posted May 1, 2011 Share Posted May 1, 2011 woops sorry, i renamed a variable and forgot to rename it somewhere else <?phpforeach ($number_of_results as $class => $number_of_class) { echo $value . " Class " . $class; echo "<br />"; } Should be <php foreach ($number_of_results as $class => $number_of_class) { echo $number_of_class . " Class " . $class; echo "<br />"; } i initially had the foreach as $key => $value but decided they were terrible variable names Quote Link to comment https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1209011 Share on other sites More sharing options...
mrt003003 Posted May 1, 2011 Author Share Posted May 1, 2011 Yes thats it brilliant! Thank you very much indeed, this is the first time ive worked with arrays. Quote Link to comment https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1209015 Share on other sites More sharing options...
Pikachu2000 Posted May 1, 2011 Share Posted May 1, 2011 Hi there im not 100% sure because i cant get your Select code to work without error: ( ! ) Parse error: syntax error, unexpected T_STRING in C:\wamp\www\SWB\test1.php on line 39 In the Class field of my table there are 4 occurances of 2, 3 occurances of 3 and 1 occurance of 1. I just want to output what they are and how many of them there are. Is this possible? Thanks I missed a quote at the end. All you needed was to add it. Quote Link to comment https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1209020 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.