vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Counting custom fields in PHP (https://vborg.vbsupport.ru/showthread.php?t=74174)

GrBear 01-12-2005 02:06 PM

Here's the solution I can up with that works.

PHP Code:

$array = array();
$dbquery $DB_site->query("SELECT field5 FROM " TABLE_PREFIX "userfield"); 

while (
$queryrow $DB_site->fetch_array($dbquery))
    {
        
$array[$queryrow['field5']]++;
    }
reset($array);
while (list(
$key$val) = each($array))
    {
                    
$f5stat .= $val " = " $key;
    } 

Thanks for all the help folks, it was appreciated!

rake 01-12-2005 06:36 PM

Keep in mind, you'll be running through a table that can be a couple of thousand rows large, depending on the number of members on your board..

You should use the solution provided by MarcoH64

GrBear 01-12-2005 09:26 PM

Good point. It would make sense that it would be quicker for the DB engine to tabulate the totals rather than have PHP parse each row individually from the DB.

I'll give it a shot.

Thanks!

GrBear 01-12-2005 11:20 PM

I had to modify my the code of course for the new SELECT, and by tweaking the SQL statement further, I was able offload the sorting to the DB since I wanted the results displayed by the highest total to the lowest.

PHP Code:

$dbquery $DB_site->query("SELECT field5, COUNT(field5) AS total FROM " TABLE_PREFIX "userfield GROUP BY field5 ORDER BY total DESC"); 

while (
$queryrow $DB_site->fetch_array($dbquery))
    {
        
$f5stat .= $queryrow['total'] . " " $queryrow['field5'];
    } 

There was a bit of extra html layout code in there as well, but for easier readability I left it out. :)

Much simpler execution to the problem. Again, many thanks! :up:

Marco van Herwaarden 01-13-2005 03:39 AM

Sometimes the solution to something that seems complicated can be real simple. :D

pshadow 02-06-2005 04:53 AM

this was great. I tried to do the same thing and it works perfect. thanks.

I'm wondering how to make 2 edits though:
1. exclude counting and displaying the empty fields. currently shows 500 blank.
2. not really related but, it displays whats in the field5 fine, how do I get the original field number from the select menus of the custom field? i.e. the ID that is used in the search => getall&field5=3 rather than getall&field5=orange

Andreas 02-06-2005 05:01 AM

1) WHERE field5 != ''
2) SELECT data from profilefield WHERE profilefieldid=5;
Then unserialize data and use the value returned by the other query as an index into this array.

pshadow 02-06-2005 05:38 AM

thanks. first part worked perfect. I'm new to the whole query stuff so not sure what part 2 entails with the unserialize and using other query. I'll have to play around and with that one.

thanks for the quick response

Andreas 02-06-2005 05:48 AM

PHP Code:

$field5 $DB_site->query_first("SELECT data FROM " TABLE_PREFIX "profilefield WHERE profilefieldid = 5");
$fieldvalues unserialize($field5['data']);

$dbquery $DB_site->query("SELECT field5, COUNT(field5) AS total FROM " TABLE_PREFIX "userfield WHERE field5 != '' GROUP BY field5 ORDER BY total DESC");

while (
$queryrow $DB_site->fetch_array($dbquery))
    {
        
$f5stat .= $queryrow['total'] . " " $fieldvalues[$queryrow['field5']];
    } 


pshadow 02-06-2005 06:36 AM

hmm.. doesn't seem to return anything? no errors though? just count number and blank. nothing for the fieldvalues.


All times are GMT. The time now is 10:10 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01187 seconds
  • Memory Usage 1,744KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete