calculate percentage of fields with data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rosslad
Forum Newbie
Posts: 4
Joined: Mon Feb 11, 2008 3:09 pm

calculate percentage of fields with data

Post by rosslad »

Hi there,

I have said before that I am not really used to asking for help as I like to figure things out for myself but again here I am with another problem that has me :banghead:

I am trying to figure out how to calculate all the fields in a certain mysql table that has data from the overall fields.

so User Table has say 20 fields and the user with the id of 1 only has 10 of them with data in, I need to write a function that can easily count all fields and then count only the fields with data and display this as a percentage.

Can this even be done? I have tried to the best of my knowledge but come up empty 8O
If anyone can show me the way I would be 3 days worth of grateful to you :wink:

Ross
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: calculate percentage of fields with data

Post by McInfo »

By your definition, if a field is not a "field with data" do you mean it is NULL, an empty string, or either?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 3:32 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: calculate percentage of fields with data

Post by McInfo »

I think I misread your question. I wrote a script to retrieve a count of all of the cells in the table that are not null. After rereading your post, it looks like you just want to find the number of cells in a single row.

For the total number of fields returned in a result, use mysql_num_fields().

For the number of filled-in fields, loop through the result and use empty() to check the values.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 3:33 pm, edited 1 time in total.
rosslad
Forum Newbie
Posts: 4
Joined: Mon Feb 11, 2008 3:09 pm

Re: calculate percentage of fields with data

Post by rosslad »

Ok sorry for not explaining it properly...

Thanks for the tips, so I now have:

Code: Select all

 
$total_cnt = mysql_query("SELECT * FROM user_table WHERE id = '$userid'");
 
$cnts = mysql_num_fields($total_cnt);
 
        foreach ($cnts as $totals) {
 
        if (empty($totals)) {
$fields = 0;
} else {
$fields = 1;
}
}
$total_counts = $fields * 100 / $totals;
 
but its not showing any result, probably my code is flawed.
Any help with the above would be awesome thanks.

Ross
Last edited by Benjamin on Mon May 04, 2009 11:40 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: calculate percentage of fields with data

Post by McInfo »

$cnts is a number (or false), not an array. You need to loop through the result set returned by mysql_query() to find the number of empty fields for the row. The total number of fields is returned by mysql_num_fields().

Even better, read this:
PHP Manual: mysql_fetch_lengths().

Edit: This post was recovered from search engine cache.
Post Reply