Skip to content

Commit

Permalink
create mvc files for register feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kuldp18 committed Dec 30, 2023
1 parent 2c89641 commit d643cd5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
32 changes: 32 additions & 0 deletions controllers/register.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
//Controllers handle the logic of the application

declare(strict_types=1);

function is_input_empty(string $fullname, string $email, string $username, string $password){
if(empty ($fullname) || empty ($email) || empty ($username) || empty ($password)){
return true;
}
return false;
}

function is_email_invalid(string $email){
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
return true;
}
return false;
}

function is_username_taken(object $pdo, string $username){
if(get_username($pdo, $username)){
return true;
}
return false;
}

function is_email_registered(object $pdo, string $email){
if(get_email($pdo, $email)){
return true;
}
return false;
}
25 changes: 25 additions & 0 deletions models/register.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
// Models actually interact with the database

declare(strict_types=1);


function get_username(object $pdo, string $username){
$query = "SELECT username FROM users WHERE username = :username"; // :username is a named placeholder
$stmt = $pdo->prepare($query); // prepare the query
$stmt->bindParam(":username", $username, PDO::PARAM_STR); // bind the $username variable to the :username placeholder
$stmt->execute(); // execute the query

$result = $stmt->fetch(PDO::FETCH_ASSOC); //fetch the result from the query
return $result;
}

function get_email(object $pdo, string $email){
$query = "SELECT email FROM users WHERE email = :email"; // :email is a named placeholder
$stmt = $pdo->prepare($query); // prepare the query
$stmt->bindParam(":email", $email, PDO::PARAM_STR); // bind the $email variable to the :email placeholder
$stmt->execute(); // execute the query

$result = $stmt->fetch(PDO::FETCH_ASSOC); //fetch the result from the query
return $result;
}
20 changes: 20 additions & 0 deletions views/register.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
//Views are responsible for displaying the data to the user

declare(strict_types=1);

function check_and_print_register_errors(){
if(isset($_SESSION["errors_register"])){
$errors = $_SESSION["errors_register"];
if(count($errors) > 0){
echo "<section class='error'>";
echo "<h1 class='error__title'>Errors occurred while registering: </h1>";
foreach($errors as $error){
echo "<p class='error__item'>$error</p>";
}
echo "</section>";
unset($_SESSION["errors_register"]);
}

}
}

0 comments on commit d643cd5

Please sign in to comment.