-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sample data, selecting list item sets the form
- Loading branch information
Showing
15 changed files
with
787 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,96 @@ | ||
<?php | ||
ini_set('display_errors', 1); | ||
error_reporting(E_STRICT|E_ALL); | ||
|
||
require 'sequel.php'; | ||
|
||
define('RESULTS_PER_PAGE', 3); | ||
|
||
?> | ||
$sql = new Sequel(new PDO( | ||
'mysql:dbname=forminator_demo;host=localhost', | ||
'root', | ||
'P0l.ar-B3ar' | ||
)); | ||
|
||
function getWhereStartsWith(array $collection, $startOfKey) { | ||
$filtered = array(); | ||
foreach($collection as $key => $value) { | ||
if(preg_match('/^' . $startOfKey . '/', $key)) { | ||
$filtered[$key] = $value; | ||
} | ||
} | ||
return $filtered; | ||
} | ||
|
||
function stripLeadingKey(array $collection, $startOfKey) { | ||
$stripped = array(); | ||
foreach($collection as $key => $value) { | ||
$strippedKey = ''; | ||
preg_replace('/^' . $startOfKey . '/', $strippedKey, $key); | ||
$stripped[$strippedKey] = $value; | ||
} | ||
return $stripped; | ||
} | ||
|
||
function getOrders() { | ||
return stripLeadingKey(getWhereStartsWith($_GET, 'order_'), 'order_'); | ||
} | ||
|
||
function getFilters() { | ||
return stripLeadingKey(getWhereStartsWith($_GET, 'filter_'), 'filter_'); | ||
} | ||
|
||
$response = null; | ||
switch($_SERVER['REQUEST_METHOD']) { | ||
case 'GET': | ||
$where = implode(' AND ', array_map(function ($key) { | ||
return $key . ' = ?'; | ||
}, array_keys(getFilters()))); | ||
|
||
$orderArray = array(); | ||
foreach(getOrders() as $key => $value) { | ||
$orderArray[] = $key . ' ' . $value; | ||
} | ||
$order = implode(', ', $orderArray); | ||
|
||
$pageNumber = (isset($_GET['page']) ? $_GET['page'] : 1) - 1; | ||
|
||
$results = array_map( | ||
function ($row) { | ||
$row['checkbox'] = explode(',', $row['checkbox']); | ||
$row['checkbox2'] = explode(',', $row['checkbox2']); | ||
return $row; | ||
}, | ||
$sql->query( | ||
'SELECT * FROM forminator ' . | ||
($where ? ' WHERE ' . $where : '') . | ||
($order ? ' ORDER BY ' . $order : '') . | ||
' LIMIT ' . (RESULTS_PER_PAGE * $pageNumber) . ', ' . RESULTS_PER_PAGE | ||
)->toArray() | ||
); | ||
|
||
$response = array('results' => $results, 'status' => 200); | ||
break; | ||
case 'POST': | ||
switch(strtolower($_GET['action'])) { | ||
case 'create': | ||
$id = $sql->insert('forminator', $_POST); | ||
$response = array('id' => $id, 'status' => 200); | ||
break; | ||
case 'update': | ||
$sql->update('forminator', $_POST); | ||
$response = array('status' => 200); | ||
break; | ||
case 'delete': | ||
$sql->delete('forminator', $_GET['id']); | ||
$response = array('status' => 200); | ||
break; | ||
default: | ||
throw new Exception('invalid action : ' . $_GET['action']); | ||
} | ||
break; | ||
default: | ||
throw new Exception('invalid request method: ' . $_SERVER['REQUEST_METHOD']); | ||
} | ||
echo json_encode($response); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
ini_set('display_errors', 1); | ||
error_reporting(E_STRICT|E_ALL); | ||
|
||
define('NUMBER_OF_ROWS_TO_GENERATE', 5000); | ||
|
||
require '../sequel.php'; | ||
|
||
function getRandomFromArray(array $array) { | ||
return $array[rand(0, count($array) - 1)]; | ||
} | ||
|
||
function getRandomFromArrayMultiple(array $array) { | ||
$returnArray = array(); | ||
foreach($array as $value) { | ||
if(rand(0, 1)) { | ||
$returnArray[] = $value; | ||
} | ||
} | ||
return implode(',', $returnArray); | ||
}; | ||
|
||
$names = explode("\n", file_get_contents('names.txt')); | ||
function getRandomName($names, $numberOfNames = null) { | ||
$numberOfNames = $numberOfNames > 0 ? $numberOfNames : 1; | ||
$namesSample = array(); | ||
for($i = 0; $i < $numberOfNames; $i += 1) { | ||
$namesSample[] = getRandomFromArray($names); | ||
} | ||
return implode(', ', $namesSample); | ||
} | ||
|
||
$sql = new Sequel(new PDO( | ||
'mysql:dbname=forminator_demo;host=localhost', | ||
'root', | ||
'P0l.ar-B3ar' | ||
)); | ||
|
||
for($i = 0; $i < NUMBER_OF_ROWS_TO_GENERATE; $i += 1) { | ||
$sql->insert('forminator', array( | ||
'text' => getRandomName($names), | ||
'text2' => getRandomName($names, rand(0, 5)), | ||
'textarea' => getRandomName($names, rand(0, 5)), | ||
'textarea2' => getRandomName($names, rand(0, 5)), | ||
'`select`' => getRandomFromArray(array('a', 'b')), | ||
'select2' => getRandomFromArray(array('a', 'b')), | ||
'radio' => getRandomFromArray(array('a', 'b')), | ||
'radio2' => getRandomFromArray(array('a', 'b')), | ||
'checkbox' => getRandomFromArrayMultiple(array('a', 'b')), | ||
'checkbox2' => getRandomFromArrayMultiple(array('a', 'b')) | ||
)); | ||
} | ||
echo 'done'; | ||
?> |
Oops, something went wrong.