<?
totals 
= array();
foreach(
$rowCollection as $row) {
    
$totals[$row["columnToCount"]] = ($totals["columnToCount"]?++$totals["columnToCount"]:1);
}

foreach(
$totals as $column => $total) {
    print 
"$column = $total\n";
}
?>
<?
totals 
= array();
foreach(
$rowCollection as $row) {
    
$totals[$row["columnToCount"]] = ($totals["columnToCount"]?++$totals["columnToCount"]:1);
}

foreach(
$totals as $column => $total) {
    print 
"$column = $total\n";
}
?>
<?
$q 
mysql_query("SELECT myCol, COUNT(myCol) FROM myTable GROUP BY myCol");  
while (
$r mysql_fetch_row($q)) ${"num{$r[0]}"} = $r[1];
?>
<?
$q 
mysql_query("SELECT myCol, COUNT(myCol) FROM myTable GROUP BY myCol");  
while (
$r mysql_fetch_row($q)) ${"num{$r[0]}"} = $r[1];
?>
PHP/MySQL question - TranceAddict Forums - Chill Out Room
return to tranceaddict TranceAddict Forums Archive > Main Forums > Chill Out Room

 
PHP/MySQL question
View this Thread in Original format
nefardec
what's the best way to do the following:

I want to find out how many kinds of different values there are in a table column, then find out the number of re-occurences there are for each of these different kinds.

for example

if I select the following column data:

Apple
Apple
Strawberry
Apple
Apple
Orange
Orange
Strawberry
Strawberry
Orange
Banana
Banana
Grape

I want to create a variable for each type of entries and set it equal to how many of each there are of that type - something like:

$numApple = 4
$numStrawberry = 3
$numOrange = 3
$numBanana = 2
$numGrape = 1
.....
$num_____ = x
echosystm
Do you know the potential values beforehand, or do you need to select them too? Do you know the number of unique values beforehand?
Joss Weatherby
Assuming you have a collection of rows from some place, or if you are using the old procedural MySQL function it would be a while and using mysql_fetch_array($mysqlCid, MYSQL_ASSOC) or something like that.

PHP:



Just use the column value as the key in an associative array and then increment the value in it, making sure to set it to 1 initially if it is undefined.
Scottaculous
SQL:

SELECT myCol, COUNT(myCol) FROM myTable GROUP BY myCol
LeopoldStotch
This

quote:
Originally posted by Scottaculous
SQL:

SELECT myCol, COUNT(myCol) FROM myTable GROUP BY myCol


+


quote:
Originally posted by Joss Weatherby

PHP:






= the answer.


The key to this is using the map to store your values. :p
xtr3m
To generate variables named exactly the way you wanted them:

PHP:

nefardec
cool guys,

thanks for your help, i came up with something that works really well.

i just got started on this stuff a few days ago and just didnt know the syntax for:

mysql - COUNT()/GROUP BY
php - ${"name{$variable}"}


using those two things I was able to put it all together. Basically I am making a social graph application and I am making the size of the graph nodes scale depending on how connected they are to one another.
PETRAN
Is this for your massive project nef? :toothless
nefardec
it is

learning a lot of new things for this

im also going to build my own adobe flex application for viewing the graph - right now I am just using a program i found that interprets special xml files that I am generating


with this fix, detroit and berlin are now looming large, like celestial bodies :p
Joss Weatherby
psh yea count would make it easier... im so used to not touching the queries though and just getting data collections.

Joss Weatherby
quote:
Originally posted by xtr3m
To generate variables named exactly the way you wanted them:

PHP:



Id advise against ever using column number result sets because if the result count ever changes then your code has to change as well.
CLICK TO RETURN TO TOP OF PAGE
 
Privacy Statement