Sandeep Pariyar Sandeep Pariyar - 7 months ago 40
SQL Question

Counting how many MySQL fields in a row are filled in Codeigniter

I need to write a method that would allow me to calculate how many fields in a row have been filled by a user.
For example:

User Name Age Country Gender Height
1 Mike 34 USA Male 6
2 Bill 23 CA 5
3 Jane 31 USA


In the above example, I would like to query the database and return a value that would reflect the degree of completion of the user's record. Such as:

User 1 = 100% complete
User 2 = 80% complete
User 3 = 60% complete


I am using Codeigniter 3. Can you guys help me.

Answer

Make a method pass a parameter in method

  function profileCompleted($user){
         $percentage = 0;
         $sql ="SELECT * FROM user  WHERE user=$user"
       $query = $this->db->query($sql);

      if ($query->num_rows() > 0)
          { 
            $notEmpty =   0;
            $totalField =5;
            foreach ($query->result() as $row)
              {
               $notEmpty +=  ($row->Name != '') ? 1 : 0;
                //do with all field
              }
          $percentage = $notEmpty/$totalField *100;
          }
        return $percentage.'%';
    }
Comments