0

I'm trying to make a profile completion progress, which shows the percentage of how much a user has completed his profile settings. If a user has filled in a field, he receives +15 or +5, however, if the field is not filled in he receives +0.

the code I did is really bad, with variable repetitions, I wanted to know if you knew a cleaner way to do this.

if (!empty($user->avatar)) {
  $avatar = 15;
} else { $avatar = 0; }
if (!empty($user->copertina)) {
  $copertina = 15;
} else { $copertina = 0; }
// dati personali
if (!empty($user->name)) {
  $name= 5;
} else { $name = 0; }
if (!empty($user->last_name)) {
  $last_name = 5;
} else { $last_name = 0; }

[...]

if (!empty($user->biografia)) {
  $biografia = 5;
} else { $biografia = 0; }

$personal = $avatar+$copertina+$name+$last_name+$sesso+$nascita;
$gaming = $steam+$battlenet+$xbox+$playstation+$switch+$uplay+$origin+$ds;
$social = $twitter+$facebook+$biografia;

$punti = $personal+$gaming+$social;

how do I remove all the others {$ variable = 0}?

4
  • 1
    You can't really, since you want the value to be a number, and not "undefined". You could initialize your variables to 0 like in this answer stackoverflow.com/questions/9651793/…. If you want to get into type comparisons for null variables, check php.net/types.comparisons. I would just initialize the variables to 0 and remove all the else. Commented Jul 15, 2018 at 11:23
  • 1
    OR... modify your $user object to have all these variables in an array ($key:$value). You can then initialize the array to 0 all over, and modify it. Adding a new profile value would be easy, and adding array values is quick. Commented Jul 15, 2018 at 11:26
  • Thank you very much for your help @Nic3500, I have used the method array destructuring. :) Commented Jul 15, 2018 at 11:52
  • Pleasure, I transformed my comments into an answer, if you could accept it I would be grateful. Commented Jul 15, 2018 at 15:31

2 Answers 2

0

You can't really, since you want the value to be a number, and not "undefined". You could initialize your variables to 0 like in this answer stackoverflow.com/questions/9651793/….

If you want to get into type comparisons for null variables, check php.net/types.comparisons. I would just initialize the variables to 0 and remove all the else.

OR...

modify your $user object to have all these variables in an array ($key:$value). You can then initialize the array to 0 all over, and modify it. Adding a new profile value would be easy, and adding array values is quick.

Sign up to request clarification or add additional context in comments.

Comments

0

This snippet :

if (!empty($user->avatar)) {
 $avatar = 15;
} 
else {
 $avatar = 0;
}

is semantically equivalent to :

$avatar = (bool)$user->avatar * 15;

Explanation:

  1. Non-empty field gets converted to true and empty string or null gets converted to false

  2. Because we do multiplication php true/false values gets converted to 1/0

  3. So after multiplication you get 15 * 1 or 15 * 0 - depending if your field was used or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.