<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	if ($_SESSION['システム管理者'] <> "YES") {
		exit;
	}
	define("PAGE_LINE_SELECT", "5,10,20,50,100,1000");	//頁内に表示する行数
	define("PAGE_LINE_DEFAULT", "10");			//頁内に表示する行数（デフォルト）
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="ja">
<head>
<meta http-equiv="Content-Language" content="ja">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta name="author" content="ok.2nd">
<title>MySQL管理ツール：テーブル データ表示</title>
<link rel="stylesheet" href="<?= _STYLE_SHEET_FOLDER ?>/tools_common.css?20091105" type="text/css">
<link rel="stylesheet" href="<?= _STYLE_SHEET_FOLDER ?>/db_tools.css?20090812" type="text/css">
</head>
<body>
<?php
	if (($dbname = $_GET['db']) == '') {
		error_exit('データベース名なし');
	}
	if (($tblname = $_GET['tb']) == '') {
		error_exit('テーブル名なし');
	}
?>
<p><?php
	page_header_return_index();
?>
<span id="tool_title">MySQL管理ツール：テーブル データ表示</span>
<a class="a_cancel_back navi_back" href='mysql-database.php'>データベース一覧に戻る</a>
<a class="a_cancel_back navi_back" href='mysql-database.php?db=<?= $dbname ?>'>テーブル一覧に戻る</a>
</p>
<?php
	$fields = get_table_field($dbname, $tblname);
	print_table_list($dbname, $tblname, $fields);
