ikebaldo Posted June 9, 2008 Share Posted June 9, 2008 Hi, im new here and im a bit of a n00b at php . I am trying to count how many times a certain peice of data appears in a database. I have read some data into an array from an sql database, and it has been sorted by name alphabetically. I am attempting to use the following code to display how many of each item there are. $records =@mysql_fetch_array($query_countResults);//stores the data from the query in an array, sorted by name. $temp = 1; //keeps hold of the number of search items as it counts $check1 = 0; //stores the reference to the first items location in the array $check2 = 1; //stores the reference to the second items location in the array while($check2 <= $number_results) { if ($records[$check1] == $records[$check2]){ $temp = $temp + 1; }else{ echo $temp; $temp = 1; } $check1 = $check1 + 1; $check2 = $check2 + 1; } this code doesnt currently display items into a table, i am hoping to do that after i have ensured that the loops are working correctly. Please help... Quote Link to comment Share on other sites More sharing options...
moselkady Posted June 9, 2008 Share Posted June 9, 2008 I wonder if this helps you. If you want to count the number of times a value (for example name) appears you can use this SQL: SELECT `name`, COUNT(`name`) AS n FROM mytable GROUP BY `name` Quote Link to comment Share on other sites More sharing options...
ikebaldo Posted June 9, 2008 Author Share Posted June 9, 2008 Thanks for the idea, but im making a list of items with how many each appears in the database, so that wouldnt work. Thank you though Quote Link to comment Share on other sites More sharing options...
moselkady Posted June 9, 2008 Share Posted June 9, 2008 Maybe array-count-values() could help you. See http://us3.php.net/manual/en/function.array-count-values.php Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 9, 2008 Share Posted June 9, 2008 I wonder if this helps you. If you want to count the number of times a value (for example name) appears you can use this SQL: SELECT `name`, COUNT(`name`) AS n FROM mytable GROUP BY `name` Thanks for the idea, but im making a list of items with how many each appears in the database, so that wouldnt work. Thank you though What do you mean? That query format will return exactly what you just stated. Just modify the query for the item name and it will return a list of all the items and the number of times each item exists in the table. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 9, 2008 Share Posted June 9, 2008 Thanks for the idea, but im making a list of items with how many each appears in the database, so that wouldnt work. Thank you though I fail to understand, why ask for help then reject it? Just wastes our time. (your username is noted) Quote Link to comment Share on other sites More sharing options...
ikebaldo Posted June 10, 2008 Author Share Posted June 10, 2008 Hi, i'm sorry. I assumed that that SQL statement would only count how many items in total were in the array. I have run that query, and saved the results in the array $records. I have this code to output the results : $temp = 0; while($temp <= $number_results){ echo $records[$temp]; } It doesnt diplay any results at all. Please help, i will actually listen 100% this time!!!! Quote Link to comment Share on other sites More sharing options...
Barand Posted June 10, 2008 Share Posted June 10, 2008 you will need to substitute your table and column names: <?php $sql = "SELECT `name`, COUNT(*) as num FROM tablename GROUP BY `name`"; $res = mysql_query ($sql); while (list($name, $num) = mysql_fetch_row($res)) { echo "$name : $num <br/>"; } ?> Quote Link to comment Share on other sites More sharing options...
ikebaldo Posted June 10, 2008 Author Share Posted June 10, 2008 Absolutly Fantastic!!!!! Thank You very much! also, sorry again for not listening the first time...... it had been a bit of an early day and late night..... 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.