Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2005
    Posts
    388
    Thanks
    0
    Thanked 0 Times in 0 Posts

    graphing survey results

    Howdy all,
    I am looking to put a survey(s) on my site, after a user votes, I would like to be able to pull the current results and then turn it into a graph, my question is - is there a way to count the # of fields in a database? Is this a MySQL function? Or PHP? anybody have any ideas on the best way to do this?

    I was thinking to count the # of fields, to determine the number of bars in the graph. Then add up the number of answers to get the upper limit of each bar.

    tia.

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not sure if you can do this directly through mysql, but one way to do it is to get the row with mysql_fetch_assoc which creates an array of that row data then use array_keys to make an array of all the keys in the row and count that, like so:

    PHP Code:
    $row mysql_fetch_assoc($result);
    $num_fields count(array_keys($row));
    echo 
    $num_fields

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    PHP Code:
    $sql "SELECT COUNT(*) AS count FROM table;";
    mysql_query($sql); 
    That does it in a mysql query (much quicker)

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Posts
    388
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks guys, but I figured out a way to do it like this.

    $columns = "SHOW COLUMNS FROM polls";

    $colres = mysql_query($columns);

    $numbars = mysql_num_rows($colres);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •