Pages

Tampilkan postingan dengan label PHP. Tampilkan semua postingan
Tampilkan postingan dengan label PHP. Tampilkan semua postingan
0 komentar

Buat tabel fleksibel dengan php

PHP mempunyai fitur untuk membuat tabel yang fleksibel. Sehingga kita akan dimudahkan dalam membuat tabel yang baik.
Kali ini saya akan mencoba untuk sharing tentang cara membuat tabel fleksibel dengan PHP. DI sini akan disiapkan kode html dan php. Kode html untuk mengenerate tabel yang digunakan yaitu berapa jumlah kolomnya,jumlah barisnya, dan jumlah total cellnya. Dan kode php sebagai hasil dari generate tabel yang dilakukan pada kode html. Berikut adalah screenshotnya:

dan ini adalah potongan scriptnya untuk yg html
<body>
<form method="post" action="generate.php">
<p><strong>TABEL GENERATOR</strong></p>
<table width="277" border="0">
<tr>
<td width="89">Rows</td>
<td width="172"><strong>: </strong><input name="RowsTotal" type="text" id="RowsTotal" onKeyUp="getmax();" onfocus="this.select();"></td>
</tr>
<tr>
<td><label>Columns</label></td>
<td><strong>: </strong><input name="ColumnsTotal" type="text" id="ColumnsTotal" onKeyUp="getmax();" onfocus="this.select();"></td>
</tr>
<tr>
<td>Cell Total </td>
<td><strong>: </strong><input name="CellsTotal" type="text" id="CellsTotal" onKeyUp="getmax();" onFocus="this.select();"></td>
</tr>
<tr>
<td>Max Cells </td>
<td><strong>: </strong><input name="maxcells" type="text" id="maxcells" readonly="readonly" style="background-color:#999999">
</td>
</tr>
<tr>
<td>
<div align="center">
<br />
<input type="reset" name="Reset" value="Reset">
</div></td>
<td>
<div align="right">
<br />
<input type="submit" name="Generate" value="Generate!">
</div></td>
</tr>
</table>
</form>

<script language="JavaScript" type="text/javascript">
<!--
function getmax() {
var R = parseInt(document.getElementById('RowsTotal').value);
var C = parseInt(document.getElementById('ColumnsTotal').value);
var X = parseInt(document.getElementById('CellsTotal').value);
var cellmax = document.getElementById('maxcells');
var total = 'N/A';
total = R * C;
cellmax.value = new String(total);
if (X > total)
{
alert('Limit Exceed, max cells is ' + total);
document.getElementById('CellsTotal').value = new String();
}
}
//-->
</script>


dan ini adalah script untuk yang PHPnya
<body>
<?php
$rows = 1;
$columns = 1;
$cells = 1;
?>

<?php $rows = (int) $_POST["RowsTotal"]; ?>
<?php $columns = (int) $_POST["ColumnsTotal"]; ?>
<?php $cells = (int) $_POST["CellsTotal"]; ?>

You pick <?php echo $rows; ?> rows,<br />
You pick <?php echo $columns; ?> columns,<br />
and you need <?php echo $cells; ?> cells,<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>";
?>
</body>

Selengkapnya......
0 komentar

Pass by value dan Pass by reference PHP

Passby1.php
function pass($value, $tambah = 1)
{
$value = $value +$tambah;
}
$value = 10;
pass ($value);
echo $value;
?>
//print out
10
Script diatas akan menghasilkan $value = 10 bukan $value = 11 mengapa ? hal ini disebabkan karena peraturan scope variable. Kode $value = 10 akan membuat sebuah variable bernama value dan bernilai 10, kemudian pada baris pass ($value); fungsi pass dipanggil. Selanjutnya $value = 10 diproses, $value = $value +$tambah;, dengan begitu nilai dari $value = 11, sesuai dengan peraturan scope varibel, $value = 11 hanya akan sampai pada baris terakhir pada fungsi, nilai yang telah diolah tidak akan dapat dipakai di luar, singkatnya bersifat local variable.

Pass by value dan Pass by reference adalah cara bagaimana variable diolah.
Pass by value, variable yang diberikan oleh parameter hanya akan diolah didalam fungsi,
variable yang berada diluar fungsi akan tetap. Dengan kata lain variable yang dipakai oleh suatu
fungsi tidak akan mempengaruhi variable yang ada diluar.
Pass by reference, jika pada pass by value, nilai hasil olahan tidak dapat digunakan diluar fungsi, maka pass by reference sebaliknya, hasil olahan yang telah diproses di dalam fungsi dapat digunakan di luar fungsi, dengan kata lain value yang di luar fungsi akan turut berubah seiring
perubahan yang terjadi pada variable didalam fungsi.
Passbyrefence.php
Function passref(&$value, $tambah=1)
{
$value = $value + $tambah;
}
$x = 10;
Echo “nilai sebelum fungsi ”. $x.”
”;
Passref($x);
Echo “nilai sesudah fungsi ”. $x.”
”;
?>
//print out
nilai sebelum fungsi 10
nilai sesudah fungsi 11

Note :
Pemahaman yang mendalam mengenai scope variable, pass by value dan pass by reference
amatlah penting, karena dalam web development skala besar hal-hal diatas amatlah diperlukan, terutama dalam hal efisiensi kode program.

Selengkapnya......