Created
July 24, 2019 13:49
-
-
Save yasu-investor/e2169d7b79ee2973afa980bc169edd08 to your computer and use it in GitHub Desktop.
phpで動的なテーブル作る
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
<?php | |
// テーブルを描画するfunction | |
function make_table($id, $colname1, $colname2, $data) { | |
echo '<table id="' . $id .'" border=1 class="responsive_table">'; | |
// ヘッダ | |
echo '<tr class="thead">'; | |
foreach($colname1 as $rabel) { | |
echo '<th>' . $rabel . '</th>'; | |
} | |
echo '</tr>'; | |
// 明細 | |
foreach($data as $row) { | |
echo '<tr>'; | |
$i = 0; | |
$code = ''; | |
foreach($row as $cel) { | |
echo '<td data-label="'. $colname2[$i] . '">' . $cel . '</td>'; | |
} | |
echo '</tr>'; | |
} | |
echo '</table>'; | |
} | |
// SQLの実行結果とかで配列を作る想定。例示では適当に作る。 | |
$data = $table= array( | |
array("a1", "a2", "a3", "a4", "a5"), | |
array("b1", "b2", "b3", "b4", "b5") | |
); | |
// ヘッダ | |
// columnName1:PC表示用のヘッダ | |
// columnName2:スマホ表示用のヘッダ | |
$columnName1 = array('PCヘッダ1','PCヘッダ2','PCヘッダ3','PCヘッダ4','PCヘッダ5'); | |
$columnName1 = array('スマホヘッダ1','スマホヘッダ2','スマホヘッダ3','スマホヘッダ4','スマホヘッダ5'); | |
?> | |
<html> | |
<head> | |
<meta content="text/html;charset=UTF-8" /> | |
<title></title> | |
</head> | |
<body> | |
<?php | |
// $dataはsqlの実行結果的な想定なので結果を返した時のみtableを描画する。みたいな。 | |
if($data) { | |
make_table('table1', $columnName, $columnName2, $data); | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment