Skip to main content
corrected spelling
Source Link
Max
  • 241
  • 1
  • 4
  • 18

Thanks to tim's reply, I was able to create an improved version.

  • I split the methodsmethod in 2 different methods
  • I changed the names of the methods to better describe the purpose of the method
  • Added bonus: code is easier to read and controller doesn't have to pass empty strings to the model.

Updated methods in the user class (as part of the model):

public function get_user_by_id($db_con, $id){
  $query_get_user = $db_con->prepare('SELECT * FROM users WHERE user_id=:id');
  $query_get_user->bindValue('id', $id, PDO::PARAM_STR);
  $query_get_user->execute();
  return $query_get_user->fetch();
}
public function get_user_by_username($db_con, $username){
  $query_get_user = $db_con->prepare('SELECT * FROM users WHERE user_username=:username');
  $query_get_user->bindValue('username', $username, PDO::PARAM_STR);
  $query_get_user->execute();
  return $query_get_user->fetch();
}

Updated method in the controller class:

public function get_user($variable, $value){
  $user = false;
  if($variable == "id")
    $user = $this->user->get_user_by_id($this->db_con, $value);
  elseif($variable == "username")
    $user = $this->user->get_user_by_username($this->db_con, $value);
  return $user;
}

Thanks to tim's reply, I was able to create an improved version.

  • I split the methods in 2 different methods
  • I changed the names of the methods to better describe the purpose of the method
  • Added bonus: code is easier to read and controller doesn't have to pass empty strings to the model.

Updated methods in the user class (as part of the model):

public function get_user_by_id($db_con, $id){
  $query_get_user = $db_con->prepare('SELECT * FROM users WHERE user_id=:id');
  $query_get_user->bindValue('id', $id, PDO::PARAM_STR);
  $query_get_user->execute();
  return $query_get_user->fetch();
}
public function get_user_by_username($db_con, $username){
  $query_get_user = $db_con->prepare('SELECT * FROM users WHERE user_username=:username');
  $query_get_user->bindValue('username', $username, PDO::PARAM_STR);
  $query_get_user->execute();
  return $query_get_user->fetch();
}

Updated method in the controller class:

public function get_user($variable, $value){
  $user = false;
  if($variable == "id")
    $user = $this->user->get_user_by_id($this->db_con, $value);
  elseif($variable == "username")
    $user = $this->user->get_user_by_username($this->db_con, $value);
  return $user;
}

Thanks to tim's reply, I was able to create an improved version.

  • I split the method in 2 different methods
  • I changed the names of the methods to better describe the purpose of the method
  • Added bonus: code is easier to read and controller doesn't have to pass empty strings to the model.

Updated methods in the user class (as part of the model):

public function get_user_by_id($db_con, $id){
  $query_get_user = $db_con->prepare('SELECT * FROM users WHERE user_id=:id');
  $query_get_user->bindValue('id', $id, PDO::PARAM_STR);
  $query_get_user->execute();
  return $query_get_user->fetch();
}
public function get_user_by_username($db_con, $username){
  $query_get_user = $db_con->prepare('SELECT * FROM users WHERE user_username=:username');
  $query_get_user->bindValue('username', $username, PDO::PARAM_STR);
  $query_get_user->execute();
  return $query_get_user->fetch();
}

Updated method in the controller class:

public function get_user($variable, $value){
  $user = false;
  if($variable == "id")
    $user = $this->user->get_user_by_id($this->db_con, $value);
  elseif($variable == "username")
    $user = $this->user->get_user_by_username($this->db_con, $value);
  return $user;
}
Source Link
Max
  • 241
  • 1
  • 4
  • 18

Thanks to tim's reply, I was able to create an improved version.

  • I split the methods in 2 different methods
  • I changed the names of the methods to better describe the purpose of the method
  • Added bonus: code is easier to read and controller doesn't have to pass empty strings to the model.

Updated methods in the user class (as part of the model):

public function get_user_by_id($db_con, $id){
  $query_get_user = $db_con->prepare('SELECT * FROM users WHERE user_id=:id');
  $query_get_user->bindValue('id', $id, PDO::PARAM_STR);
  $query_get_user->execute();
  return $query_get_user->fetch();
}
public function get_user_by_username($db_con, $username){
  $query_get_user = $db_con->prepare('SELECT * FROM users WHERE user_username=:username');
  $query_get_user->bindValue('username', $username, PDO::PARAM_STR);
  $query_get_user->execute();
  return $query_get_user->fetch();
}

Updated method in the controller class:

public function get_user($variable, $value){
  $user = false;
  if($variable == "id")
    $user = $this->user->get_user_by_id($this->db_con, $value);
  elseif($variable == "username")
    $user = $this->user->get_user_by_username($this->db_con, $value);
  return $user;
}