Skip to content

Commit

Permalink
Updated add record, still not completed
Browse files Browse the repository at this point in the history
  • Loading branch information
asakpke committed Nov 18, 2023
1 parent fdad659 commit ddd7d0d
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Example1/Add.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

require 'Example1.php';

Expand Down
14 changes: 12 additions & 2 deletions Example1/Example1.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,44 @@ class Example1 extends PRC
{
function __construct()
{
$this->tblName = 'example1';

parent::__construct();

$this->tblName = 'example1';
$this->auth = "WHERE user_id = {$_SESSION['user_id']}";
$this->auth = "WHERE user_id = {$_SESSION['user_id']}"; // we may adjust or fix it later on
$this->tblCols = array(
'id' => array(
'type' => 'text',
'display as' => 'ID',
'is display' => array(
'on listing' => true,
'on view' => false,
'on add' => false,
'on edit' => false,
),
'is required' => true,
),
'name' => array(
'type' => 'text',
'display as' => 'Name',
'is display' => array(
'on listing' => true,
'on view' => true,
'on add' => true,
'on edit' => true,
),
'is required' => true,
),
'date' => array(
'type' => 'date',
'display as' => 'Date',
'is display' => array(
'on listing' => true,
'on view' => true,
'on add' => true,
'on edit' => true,
),
'is required' => false,
),
);
} // __construct()
Expand Down
9 changes: 9 additions & 0 deletions Example1/View.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

require 'Example1.php';

$example1 = new Example1();
$example1->view();
3 changes: 3 additions & 0 deletions Example1/index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

require 'Example1.php';

Expand Down
42 changes: 42 additions & 0 deletions Example2/Example2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

require '../PRC.php';

class Example2 extends PRC
{
function __construct()
{
$this->tblName = 'example2';

parent::__construct();

$this->tblCols = array(
'id' => array(
'type' => 'text',
'display as' => 'ID',
'is display' => array(
'on listing' => true,
'on view' => false,
'on add' => false,
'on edit' => false,
),
'is required' => true,
),
'name' => array(
'type' => 'text',
'display as' => 'Name',
'is display' => array(
'on listing' => true,
'on view' => true,
'on add' => true,
'on edit' => true,
),
'is required' => true,
),
);
} // __construct()

function __destruct() {
parent::__destruct();
} // __destruct()
} // class
9 changes: 9 additions & 0 deletions Example2/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

require 'Example2.php';

$example2 = new Example2();
$example2->list();
106 changes: 92 additions & 14 deletions PRC.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class PRC
protected $appPathUrl = 'http://localhost/PRC';
protected $dbConn;
protected $tblName;
protected $tbl;
protected $tblNameUcF;
protected $isTblNamePlural = true;
protected $tblCols;
protected $showHTML = true;
protected $auth = false;
Expand Down Expand Up @@ -41,6 +44,8 @@ function __construct($opt = null)
'example' // Database name, notice no ","
);

$this->tblNameUcF = ucfirst($this->tblName ?? false);

if ($this->showHTML) {
?>
<!doctype html>
Expand All @@ -64,10 +69,18 @@ function __construct($opt = null)
$all = array_diff(scandir($dir), [".", "..", ".git"]);

foreach ($all as $ff) {
if (is_dir($dir . $ff)) {
echo "<a href=\"{$this->appPathUrl}/{$ff}\">{$ff}</a> |";
}
}
if (is_dir($dir . $ff)) {
// pr($ff,'$ff');
// pr($this->tblName,'$this->tblName');

if ($ff == $this->tblNameUcF) {
echo "<strong>{$ff}</strong> | ";
}
else {
echo "<a href=\"{$this->appPathUrl}/{$ff}\">{$ff}</a> | ";
} // if
} // if is_dir
} // foreach all
?>
<a href="<?= $this->appPathUrl ?>/Logout.php">Logout</a>
</nav>
Expand All @@ -89,6 +102,7 @@ function list()
$headers .= "<th scope=\"col\">{$tblCol['display as']}</th>";
$fields .= "{$this->tblName}.{$key}, ";
$displayColCount++;
// Maybe limit headers/values to 10 or some columns
} // foreach (tblCols)

$fields = rtrim($fields,', ');
Expand All @@ -107,10 +121,10 @@ function list()
// print_r($cols);
} // if ($result->num_rows)

$tblNameUcF = ucfirst($this->tblName);
// $tblNameUcF = ucfirst($this->tblName);
?>
<main>
<h2><?= $tblNameUcF ?> <a href="<?= $this->appPathUrl.'/'.$tblNameUcF ?>/Add.php" style="text-decoration: none;">+ <small>(add new record)</small></a></h2>
<h2><?= $this->tblNameUcF ?> <a href="<?= $this->appPathUrl.'/'.$this->tblNameUcF ?>/Add.php" style="text-decoration: none;">+ <small>(add new record)</small></a></h2>
<table style="width:100%">
<?php
if (mysqli_num_rows($result)) {
Expand Down Expand Up @@ -141,7 +155,7 @@ function list()
echo "<td>{$row[$key]}</td>";
} // foreach (tblCols)

echo '<td><a href="#">Edit</a> | <a href="#">Delete</a></td>';
echo "<td><a href=\"{$this->appPathUrl}/{$this->tblNameUcF}/View.php\">View</a> | <a href=\"#\">Edit</a> | <a href=\"#\">Delete</a></td>";
?>
</tr>
<?php
Expand All @@ -159,30 +173,85 @@ function list()
<?php
} // list()

function view()
{
$fields = '';

foreach ($this->tblCols as $key => $tblCol) {
if ($tblCol['is display']['on view'] === false) {
continue;
}

$fields .= "{$this->tblName}.{$key}, ";
// Maybe limit headers/values to 10 or some columns
} // foreach (tblCols)

$fields = rtrim($fields,', ');

$sql = "SELECT $fields FROM {$this->tblName} {$this->auth};";
// print_r($sql);

$result = mysqli_query(
$this->dbConn,
$sql
);

if (mysqli_num_rows($result)) {
$row = mysqli_fetch_assoc($result);
$cols = array_keys($row);
// print_r($cols);
} // if ($result->num_rows)

// $tblNameUcF = ucfirst($this->tblName);
?>
<main>
<h2><?= $this->tblNameUcF ?></h2>

</main>
<?php
} // view

function add()
{
echo '<pre><h1>$_POST</h1>';
print_r($_POST);
echo '</pre>';
$this->pr($_POST,'$_POST');
$tblName = ucfirst($this->isTblNamePlural ? rtrim($this->tblName,'s') : $this->tblName);
?>
<main>
<h2>Add <?= ucfirst(rtrim($this->tblName,'s')) ?></h2>
<h2>Add <?= $tblName ?></h2>
<form method="post">
<?php
foreach ($this->tblCols as $key => $tblCol) {
if ($tblCol['is display']['on add'] === false) {
continue;
}

// $this->pr($key,'$key');
// $this->pr($tblCol,'$tblCol');

// switch ($key) {
// case 'date':
// break;

// default:
// break;
// } // switch key
?>
<div>
<input name="<?= $key ?>" id="<?= $key ?>" placeholder="Enter <?= $key ?>">
<label for="<?= $key ?>"><?= $tblCol['display as'] ?></label>
<input
type="<?= $tblCol['type'] ?>"
name="<?= $key ?>"
id="<?= $key ?>"
placeholder="Enter <?= $key ?>"
value="<?= $_POST[$key] ? $_POST[$key] : '' ?>"
<?= $tblCol['is required'] ? 'required' : '' ?>
>
</div>
<?php
} // foreach
?>
<br>
<button class="btn btn-primary py-2" type="submit">Add <?= ucfirst(rtrim($this->tblName,'s')) ?></button>
<button class="btn btn-primary py-2" type="submit">Add <?= $tblName ?></button>
</form>
</main>
<?php
Expand Down Expand Up @@ -349,4 +418,13 @@ function __destruct() {

mysqli_close($this->dbConn); // optional
} // __destruct()
} // class PRC
} // class PRC

function pr($value,$name)
{
$count = is_countable($value) ? count($value) : null;

echo "<pre><h1>Name: {$name}, <small>Count:{$count}</small></h1>";
print_r($value);
echo '</pre>';
} // pr()
25 changes: 24 additions & 1 deletion example.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Nov 17, 2023 at 05:16 AM
-- Generation Time: Nov 18, 2023 at 02:04 AM
-- Server version: 10.4.28-MariaDB
-- PHP Version: 8.2.4

Expand Down Expand Up @@ -38,6 +38,17 @@ CREATE TABLE `example1` (

-- --------------------------------------------------------

--
-- Table structure for table `example2`
--

CREATE TABLE `example2` (
`id` int(10) UNSIGNED NOT NULL,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--
Expand All @@ -60,6 +71,12 @@ ALTER TABLE `example1`
ADD PRIMARY KEY (`id`),
ADD KEY `user_id` (`user_id`);

--
-- Indexes for table `example2`
--
ALTER TABLE `example2`
ADD PRIMARY KEY (`id`);

--
-- Indexes for table `users`
--
Expand All @@ -77,6 +94,12 @@ ALTER TABLE `users`
ALTER TABLE `example1`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `example2`
--
ALTER TABLE `example2`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `users`
--
Expand Down

0 comments on commit ddd7d0d

Please sign in to comment.