This page explanation:
Displays a list of customers registered in the database.
This page function:
Search
Display(Use HTML tables)
Pager
1.Display from database to list
① Controller
Controller:/app/controller/customerController.php
class customer {
public function action_index() {
//get data& in array ※idiorm libraly
$customerArr = ORM::for_table('customer')->find_array();
//include view
include_once(dirname(__FILE__) . "/../view/customer_list.php");
}
}
② View
View:/app/view/customer_list.php
<table>
<!--Header Information-->
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>birthday(age)</th>
<th>gender</th>
<th>blood type</th>
</tr>
</thead>
<tbody>
<?php
//↓The value of “$customerArr” of the controller is retrieved by “foreach”.
foreach((array)$customerArr as $key => $value){
$targetid = isset($value['id']) ? $value['id'] : '';
$birthday = isset($value['birthday']) ? $value['birthday'] : '';
$last_name = isset($value['last_name']) ? $value['last_name'] : '';
//~~~
?>
<!--※Below this is html. once php is separated.-->
<!-- ■■■■■■■■■■■■ list ■■■■■■■■■■■■-->
<tr>
<!--※The value taken out is entered.-->
<td><?= $targetId?></td>
<td><?= $last_name.$first_name?></td>
<td><?= $birthday?></td>
<td><?= $genderType?></td>
<td><?= $bloodType?></td>
</tr>
<?php }?>
<!--↑end of "foreach"-->
</tbody>
</table>
2.Search function
① Controller
Controller:/app/controller/customerController.php
First, implement the ability to reset the search.
class customer {
public function action_index() {
//search reset
//If "$_POST['search_type']" exists and "$_POST['search_type']" is "Reset",
//then "$_POST['search']" is an empty array.
if(isset($_POST['search_type']) && $_POST['search_type'] == 'Reset'){
$_POST['search'] = array();
}
Continue coding as is.
Receive POST data when the search button is pressed.
if($_POST['search']){
//Get POST data
$searchFirstName = isset($_POST['search']['first_name']) ? htmlspecialchars($_POST['search']['first_name']) : '';
$searchLastName = isset($_POST['search']['last_name']) ? htmlspecialchars($_POST['search']['last_name']) : '';
$searchGender = isset($_POST['search']['gender']) ? htmlspecialchars($_POST['search']['gender']) : '';
$searchBirthdayBefore = isset($_POST['search']['birthday']['before']) ? htmlspecialchars($_POST['search']['birthday']['before']) : '';
$searchBirthdayAfter = isset($_POST['search']['birthday']['after']) ? htmlspecialchars($_POST['search']['birthday']['after']) : '';
$searchBloodType = isset($_POST['search']['blood_type']) ? htmlspecialchars($_POST['search']['blood_type']) : '';
First issue the sql statement.
The conditions are connected to this SQL statement.
Here, the records of ‘customer’ with ‘delete_flg’ of 0 are retrieved.
※idiorm libraly
try{
$sql = ORM::for_table('customer')->where('delete_flg',0);
Judges if there is POST data with an IF statement.
If there is, connect the method to the SQL statement.
※idiorm libraly
$sql = ORM::for_table('customer')->where('delete_flg',0);
if($searchFirstName != ""){
$sql->where_like("first_name", "%$searchFirstName%");
}
if($searchLastName != ""){
$sql->where_like("last_name", "%$searchLastName%");
}
if($searchGender != ""){
$sql->where("gender",$searchGender);
}
if($searchBirthdayBefore != ""){
$sql->where_gte("birthday",$searchBirthdayBefore);
}
if($searchBirthdayAfter != ""){
$sql->where_lte("birthday",$searchBirthdayAfter);
}
if($searchBloodType != ""){
$sql->where("blood_type",$searchBloodType);
}
$customerArr = $sql->find_array();
}catch(\Throwable $e){
STATIC_UNEXPECTED_LOGGER::logsMessageAndMore($e,"Search display failed.");
}
}else{
try{
$customerArr = ORM::for_table('customer')->where('delete_flg',0)->order_by_desc('id')->find_array();
}catch(\Throwable $e){
STATIC_UNEXPECTED_LOGGER::logsMessageAndMore($e,"Failed to display the list");
}
}
//include view
include_once(dirname(__FILE__) . "/../view/customer_list.php");
}
}
② View
View:/app/view/customer_list.php
<form id="search_form" action="<?= SYSTEM_DIR; ?>/customer" method="POST">
<div>
<div>
<div>
<div>FirstName</div>
<div>
<input name="search[first_name]" type="text" class="form-control" value="<?=$searchFirstName;?>" >
</div>
</div>
<div>
<div>LastName</div>
<div>
<input name="search[last_name]" type="text" class="form-control" value="<?=$searchLastName;?>" >
</div>
</div>
<div>
<div>Gender</div>
<div>
<select class="form-control" name="search[gender]">
<option value="">-</option>
<option value="0">Male</option>
<option value="1">Female</option>
<option value="2">Others</option>
</select>
</div>
</div>
<div>
<div>Birthday(s)</div>
<div>
<input name="search[birthday][before]" type="date" class="form-control" value="<?=$searchBirthdayBefore;?>" >
</div>
</div>
<div>
<div>Birthday(e)</div>
<div>
<input name="search[birthday][after]" type="date" class="form-control" value="<?=$searchBirthdayAfter;?>" >
</div>
</div>
<div>
<div>Gender</div>
<div>
<select class="form-control" name="search[blood_type]">
<option value="">-</option>
<option value="0">A</option>
<option value="1">B</option>
<option value="2">O</option>
<option value="3">AB</option>
</select>
</div>
</div>
<div>
<div>
<input type="submit" value="Search" name="search_type">
<input type="submit" value="Reset" name="search_type">
</div>
</div>
</div>
</div>
</form>
3.Paging function
① Controller
Controller:/app/controller/customerController.php
class customer {
public function action_index(){
//~~↓Write following the search and list view functions~~//
$pageselect = isset($_POST['pageselect']) ? $_POST['pageselect'] : 1;
$listMaxCount = count($customerArr);
$listArr = array_chunk($customerArr, 50);
if($pageselect - 1 < count($listArr)){
$listArrView = isset($listArr[$pageselect - 1]) ? $listArr[$pageselect - 1] : array();
} else {
$listArrView = isset($listArr[0]) ? $listArr[0] :array();
}
$listArrView = isset($listArr[$pageselect - 1]) ? $listArr[$pageselect - 1] : array();
$listPageList = array_keys($listArr);
//include view
include_once(dirname(__FILE__) . "/../view/customer_list.php");
}
}
② View
First, The following functions are used for the paging process.
It takes an array created by the controller ($listPageArr) and the page number received by POST ($pageselect) as arguments.
The array is passed around with a foreah statement, and $pageNo is set to +1. (since the array starts from 0)
In an if statement, the selected page number is compared, displayed, and if correct, selected.
This function is described separately in common.php.
Common:/app/config/common.php
function paging_pagesekect($listPageList, $pageselect) {
echo "<select id='pageselect_base' name=">";
foreach ((array)$listPageList as $pageNo) {
$page = $pageNo + 1;
if ($page == $pageselect) {
echo "<option value='$page' selected>$page</option>";
} else {
echo "<option value='$page'>$page</option>";
}
}
echo "</select>";
}
View:/app/view/customer_list.php
<div class="d-flex">
<form action="<?= SYSTEM_DIR; ?>/customer" method="POST" id="paging_form">
<!-- ↓This "input" is not displayed on the screen. (type="hidden")
input" to set a value to "value".-->
<input type="hidden" name="pageselect" id="pageselect" value="">
<div class="">total <?php echo $listMaxCount; ?> rows | </div>
PAGE<?php paging_pagesekect($listPageList, $pageselect); ?>
</form>
</div>
<!-- ~~~~ -->
<!--↓this section is javascript coding zone-->
<!--The following script is coded by jquery.
jquery is a javascript library that makes writing javascript simple.-->
<script>
$(document).on("change", "#pageselect_base", function() {
//Put a value in the variable "pageno".
//Here the value of <option value='$page'...> of "paging_pagesekect" above is entered.
var pageno = $(this).val();
//Set the value of "pageno" to [value=""] of <input type="hidden" name="pageselect" id="pageselect" value="">.
$("#pageselect").val(pageno);
//Submit paging_form.
$("#paging_form").submit();
});
</script>
