Welcome Guest, Not a member yet? Register   Sign In
Check multiple fields inside table for value and return to $data
#1

[eluser]jcjc[/eluser]
I'm working on a query that needs to check if fields sectionOne, sectionTwo, sectionThree are equal to the value 1. If any of them are return the field name that has the value of 1, allowing me to proceed.

currently I have:

Code:
function _checkSections()
{
  $getUser = $this->userModel->getUserID();

  $this->db->select('SectionOne, SectionTwo, SectionThree');
  $this->db->where('UserID', $getUser);
  $this->db->where('SectionOne', 1);
                $this->db->or_where('SectionTwo', 1);
                $this->db->where('SectionThree', 1);
  $query = $this->db->get('Induction', 1);

  if($query->num_rows() > 0)
  {

  }

}
Thanks
#2

[eluser]Tim Brownlaw[/eluser]
This is totally untested but should give you an idea...
It will give you a count of how many were set to 1 but not which ones!

Not sure what you want to Return so I've left that out...

Code:
function _checkSections()
{
  $valid = 0;
  $getUser = $this->userModel->getUserID();

  $this->db->select('SectionOne, SectionTwo, SectionThree');
  $this->db->where('UserID', $getUser);
  $query = $this->db->get('Induction');

  if($query->num_rows() > 0)
  {
    $row = $query->row_array()  // You should only get 1 row?
    foreach($row as $key=>$value)
    {
       if($value == 1)
      {
         $valid++;
      }
    }
  }

  if($valid > 0)
  {
     // Do stuff
  }
}




Theme © iAndrew 2016 - Forum software by © MyBB