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


All times are GMT. The time now is 08:20 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.03676 seconds
  • Memory Usage 1,756KB
  • 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
  • (3)bbcode_php_printable
  • (4)bbcode_quote_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