www.webdeveloper.com
Results 1 to 3 of 3

Thread: PHP SQL User Profile Completeness calculation not working

  1. #1
    Join Date
    May 2014
    Posts
    1

    PHP SQL User Profile Completeness calculation not working

    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.

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    862
    My guess is your lack of brackets on the while() loop. Well.. I see some brackets, but they are in the wrong place...
    PHP Code:
    $maximumPoints 100;
    $point 0;
    while(
    $row mysql_fetch_assoc($result)) {
        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."%"

    And for the sake of standards and all that jazz, don't use the original mysql functions in PHP (as denoted here). It's outdated, less secure and as of PHP 5.5 no longer supported (thus your code will break when your host/server updates PHP). Switch to mysqli (better, and here's a link) or PDO (winner! link). It's more than worthwhile to convert your code and start using one of these newer, more secure methods of running MySQL queries in PHP.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    typically when you use an ID field, it is unique. you can drop the LIMIT 1 in that case. also, if you are expecting only one row to work on you can drop the while loop. exit can be used to dump text, reducing the need to use echo/exit combo.
    PHP Code:
    <?php
    $result 
    mysql_query("SELECT title, name, surname, identityno, gender FROM cic_candidates WHERE id = '{$id}'");

    if (
    $result === false)
        exit(
    'Could not successfully run query ' mysql_error());
    if (
    mysql_num_rows($result) == 0)
        exit(
    'No rows found, nothing to print so am exiting');

    $total 5;
    $point 0;

    if (
    $row mysql_fetch_assoc($result)) {
        if(
    $row['title'] != '')
            
    $point++;
        if(
    $row['name'] != '')
            
    $point++;
        if(
    $row['surname'] != '')
            
    $point++;
        if(
    $row['identityno'] != '')
            
    $point++;
        if(
    $row['gender'] != '')
            
    $point++;
    }
    mysql_free_result($result);
    echo (
    $point $total) . '%';
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles