skip to Main Content

What I’m doing is checking every Input tag if its empty or not, if not empty call the function, Im just wondering if there is a better way to do this

        $firstname = $this->request->getPost('firstname');
        $lastname = $this->request->getPost('lastname');
        $middlename = $this->request->getPost('middlename');
        $address = $this->request->getPost('address1') . $this->request->getPost('address2');
        $idNum = $this->request->getPost('idNum');
        $department = $this->request->getPost('department');
        $driverNumber = $this->session->get('driverNumber');

        if (!empty($firstname)) {
            $r = $this->db->editInfo('firstname', $firstname, $driverNumber);
        }
        if (!empty($lastname)) {
            $r = $this->db->editInfo('lastname', $lastname, $driverNumber);
        }
        if (!empty($middlename)) {
            $r = $this->db->editInfo('middlename', $middlename, $driverNumber);
        }
        if (!empty($this->request->getPost('address1')) || !empty($this->request->getPost('address2'))) {
            $r = $this->db->editInfo('address', $address, $driverNumber);
        }
        if (!empty($department)) {
            $r = $this->db->editInfo('department', $department, $driverNumber);
        }
        if (!empty($idNum)) {
            $r = $this->db->editInfo('idNum', $idNum, $driverNumber);
        }

I tried Switch cases but from what I understand switch cases needs a variable to equal to a string

$favcolor = "red";

switch ($favcolor) {
  case "red":
    echo "Your favorite color is red!";
    break;
  case "blue":
    echo "Your favorite color is blue!";
    break;
  case "green":
    echo "Your favorite color is green!";
    break;
  default:
    echo "Your favorite color is neither red, blue, nor green!";
}

3

Answers


  1. You could add each of the variables to an array, cycle the array to check if any are empty, if all are present, perform the if statements, else, error out.

    $fields = array($firstname, $lastname, $middlename, $address, $idNum, 
    $department, $driverNum);
    
    foreach ($fields as $field) {
    if ($field) {
    // db calls
    } else {echo "empty field"}`;
    }
    

    You could also do this associatively to indicate which key is empty.

    $fields = array("firstname" => $firstname, "lastname" => $lastname, 
    "middlename" => $middlename, "address" => $address, "id" => $idNum, 
    "department" => $department, "driverNo" => $driverNum);
    
    foreach ($fields as $key => $value) {
    if ($value)
    //db calls
    } else {echo  $key . "is empty"}
    

    Hope this helps.

    Login or Signup to reply.
  2. In your case, you can simplify the code by using an array of fields and looping 
    through them to check if they are empty. This way, you can avoid repeating the 
    same conditional statements for each field ... your updated code is below 
    
    
    
    
    $fields = array(
    'firstname' => $this->request->getPost('firstname'),
    'lastname' => $this->request->getPost('lastname'),
    'middlename' => $this->request->getPost('middlename'),
    'address' => $this->request->getPost('address1') . $this->request->getPost('address2'),
    'idNum' => $this->request->getPost('idNum'),
    'department' => $this->request->getPost('department')
     );
    
    $driverNumber = $this->session->get('driverNumber');
    
    foreach ($fields as $field => $value) {
    if (!empty($value)) {
        $r = $this->db->editInfo($field, $value, $driverNumber);
    }
    }
    
    Login or Signup to reply.
  3. Create Model /app/Models/Model_name.php & define update function:

    namespace AppModels;
    
    use CodeIgniterModel;
    use CodeIgniterDatabaseConnectionInterface;
    class Model_name extends Model {
    
        protected $db;
        public function __construct(ConnectionInterface &$db) {
            $this->db =& $db;
        }
    
        public function update($id, $data) {
            return $this->db
                            ->table('user')
                            ->where(["driverNumber" => $id])
                            ->set($data)
                            ->update();
        }
    }
    

    In Your controller:

    1. Call a Model in Controller
    2. Initailize constructor
    3. Use Model to update data
    namespace AppControllers;
    
    use AppModelsModel_name; //Call a Model in Controller
    
    class User extends BaseController {
    
      public function __construct() {    
          $db = db_connect();
          $this->Model_name= new Model_name($db);  //Update your model name
      }
    
      public function update() {
        $firstname = $this->request->getPost('firstname');
        $lastname = $this->request->getPost('lastname');
        $driverNumber = $this->session->get('driverNumber');
    
        $data = [
          'firstname'    => $firstname,
          'lastname'     => $lastname,
        ];
    
        $result = $this->Model_name->update($driverNumber, $data);
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search