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.

My shortened code is as follows:


<?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."%";

?>

The percentage shows in the echo but the total is wrong at 0% - 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.

    First:

    Wrong

    $percentage = ($point*$maximumPoints)/100;

    Right

    $percentage = ($point*100)/$maximumPoints;

    This time its the same, but when $maximumPoints isnt 100 the first one is wrong 🙂

    Then to the case...
    I guess you have checked that you really go in to the if-clauses? If not try to 'echo' something in them.

      First of all:

      ...........LIMIT 1

      You're only going to go through the loop once, so there's no point making a loop out of it (loops are for doing something maybe more than once).

      Second, the calculation could have been done in the query, but maybe you're doing other things with the same fields further down.

      Third, the calculation can be simplified because all the percentages are precalculated, then you undo the percentage scaling, then you rescale it back to being a percentage:

      $row = mysql_fetch_assoc($result);
      
      $maximumPoints  = 5;
      $point = 0;
      
      if($row['title'] != '')
      	$point++;
      
      if($row['name'] != '')
      	$point++;
      
      if($row['surname'] != '')
      	$point++;
      
      if($row['identityno'] != '')
      	$point++;
      
      if($row['gender'] != '')
      	$point++;
      
      $percentage = ($point/$maximumPoints)*100;
      echo $percentage."%";
      

      (at which point you could use PHP's type juggling and write [font=monospace]$point += ($row['gender'] != '');[/font] instead of the if statement).

      But more to the point:

      AshleighCo wrote:

      The percentage shows in the echo but the total is wrong at 0% - 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.

      So you're showing us the bit that works but you haven't shown us the bit that isn't working? What's this "'addition' part of the percentage calculation"?

        Write a Reply...