Contoh di bawah ini cara pembuatannya dengan teknik membuat sebuah fungsi yang menerima argumen berupa jumlah sel dan jumlah kolom (pembentukan sel tabel didasarkan pada nilai jumlah sel dan jumlah kolom yang diberikan).
Berikut adalah cara membuat generator sel tabel:
1. buat source code seperti di bawah ini:
<em><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Table Generator</title>
<style type="text/css">
<!--
#apDiv1 {
width:178px;
height:24px;
z-index:1;
left: 284px;
top: 189px;
}
color: #00ff00;
.style1 {
font-weight: bold;
}
-->
</style>
</head>
<body background="bg1.jpg">
<script language="JavaScript" type="text/javascript">
function getmax() {
var R = parseInt(document.getElementById('JumlahRow').value);
var C = parseInt(document.getElementById('JumlahColum').value);
var X = parseInt(document.getElementById('JumlahCell').value);
var cellmax = document.getElementById('maxcells');
var total = 'N/A';
total = R * C;
cellmax.value = new String(total);
if (X > total)
{
alert('Cell Total Yang Anda Masukkan Terlalu Besar, Nilai Maksimum Cells = ' + total);
document.getElementById('CellsTotal').value = new String();
}
}
</script>
<form method="post" action="TableArgument.php">
<h3 class="style1">.:Generate Tabel Secara Fleksibel:.</h3>
<div>
<table width="300" border="0" bgcolor="#00ff00">
<tr bgcolor="#ff00ff">
<td width="120" style="text-align:center">Rows</td>
<td width="189"><strong>= </strong>
<input name="JumlahRow" type="text" id="JumlahRow" onKeyUp="getmax();" onfocus="this.select();"/></td>
</tr>
<tr bgcolor="#ff00ff">
<td style="text-align:center"><label>Columns</label></td>
<td><strong>= </strong>
<input name="JumlahColum" type="text" id="JumlahColum" onKeyUp="getmax();" onfocus="this.select();"/></td>
</tr>
<tr bgcolor="#ff00ff">
<td style="text-align:center">Cell Total </td>
<td><strong>= </strong>
<input name="JumlahCell" type="text" id="JumlahCell" onKeyUp="getmax();" onFocus="this.select();"/></td>
</tr>
<tr bgcolor="#ff00ff">
<td style="text-align:center">Max Cells </td>
<td><strong>= </strong>
<input name="maxcells" type="text" id="maxcells" readonly="readonly" style="text-align:center"/></td>
</tr>
</table>
</div>
<div id="apDiv1">
<input type="submit" name="Generate" value="Generate"/><input type="reset" name="Reset" value="Reset"/>
</div>
</form>
</body>
</html></em>
Simpan dengan nama 'generator.html' dalam direktori Wamp
2. kemudian file PHPnya yaitu:
<em><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hasil Generate Tabel</title>
</head>
<body>
<div align="center">
<?php
$rows = 1;
$columns = 1;
$cells = 1;
?>
<?php $rows = (int) $_POST["JumlahRow"]; ?>
<?php $columns = (int) $_POST["JumlahColum"]; ?>
<?php $cells = (int) $_POST["JumlahCell"]; ?>
<strong>Kamu Memilih</strong> <?php echo $rows; ?> <em>rows,</em><br />
<strong>Kamu Memilih</strong> <?php echo $columns; ?> <em>columns,</em><br />
<strong>Dan Anda Membutuhkan</strong> <?php echo $cells; ?> <em>cells,</em><br />
<br />
<br />
<?php
$width = $columns * 75;
echo "<table width=".$width." border=1>";
$rw = 0;
$cel = 1;
while ($rw < $rows && $cel <= $cells)
{
echo "<tr>";
$cl = 0;
while ($cl < $columns)
{
if ($cel <= $cells)
{
echo "<td><div align=center>".$cel."</div></td>";
$cel++;
}
$cl++;
}
echo "</tr>";
$rw++;
}
echo "</table>";
?>
</div>
</body>
</html></em>
<html>
<head>
<title>Hasil Generate Tabel</title>
</head>
<body>
<div align="center">
<?php
$rows = 1;
$columns = 1;
$cells = 1;
?>
<?php $rows = (int) $_POST["JumlahRow"]; ?>
<?php $columns = (int) $_POST["JumlahColum"]; ?>
<?php $cells = (int) $_POST["JumlahCell"]; ?>
<strong>Kamu Memilih</strong> <?php echo $rows; ?> <em>rows,</em><br />
<strong>Kamu Memilih</strong> <?php echo $columns; ?> <em>columns,</em><br />
<strong>Dan Anda Membutuhkan</strong> <?php echo $cells; ?> <em>cells,</em><br />
<br />
<br />
<?php
$width = $columns * 75;
echo "<table width=".$width." border=1>";
$rw = 0;
$cel = 1;
while ($rw < $rows && $cel <= $cells)
{
echo "<tr>";
$cl = 0;
while ($cl < $columns)
{
if ($cel <= $cells)
{
echo "<td><div align=center>".$cel."</div></td>";
$cel++;
}
$cl++;
}
echo "</tr>";
$rw++;
}
echo "</table>";
?>
</div>
</body>
</html></em>
Simpan dengan nama generate.php dalam direktori Wamp
3. buka direktori hasil pembuatan script pada browser yang sudah terinstal plug in, misalnya mozilla. maka akan menghasilkan tampilan seperti berikut:
3. buka direktori hasil pembuatan script pada browser yang sudah terinstal plug in, misalnya mozilla. maka akan menghasilkan tampilan seperti berikut:
Table generator screenshot:
Generated Table Screenshot based on Table Generator Fields:
Well, hope it'll be usefull...
0 Comment:
Posting Komentar