Re: newbie: dynamically building associative arrays
| From: | 1LT John W. Holmes | Date: | Wed, 15 May 2002 18:36:25 +0000 |
| Subject: | Re: newbie: dynamically building associative arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-97769@lists.php.net to get a copy of this message | ||
Heh...you have to learn some SQL!!
Try this query:
SELECT term, count(*) AS cnt FROM table GROUP BY term;
---John Holmes...
----- Original Message -----
From: "ROBERT MCPEAK" <RMCPEAK@jhuccp.org>
To: <php-general@lists.php.net>
Sent: Wednesday, May 15, 2002 2:16 PM
Subject: [PHP] newbie: dynamically building associative arrays
> I'm trying to build what I think is an associative array from a set of
> mySQL results.
>
> It looks like this:
>
> //First, a mySQL results loop for a select statement return unique
> occurrences of data in the field "term":
>
> for ($i=0; $i <$num_results; $i++)
> {
> $row = mysql_fetch_array($result);
>
> echo $row["term"];
>
> //Then, within the loop, another search to find the number of
> occurrences //of that result
>
> $this_term[$i]=$row["term"];
> $this_query = "select term from photoqueries where term = '$this_term'
> order by term";
> $this_result = mysql_query($this_query);
> $this_num_results[$i] = mysql_num_rows($this_result);
>
> }
>
> //so, now I have 2 vars corresponding to each "term" value from the
> table: $this_term[i], and $this_num_results[i].
>
> That's where I get stuck! How do I stick all of my $this_term and
> $this_num_results values together in an array. Once that is
> accomplished, how do I then sort the array (descending) by the
> $this_num_results values?
>
> In short, what I'm trying to do is display a list of all "term" values
> and the number of times that data appears in the table. If I had a
> table "names" containg these values:
>
> bob
> bob
> joe
> sue
> fred
> bob
> sue
> jane
>
> The display I'm trying to build would look like this:
>
> bob 3
> sue 2
> jane 1
> joe 1
>
> Any help is greatly appreciated, sincerely!
>
> Thanks!
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>