add simrs supporting functions
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
class PostgresDb
|
||||
{
|
||||
private $pdo;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$host = "10.10.123.238";
|
||||
$port = "5432";
|
||||
$dbname = "simrs";
|
||||
$user = "brawijaya";
|
||||
$password = "ub*2025";
|
||||
|
||||
$dsn = "pgsql:host=$host;port=$port;dbname=$dbname";
|
||||
|
||||
try {
|
||||
|
||||
$this->pdo = new PDO($dsn, $user, $password, [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
|
||||
]);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
|
||||
die("Database connection failed: " . $e->getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function query($sql, $params = [])
|
||||
{
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
|
||||
public function fetchAll($sql, $params = [])
|
||||
{
|
||||
return $this->query($sql, $params)->fetchAll();
|
||||
}
|
||||
|
||||
|
||||
public function fetch($sql, $params = [])
|
||||
{
|
||||
return $this->query($sql, $params)->fetch();
|
||||
}
|
||||
|
||||
|
||||
public function lastInsertId()
|
||||
{
|
||||
return $this->pdo->lastInsertId();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
if (!function_exists('form_dropdown')) {
|
||||
function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '', $callback = null)
|
||||
{
|
||||
if (!is_array($selected)) {
|
||||
$selected = array($selected);
|
||||
}
|
||||
// If no selected state was submitted we will attempt to set it automatically
|
||||
if (count($selected) === 0) {
|
||||
// If the form name appears in the $_POST array we have a winner!
|
||||
if (isset($_POST[$name])) {
|
||||
$selected = array($_POST[$name]);
|
||||
}
|
||||
}
|
||||
if ($extra != '') $extra = ' ' . $extra;
|
||||
$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
|
||||
$form = '<select name="' . $name . '"' . $extra . $multiple . ">\n";
|
||||
foreach ($options as $key => $val) {
|
||||
$key = (string)$key;
|
||||
if (is_array($val) && !empty($val)) {
|
||||
$form .= '<optgroup label="' . $key . '">' . "\n";
|
||||
foreach ($val as $optgroup_key => $optgroup_val) {
|
||||
$sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
|
||||
$form .= '<option value="' . $optgroup_key . '"' . $sel . '>' . (string)$optgroup_val . "</option>\n";
|
||||
}
|
||||
$form .= '</optgroup>' . "\n";
|
||||
} else {
|
||||
$val = ($callback != '') ? $callback($val) : $val;
|
||||
$sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
|
||||
$form .= '<option value="' . $key . '"' . $sel . '>' . (string)$val . "</option>\n";
|
||||
}
|
||||
}
|
||||
$form .= '</select>';
|
||||
return $form;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user