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-11-2005 02:47 AM

Counting custom fields in PHP
 
Sorry, I seem to have left my PHP books at the office, but I had a quick question that someone should hopefully be able to answer.

I'm writing a little stat hack for my members that will count the number of users of various interests that I'll display in my Site Stats row of the main forum page.

My stats will be generated off the custom "field5" (listed in the DB as "field5"), which is a pulldown that lists 5 or 6 options, and the member can pick one. I don't care about usernames, etc. I just want raw stats. This is example code of what I wanted to use, but I'm not sure about the array stuff. I want the text key for the array to match the text of the option in the pulldown, and increment it if there's a match from the database row from my select statement.

Code:

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

while ($queryrow = $DB_site->fetch_array($dbquery))

{
    $field5stat[$queryrow['field5']]++
}

What I'm hoping that last line does is say, if field5 is 'blue', then $field5stat['blue'] should be incremented.

Will that work, or is my syntax of either the array or incrementing wrong?

Thanks for any assistant, my PHP coding skills are rather rusty as I haven't programmed in it for about 4 years *laughs*

sabret00the 01-11-2005 09:22 AM

your best bet would be to scrap the way you done it in order of something alot simpler

PHP Code:

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

while (
$queryrow $DB_site->fetch_array($dbquery))

{
    if (
$queryrow[field5] == "blue")
    {
        
$field5stat['bue']++
    }
    else if (
$queryrow[field5] == "red")
    {
        
$field5stat['red']++
    }
}
then echo template to echo values 

theirs propably parse errors but you seem competant enough to sort that out.

GrBear 01-11-2005 02:51 PM

Quote:

Originally Posted by sabret00the
your best bet would be to scrap the way you done it in order of something alot simpler

Other than one parse error, apparently my code worked. *laughs*

I get:

$field5stat[0] aka ['blue'] = 25
$field5stat[1] aka ['red'] = 5
...
$field5stat[7] aka ['orange'] = 10

count($field5stat) returns 8 (there are 8 indexes)

My problem now seems to be that I can't pull up the array values by the numerical key, only the textual key. I though arrays can have both the numerical AND textual key values. :disappointed:

Say I now wanted to loop through the array and spit out the textual key and the value (not the key#) to get a listing of something like:

blue 25
red 5
...
orange 10

I was trying to do a loop like:

PHP Code:

for ($i == 0$i count($field5stat); $i++)

{
       echo 
$field5stat[$i];  // echo just the value, not the key


I don't get anything back, which is why I'm guessing that you can't call back an array by both it's numerical index AND textual key name.

*sighs*

Dean C 01-11-2005 03:34 PM

[sql]SELECT COUNT(field5) AS total FROM userfield[/sql]

Run that query and see if it's what you want? Little confused as to what you're doing here :)

GrBear 01-11-2005 03:49 PM

Quote:

Originally Posted by Dean C
Run that query and see if it's what you want? Little confused as to what you're doing here :)

The select statement only returns the total of entries in the column.

I'll give an example of what I'm trying to do.

I have a custom field in the UCP, which one is "My favorite color is:" and there's a pull down of 8 options to choose from. (red, blue, green, etc..) This column is called field5.

I want to scan the column and display how many people like red, blue, green, etc.

The reason I didn't go with sabret00the's original suggestion was because then I'd have to change the code if I decided to add another option for the field (a new color).

Thanks for your suggestion though.

sabret00the 01-11-2005 04:18 PM

Quote:

Originally Posted by GrBear
My problem now seems to be that I can't pull up the array values by the numerical key, only the textual key. I though arrays can have both the numerical AND textual key values.

you can only pull the numerical key values if they array is generated via a query to the best of my knowledge, according to your above code you're setting the array manually.

Quote:

Originally Posted by GrBear
The reason I didn't go with sabret00the's original suggestion was because then I'd have to change the code if I decided to add another option for the field (a new color).

you could actually acheive that on the fly, but i was just looking for the simplest answers.

sabret00the 01-11-2005 04:28 PM

PHP Code:

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



while (
$queryrow $DB_site->fetch_array($dbquery))

{
    while (
$queryrow[field5] = $cats)
    {
        
$field5stat['field5']++
    }
}
then echo template to echo values 

i just woke up so i can't check the code but i think i done something like this in another script that came out looking a bit like this.

Marco van Herwaarden 01-11-2005 07:17 PM

What about a:
[sql]SELECT field5, COUNT(field5) FROM userfield GROUP BY field5;[/sql]

wouldn't that do the trick?

Andreas 01-11-2005 07:24 PM

MarcoH64 is right, basically this query should do the work.

But i'd change it a bit to make the results easier to use:

[sql]SELECT field5, COUNT(field5) AS total FROM userfield GROUP BY field5[/sql]

Marco van Herwaarden 01-11-2005 07:29 PM

sorry, yes you're right Kirby, should have added that. Bit easier to use on scripts :D

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 04:47 AM.

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.01150 seconds
  • Memory Usage 1,791KB
  • 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
  • (1)bbcode_code_printable
  • (6)bbcode_php_printable
  • (4)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (20)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
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete