Jump to content

PHP SQL User Profile Completeness calculation not working


AshleighCo

Recommended Posts

I'm trying to show a profile completeness bar on the users account and the progress bar is showing but it's not adding the number values in order to calculate the percentage of completed fields ie: if($row['title'] != '') $completedTitle = 20;

My shortened code is as follows:

<?php
$result
= mysql_query("SELECT title,name,surname,identityno,gender FROM cic_candidates WHERE id='$id' LIMIT 1");

while($row = mysql_fetch_assoc($result))

$maximumPoints = 100;

{
if($row['title'] != '')
$completedTitle = 20;

if($row['name'] != '')
$completedName = 20;

if($row['surname'] != '')
$completedSurname = 20;

if($row['identityno'] != '')
$dcompletedIdentityno = 20;

if($row['gender'] != '')
$completedGender = 20;

}

$percentage = ($completedTitle+$completedName+$completedSurname+$completedIdentityno+$completedGender)*$maximumPoints/100;

echo "".$percentage."%";

?>

The percentage shows in the echo but the total is wrong - it's not taking the values of 20 points for each field that is completed and including them in the "addition" part of the percentage calculation. Please can you tell me where I'm going wrong - I've been trying to figure this out for 4 days and have googled this and read over 2000 forums but can't find the answer. Any help would be greatly appreciated.

UPDATED CODE: but still showing 0% (no errors)

 

<?php

$result = mysql_query("SELECT title,name,surname,identityno,gender FROM cic_candidates WHERE id='{$id}' LIMIT 1");

if (!$result) {
echo "Could not successfully run query " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while($row = mysql_fetch_assoc($result))

$maximumPoints  = 100;
$point = 0;
 {
if($row['title'] != '')
$point+=20;

if($row['name'] != '')
$point+=20;

if($row['surname'] != '')
$point+=20;

if($row['identityno'] != '')
$point+=20;

if($row['gender'] != '')
$point+=20;

 }

$percentage = ($point*$maximumPoints)/100;
echo $percentage."%";

?>

Why are you using a while() loop when there is only one record? It doesn't have a code block associated with it - but that shouldn't cause a problem in how you are using it. It just looks very odd since it isn't needed. Have you output the values from the query? Could be they are all empty. Also, the calculation will work only with that specific max value of 100. That is the wrong way to calculate the percentage.

 

percent x totalvalue = percentvalue

 

You have the totalvalue and the percent value so you need to calculate the percentage like this

 

percent = percentvlaue / totalvalue

i.e. percent = 100 / user points

 

Try this

 

<?php
 
$query = "SELECT title, name, surname, identityno, gender
          FROM cic_candidates
          WHERE id='{$id}'
          LIMIT 1";
$result = mysql_query($query);
if (!$result)
{
    die("Could not successfully run query " . mysql_error());
}
if (!mysql_num_rows($result))
{
    die("No rows found, nothing to print so am exiting");
}
 
 
$maximumPoints = 100;
$user = mysql_fetch_assoc($result));
 
$user_points = 0;
$user_points += !empty($user['title'])      ? 20 : 0;
$user_points += !empty($user['name'])       ? 20 : 0;
$user_points += !empty($user['surname'])    ? 20 : 0;
$user_points += !empty($user['identityno']) ? 20 : 0;
$user_points += !empty($user['gender'])     ? 20 : 0;
 
$percentage = $user_points / $maximumPoints * 100;
echo "{$percentage}%";
 
//Debug
echo "<br><pre>";
var_dump($user);
echo "</pre>";
 
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.