webdata
DX推進をサポートする技術者向け情報提供サイト

初心者向けPHP・データベース入門

TOP >初心者向けPHP・データベース入門 >2.9 得意先一覧ページ指定・表示

【PHP入門】得意先一覧ページ指定・表示

 2023-05-02 (更新日:2024-02-04)

<学習する内容>

 得意先一覧にて表示させる行数の制限を行いページ毎表示させる動きを理解します。

1)得意先一覧のページ指定・表示

1ページに5行表示、ページ指定を追記します。
下記のサンプルプログラムをコピーもしくはダウンロードし指定のフォルダーに配置してください。
必ずDBのパスワードを********から設定したパスワードに変更してください
ファイル名:customerlist2_9.php
配置先:c:\xampp\htdocs\
配置先URL:http://localhost/customerlist2_9.php

サンプルプログラム名:customerlist2_9.php
						<?php
						
						//DBへの接続
							$host = 'localhost';	//サーバ名
							$user = 'root';			//ユーザ名
							$pass = '********';		//DBのパスワード(自環境のパスワードに書き換えのこと)
							$dbnm = 'sales';		//データベース名
							$conn = mysqli_connect($host,$user,$pass,$dbnm) or die("er 接続できません");
						
						//登録データの受信
							$fnc = filter_input(INPUT_POST, 'fnc');
							$set_customerCode = filter_input(INPUT_POST, 'set_customerCode');
							$set_customerName = filter_input(INPUT_POST, 'set_customerName');
							$set_zipcode = filter_input(INPUT_POST, 'set_zipcode');
							$set_address1 = filter_input(INPUT_POST, 'set_address1');
							$set_address2 = filter_input(INPUT_POST, 'set_address2');
						
						//データ有無チェック
									$mess = null;		//$messのリセット(これを定義しないとwarning)
						if(isset($fnc)){
							if(empty($set_customerCode)){
									$mess = '得意先コードが未登録です。';
								if(empty($set_customerName)){
									$mess = '得意先コードと得意先名が未登録です。';
								}
							}else{
								if(empty($set_customerName)){
									$mess = '得意先名が未登録です。';
								}else{
									$set_customerName = mb_convert_kana($set_customerName,'AKS');		//文字の全角化
									$set_customerName = str_replace(' ','',$set_customerName);			//空白をとる
									$set_customerName = "'$set_customerName'";	//varcharの変数を"''"囲む
								}
							}
							if(empty($set_zipcode)){
								$set_zipcode = 'null';				//データがなければ'null'を代入
							}else{
								$set_zipcode = "'$set_zipcode'";
							}
							if(empty($set_address1)){
								$set_address1 = 'null';			//データがなければ'null'を代入
							}else{
								$set_address1 = "'$set_address1'";
							}
							if(empty($set_address2)){
								$set_address2 = 'null';			//データがなければ'null'を代入
							}else{
								$set_address2 = mysqli_real_escape_string($conn,$set_address2);
								$set_address2 = "'$set_address2'";
							}
						}
						
						//データ登録
						if(empty($mess)){					//データチェックで問題がない($messに何もデータがない)
							if(isset($set_customerCode)){
								$sql = "insert into Mcustomer
											(
												customerCode
												,customerName
												,zipcode
												,address1
												,address2
											) values (
												$set_customerCode
												,$set_customerName
												,$set_zipcode
												,$set_address1
												,$set_address2
											) on duplicate key update
												customerName = $set_customerName
												,zipcode = $set_zipcode
												,address1 = $set_address1
												,address2 = $set_address2
											;";
								mysqli_query($conn,$sql) or die ("error $sql");
							}
						}
						
						//検索データ受信
							$search_customerName = filter_input(INPUT_POST, 'search_customerName');
							$search_customerName = mb_convert_kana($search_customerName,'AK');		//文字の全角化
						if(empty($search_customerName) or $search_customerName == '得意先名検索'){
							$where_customer = null;
						}else{
							$where_customer = "where customerName like '%$search_customerName%'";
						}
						//ソートデータ受信
							$select_sort = filter_input(INPUT_POST, 'select_sort');
							$sort_display = 'ソート';
							$SORT = null;
							switch($select_sort){
								case 1:
									$SORT = 'order by customerName';
									$sort_display = '得意先名昇順';
									break;
								case 2:
									$SORT = 'order by customerName desc';
									$sort_display = '得意先名降順';
									break;
							}
							
						//ページ対応
							$page = filter_input(INPUT_POST, 'page');	//ページ指定
							$add = filter_input(INPUT_POST, 'add');		//ページを戻るか進めるか
							if(empty($page)){
								$page = 0;
								$offset = 0;
							}
							if($add == 1){				//ページを進める
								$page = $page + 1;
							}
							if($add == -1){				//ページを戻す
								$page = $page - 1;
							}
							$offset = $page * 5;		//5行のデータを表示 20にすると20行表示
							
							//合計件数
							$sql = "select customerCode,customerName,zipcode,address1,address2 from Mcustomer
							$where_customer;";
							$res = mysqli_query($conn,$sql) or die("エラー $sql");
							$cnt_all = mysqli_num_rows($res);
							$cnt = ceil($cnt_all/5);

							$page_add1 = $page + 1;		//表示させるページ
							
						echo <<<EOT
						<!DOCTYPE html>
						<html lang="ja">
						
						<head>
							<meta charset="utf-8">
							<link rel="stylesheet" href="/style.css" type="text/css">
							<title>得意先一覧表</title>
						</head>
							
						<body>
						<table>
							<tr>
								<td width="100">得意先一覧表</td>
							<form method="POST" action="{$_SERVER['PHP_SELF']}">
								<td width="130px">
									<input type="text" name="select_customerName" style="width:120px;height:20px;" 
									placeholder="得意先名検索" onfocus="this.placeholder=''" onblur="this.placeholder='得意先名検索'"
        							value="{$select_customerName}">
								</td>
								<td width="60px">
									<input type="submit" value="検索" style="width:100%;">
								</td>
							</form>
							<form method="POST" action="{$_SERVER['PHP_SELF']}">
								<td width="60px">
									<input type="submit" value="クリア" style="width:60px;">
								</td>
							</form>
								<td>{$mess}<td>
							<form method="POST" action="{$_SERVER['PHP_SELF']}">
								<td width="110px">
									<select type="text"  name="select_sort" onchange="submit(this.form)"  style="width:100px;height:24px;">
										<option value="{$select_sort}">{$sort_display}
										<option value="1">得意先名昇順
										<option value="2">得意先名降順
										<option value="">解除
									</select>
								</td>
							</form>
						EOT;
							if($page > 0){			//戻るページがあれば下記を表示
						echo <<<EOT
							<form method="POST" action="{$_SERVER['PHP_SELF']}">
								<td width="25px">
									<input type="hidden" name="add" value="-1">
									<input type="hidden" name="page" value="{$page}">
									<input type="hidden" name=select_customerName" value="{$select_customerName}">
									<input type="hidden" name="select_sort" value="{$select_sort}">
									<input type="submit" value="<" style="width:25px;">
								</td>
							</form>
						EOT;
							}else{
						echo <<<EOT
								<td width="25px"></td>
						EOT;
							}
							if($page_add1 < $cnt){	//進めるページがあれば下記を表示
						echo <<<EOT
							<form method="POST" action="{$_SERVER['PHP_SELF']}">
								<td width="25px">
										<input type="hidden" name="add" value="1">
										<input type="hidden" name="page" value="{$page}">
										<input type="hidden" name="select_customerName" value="{$select_customerName}">
										<input type="hidden" name="select_sort" value="{$select_sort}">
									    <input type="submit" value=">" style="width:25px;">
								</td>
							</form>
						EOT;
							}else{
						echo <<<EOT
								<td width="25px"></td>
						EOT;
							}
						echo <<<EOT
								<td width="150" align="left"> {$page_add1} / {$cnt}ページ {$cnt_all}件<td>
							   </tr>
						</table>
						<table bgcolor="#a9a9a9" cellspacing="1px" style="font-size:12px;" >
							<tr bgcolor="#D3D3D3" style="height:24px;" align="center">
								<td width="90">得意先コード</td>
								<td width="200">得意先名</td>
								<td width="70">郵便番号</td>
								<td width="200">住所1</td>
								<td width="200">住所2</td>
								<td width="60">登録</td>
							</tr>
							<tr bgcolor="white" style="height:24px;" align="left">
							<form method="POST" action="{$_SERVER['PHP_SELF']}">
									<input type="hidden" name="fnc" value="1">
								<td>
									<input type="text" name="set_customerCode" style="width:98px;">
								</td>
								<td>
									<input type="text" name="set_customerName" style="width:198px;">
								</td>
								<td>
									<input type="text" name="set_zipcode" style="width:68px;">
								</td>
								<td>
									<input type="text" name="set_address1" style="width:198px;">
								</td>
								<td>
									<input type="text" name="set_address2" style="width:198px;">
								</td>
								<td>
									<input type="submit" value="新規" style="width:60px;">
								</td>
							</form>
							</tr>
						EOT;
						
						//DBからデータを抽出
							$sql = "select customerCode,customerName,zipcode,address1,address2 from Mcustomer
									$where_customer
									$SORT
									limit 5 offset $offset ;";		//limit1 5 は5行ずつ抽出、offset データ取得開始位置指定
							$res = mysqli_query($conn,$sql) or die("エラー $sql");
							while($row = mysqli_fetch_array($res)){		//while(){ ~ }繰り返し処理
								$customerCode = $row["customerCode"];
								$customerName = $row["customerName"];
								$zipcode = $row["zipcode"];
								$address1 = $row["address1"];
								$address2 = $row["address2"];
								
						echo <<<EOT
							<tr bgcolor="white" style="height:24px;" align="left">
							<form method="POST" action="{$_SERVER['PHP_SELF']}">
									<input type="hidden" name="fnc" value="1">
									<input type="hidden" name="set_customerCode" value="{$customerCode}">
								<td width="100">
									{$customerCode}
								</td>
								<td>
									<input type="text" name="set_customerName" style="width:198px;" value="{$customerName}">
								</td>
								<td>
									<input type="text" name="set_zipcode" style="width:68px;" value="{$zipcode}">
								</td>
								<td>
									<input type="text" name="set_address1" style="width:198px;" value="{$address1}">
								</td>
								<td>
									<input type="text" name="set_address2" style="width:198px;" value="{$address2}">
								</td>
								<td>
									<input type="submit" value="修正" style="width:60px;">
								</td>
							</form>
							</tr>
						EOT;
							}		//← 繰り返し処理の }
						
						echo <<<EOT
							</table>
						</body>
						
						</html>
						EOT;
						
						?>
						
<解説>
102行目から124行目(ページ指定データ)
$page = filter_input(INPUT_POST, 'page'); はページ指定
$add = filter_input(INPUT_POST, 'add'); はページを戻るか進めるか
if(empty($page)){ はページ未指定時のデフォルト値を定義
  $page = 0;
  $offset = 0;
}
$cnt_all = mysqli_num_rows($res);は抽出結果の数を取り出すコマンド
$cnt = ceil(/5);ページ数を計算 20行ずつなら/20 ceilは小数点以下切り上げ
$page_add1 = $page + 1; ページ位置を表示させるページ数
167行目から194行目(ページ指定)
<input type="hidden" name="add" value="-1">はページを戻す
<input type="hidden" name="add" value="1">はページを進める
<input type="hidden" name="select_customerName" value="{$select_customerName}">と
<input type="hidden" name="select_sort" value="{$select_sort}">
記載の意味は、検索条件を引き継がせている。この条件で合計数は変わるため。

240行目から243行目(SQL文)
limit 5 offset ;"; はlimit1 5 は5行ずつ抽出(20行なら20を記述)、offset データ取得開始位置指定


include文を使って得意先一覧の簡素化を行います。