?>
<div id="footer">
<a href="http://ok2nd.web.fc2.com/" target="_blank" style="color:#8080ff;">Powered by ok.2nd</a>
</div>
</body>
</html>
<?php
function get_table_field($dbname, $tblname) {
	$con = @mysql_connect(_DB_SERVER, _DB_SCHEMA_USERNAME, _DB_SCHEMA_PASSWORD);
	if ($con == False) {
		error_exit('データベース接続エラー');
	}
	mysql_query('set names utf8', $con);
	if (!mysql_select_db($dbname)) {
		error_exit('データベース選択エラー');
	}
	$rs = mysql_query('desc '.$tblname);
	if (!$rs) {
		error_exit ($tblname.'：テーブル選択エラー');
	}
	$fields = array();
	while ($rec = mysql_fetch_array($rs)) {
		$fields[] = array($rec['Field'], $rec['Type']);
	}
	$con = mysql_close($con);
	return $fields;
}
function print_table_list($dbname, $tblname, $fields) {
	$colname = '';
	foreach ($fields as $field) {
		if ($colname <> '') $colname .= ',';
		$colname .= $field[0];
	}
	$sql = 'select ' . $colname . ' from ' . $tblname;
	if ($_GET['so'] <> '') {
		$sql .= ' order by ' . $_GET['so'];
	}
	$con = @mysql_connect(_DB_SERVER, _DB_SCHEMA_USERNAME, _DB_SCHEMA_PASSWORD);
	if ($con == False) {
		error_exit('データベース接続エラー');
	}
	mysql_query('set names utf8', $con);
	if (!mysql_select_db($dbname)) {
		error_exit('データベース選択エラー');
	}
	$rs = mysql_query($sql);
	$rowcnt = mysql_num_rows($rs);
?>
	<p class="subtitle">データベース：<span class="db_name"><?= $dbname ?></span>&nbsp;&nbsp;
	テーブル：<span class="tbl_name"><?= $tblname ?></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href='excel-import.php?db=<?= $dbname ?>&tb=<?= $tblname?>'>Excelインポート</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href='csv-import.php?db=<?= $dbname ?>&tb=<?= $tblname?>'>CSVインポート</a>
	</p>
<?php
	if ($rowcnt > 0) {
		table_list_view($dbname, $tblname, $fields, $rs, $rowcnt);
	} else {
		error_msg('このテーブルにデータはありません。');
	}
	$con = mysql_close($con);
}
function table_list_view($dbname, $tblname, $fields, $rs, $rowcnt) {
	if ($_GET['pl'] <> '') {
		$_SESSION['my_sql_table_list_pageline'] = $_GET['pl'];
	}
	if ($_SESSION['my_sql_table_list_pageline'] == '') {
		$_SESSION['my_sql_table_list_pageline'] = PAGE_LINE_DEFAULT;
	}
	$pageline = $_SESSION['my_sql_table_list_pageline'];
	if ($_GET['page'] <> '') {
		$page = $_GET['page'];
		if ($page < 1) {
			$page = 1;
		}
	} else {
		$page = 1;
	}
	$startline = ($page - 1) * $pageline;				//開始行を算出
	$endline = $page * $pageline -1;				//終了行を算出
	$pagemax = ceil($rowcnt / $pageline);				//取得した行数から最終頁を算出
	mysql_data_seek($rs, $startline);
	list_page_control($rowcnt, $page, $pagemax, $pageline);
?>
	<table class="db_table" cellspacing=1>
		<tr>
<?php
		foreach ($fields as $field) {
?>
			<th><a class="a_field_name" href="?db=<?= $dbname ?>&tb=<?= $tblname ?>&so=<?= $field[0] ?>"><?= $field[0] ?></a></th>
<?php
		}
?>
		</tr>
<?php
	$line = $startline;
	while ($rec=mysql_fetch_array($rs) and $line++<=$endline) {
?>
		<tr>
<?php
		foreach ($fields as $field) {
?>
			<td<? if (stripos($field[1],'int') !== False or stripos($field[1],'decimal') !== False) echo ' align=right' ?>><?= my_htmlspecialchars($rec[$field[0]]) ?></td>
<?php		}
?>
		</tr>
<?php
	}
?>
	</table>
<?php
}
function list_page_control($rowcnt, $page, $pagemax, $pageline) {
	$arg = 'db='.$_GET['db'].'&tb='.$_GET['tb'].'&so='.$_GET['so'];
?>
<div class="mp_list_page_control">
<span class="mp_list_page_control_info">
	<span class="choice">件数：<span class="mp_rowcnt"><?= $rowcnt ?></span>件</span>
	<span class="choice">頁：<span class="mp_page"><?= $page ?></span>/<span class="mp_pagemax"><?= $pagemax ?></span></span>
</span>
<span class="mp_list_page_control_move">
	<?php if ($page > 1) { ?>
		<span><a href="?<?= $arg ?>&page=1&">[←先頭]</a></span>
		<span><a href="?<?= $arg ?>&page=<?= $page-1 ?>">[←前]</a></span>
	<?php } else { ?>
		<span class="link_off">[←先頭]</span>
		<span class="link_off">[←前]</span>
	<?php } ?>
	<?php if ($page < $pagemax) { ?>
		<span><a href="?<?= $arg ?>&page=<?= $page+1 ?>">[→次]</a></span>
		<span><a href="?<?= $arg ?>&page=<?= $pagemax ?>">[→最後]</a></span>
	<?php } else { ?>
		<span class="link_off">[→次]</span>
		<span class="link_off">[→最後]</span>
	<?php } ?>
</span>
<script language="JavaScript">
<!--
function SelectionPageLine(form, sel)
{
	for (i = 0; i < sel.options.length; i++) {
		if (sel.options[i].selected == true) {
			window.location.href = '<?= $_SERVER['SCRIPT_NAME'] ?>' + '?db=<?= $_GET['db'] ?>&tb=<?= $_GET['tb'] ?>&so=<?= $_GET['so'] ?>' + '&pl=' + escape(sel.options[i].value);
		}
	}
}
//-->
</script>
&nbsp;&nbsp;
<span>
頁行数：<select name="" onchange="SelectionPageLine(this.form, this)">
<?php
	$keyary = explode(",", PAGE_LINE_SELECT);
	foreach ($keyary as $keytmp) {
?>
	<option value="<?= $keytmp ?>"<?= $pageline == $keytmp ? " selected" : "" ?>><?= $keytmp ?>
<?php
	}
?>
</select>
</span>
</div>
<?php
}
?>
