Forum Moderators: coopster

Message Too Old, No Replies

Find and count unique values in array

         

Kallym

8:17 pm on Mar 2, 2010 (gmt 0)

10+ Year Member



Hello, I have a multi dimensional array of $products[] information that is the result of a db query.

example: $products[1] = array( category => "widgets", name => "Widget 1", price => "1.00", etc., etc.);

The $products array has approx. 60 items in 9 categories.

I output the list to three columns. Currently I am counting the rows in the array and while looping through, start a new column when the count reaches the 1/3 mark.

But, I need to change that to start a new column, only after the category is ended. So I don't have orphan products at the top of the new column. To do this I need the category count as I loop through.

I know I could do a separate data base query and count the distinct categories in the table, but rather than doing a new query what would be the best way to do this?

I'm thinking there should be a way to count the distinct categories in the result, rather than a new db query, but have been struggling with more than a day on this, using different array functions.

Should I just do another db query or is there a simple solution that reuses the current result?

Thank You!

rocknbil

8:38 pm on Mar 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IMO the best way to do this is no not store them in an array. When you store in an array, you take up memory, which is considerably larger than the memory used by a scalar for the same amount of actual data. As your products base gets large, this could get, well, insane. :-)

Also as your products base grows, you will have to do something about pagination and this is going to get unnecessarily complicated. The argument against both of these very well may be "the products will never grow to more than X" but it's not a great approach.

I suggest doing all this at the time you make the query, within the while loop. Create a "marker" variable in your while loop for when the category has changed. So "if the category from this row is different than the current category OR the length of this category has exceeded my specified column length, start a new column." Something like that.

This will require you do a count(*) first rather than use the mysql_num_rows, but that count(*) value will become very useful as your products base grows - your normal select will contain a limit clause.

Kallym

10:18 pm on Mar 2, 2010 (gmt 0)

10+ Year Member



IMO the best way to do this is no not store them in an array.

I understand your concerns, but I am doing this within the context of Joomla and I am dealing with an array returned by a function in the core program and not a query specifically for this page. If, for this client, the list ever gets out of hand we will just come up with another solution. Right now the output is just a simple list of categories with products names under each category. The product names link to the detail pages and there is room on the page to grow.

I suggest doing all this at the time you make the query, within the while loop. Create a "marker" variable in your while loop for when the category has changed. So "if the category from this row is different than the current category OR the length of this category has exceeded my specified column length, start a new column." Something like that.


I tried that before I made the post, but it was not giving me "pretty" results. I really need the category count so that I can break specifically where desired. In your scenario (assuming there are 60 products and 9 categories), if my row count has not yet reached 20, but I already have three complete categories in that column, it will create a 4th category, thus leaving the other columns out of balance.

This will require you do a count(*) first rather than use the mysql_num_rows, but that count(*) value will become very useful as your products base grows - your normal select will contain a limit clause.

I use pagination on many sites, and appreciate your looking forward, but, in this case I need a simple fix for this client and really just need to know if there is an easy way to count the distinct categories in a result set.

Here is something I can do:
$old_category_id = 0;
$count_category = 0;
foreach( $categories as $category ) {
if($category["category_id"] != $old_category_id) {
$count_category++;
$old_category_id = $category["category_id"];
}}
//echo $count_category;

Is this a good way to do it under the circumstances?

Thanks for your quick reply. It is really appreciated!

Kallym

3:37 am on Mar 3, 2010 (gmt 0)

10+ Year Member



Okay, I feel dumb. There was a global variable with the category count. :-)

My code worked, but it ended up I didn't need it.