Saturday, August 29, 2009

Table Dynamis tanpa Echo Nama Field dengan PHP dan MySQL

Maksud dari tutorial ini adalah kita tidak perlu lagi membuat skript untuk nama nama kolom yang sering digunakan dalam pemrograman PHP MySQL..

Contoh :




<?php
include("conn.php");
//$query = "select field1, fieldn from table [where clause][group by clause][order by clause][limit clause]";

if (empty($_GET[action])){
$query = "select KecId as ID, KecCode, KecDesc, RegionID, RegionCode from m_kecamatan";
$result = mysql_query($query);
if (($result)||(mysql_errno == 0))
{
echo "<table width='90%' border=1 align=center><tr>";
if (mysql_num_rows($result)>0)
{
//loop thru the field names to print the correct headers
$i = 0;
while ($i < mysql_num_fields($result))
{
echo "<th>". mysql_field_name($result, $i) . "</th>";
$i++;
}
echo "<td align='center'><strong>Keterangan</strong></td>"; //mypoints --> picas
echo "</tr>";

//display the data
$j = 0;
while ($rows = mysql_fetch_array($result,MYSQL_NUM))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='left'>". $data . "</td>";
$j++;
}
echo "<td align='left'><a href=fieldname.php?action=edit&id=".$rows[0].">Edit</a> | <a href=fieldname.php?action=delete&id=".$rows[0].">Delete</a> </td>"; //mypoints --> picas
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
echo "</table>";
}else{
echo "Error in running query :". mysql_error();
}

}
?>


Jangan lupa file koneksinya..

<?
$server = "localhost";
$username = "root";
$password = "picas";
$database = "db_combo";

// Koneksi dan memilih database di server
mysql_connect($server,$username,$password) or die("Koneksi gagal");
mysql_select_db($database) or die("Database tidak bisa dibuka");
?>



dan contoh hasilnya...



Oh Ya..hampir lupa..
Nama Field bisa di Customize dengan Query semisal

----> Select nm_kec as Nama Kecamatan from ....

0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Sweet Tomatoes Printable Coupons