Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2005, 02:47 AM
GrBear GrBear is offline
 
Join Date: Aug 2004
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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*
Reply With Quote
  #2  
Old 01-11-2005, 09:22 AM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #3  
Old 01-11-2005, 02:51 PM
GrBear GrBear is offline
 
Join Date: Aug 2004
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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*
Reply With Quote
  #4  
Old 01-11-2005, 03:34 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

[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
Reply With Quote
  #5  
Old 01-11-2005, 03:49 PM
GrBear GrBear is offline
 
Join Date: Aug 2004
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #6  
Old 01-11-2005, 04:18 PM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #7  
Old 01-11-2005, 04:28 PM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #8  
Old 01-11-2005, 07:17 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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

wouldn't that do the trick?
Reply With Quote
  #9  
Old 01-11-2005, 07:24 PM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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]
Reply With Quote
  #10  
Old 01-11-2005, 07:29 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

sorry, yes you're right Kirby, should have added that. Bit easier to use on scripts
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:23 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.04151 seconds
  • Memory Usage 2,269KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (3)bbcode_php
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete