/**
* Generate HTML form elements
*/
class JLSS_Form_Generator {
// Define constant for available input types, including 'password'
const TEXT = 'text';
const CHECKBOX = 'checkbox';
const SELECT = 'select';
const EMAIL = 'email';
const PHONE = 'tel'; // HTML input type for telephone is 'tel'
const SUBMIT = 'submit';
const RADIO = 'radio';
const PASSWORD = 'password';
const HIDDEN = 'hidden';
const INPUT_TYPES = [self::TEXT,
self::CHECKBOX,
self::SELECT,
self::EMAIL,
self::PHONE,
self::SUBMIT,
self::RADIO,
self::PASSWORD,
self::HIDDEN];
/**
* Generate HTML for any input type
*/
public static function generateInput($type, $name, $options = []) {
// Check if the input type is valid
if (!in_array($type, self::INPUT_TYPES)) {
return "Error: Unsupported input type '{$type}'.";
}
$label = isset($options['label']) ? $options['label'] : ucfirst($name);
// Start building the HTML string
$html = "";
// Handle select separately because it's fundamentally different
if ($type == 'select') {
$html .= "
";
$html .= "
";
} else {
// For input types other than select
$html .= "
";
}
return $html;
}
}
/**
* Singleton class for handling database interactions.
*/
class JLSS_Db {
private static $conn = null;
// Get database connection details from environment variables
private static $dbHost;
private static $dbUser;
private static $dbPassword;
private static $dbName;
// Destructor to ensure connection is closed
function __destruct() {
self::closeConn();
}
private static function initialize() {
self::$dbHost = getenv('JLSS_DB_HOST');
self::$dbUser = getenv('JLSS_DB_USER');
self::$dbPassword = getenv('JLSS_DB_PASSWORD');
self::$dbName = getenv('JLSS_DB_NAME');
}
public static function getConn() {
if (self::$conn === null) {
self::initialize(); // Ensure environment variables are loaded
self::$conn = new mysqli(self::$dbHost, self::$dbUser, self::$dbPassword, self::$dbName);
// Check connection
if (self::$conn->connect_error) {
die('Connection failed: ' . self::$conn->connect_error);
}
}
return self::$conn;
}
/**
* Close the database connection.
*/
public static function closeConn() {
if (self::$conn != null) {
self::$conn->close();
self::$conn = null;
}
}
/**
* Execute a query on the database and return result as an array of associative arrays.
*
* @param string $sql The SQL query string.
* @return array|false Returns an array of associative arrays representing the rows
* returned by the query, or false on failure.
*/
public static function query($sql) {
$conn = self::getConn();
$result = $conn->query($sql);
if ($result === false) {
// Query failed: handle error appropriately
return false;
}
if ($result instanceof mysqli_result) {
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
$result->free(); // Free the memory associated with the result
return $data;
} else {
// The query was probably an INSERT/UPDATE/DELETE which does not produce a result set
return [];
}
}
/**
* Prepare a query.
*/
public static function prepare($sql) {
$conn = self::getConn();
return $conn->prepare($sql);
}
}
Products Archive - J. Lee Salon Suites