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
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.
You could also do this associatively to indicate which key is empty.
Hope this helps.
Create Model /app/Models/Model_name.php & define update function:
In Your controller: