จากตัวอย่าง
Code
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
</tr>
<?
}
?>
</table>
ไฟล์ที่ 1 function.php
Code
//functon.php
function connect($host,$user,$password,$database,$encod){
$objConnect = mysql_connect($host,$user,$password) or die("Error Connect to Database");
$objDB = mysql_select_db($database);
mysql_query($encod);
}
function maketable($query, $fieldarray){
//count number of columns
$columns = count($fieldarray);
//run the query
$result = mysql_query($query) or die(mysql_error()) ;
$itemnum = mysql_num_rows($result);
?><table border=1 ><?
if($itemnum > 0){
do{
echo "<tr>";
for($x = 0; $x < $columns; $x++){
echo "<th width=90>".$items[$fieldarray[$x]];
echo "</th>";
}
echo "</tr>";
}while($items = mysql_fetch_array($result));
}
}
?>
ไฟล์ที่ 2 showtable.php
Code
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<table border = 1>
<?
$fieldarray = array("CustomerID","Name","Email");
require_once("function.php");
connect("localhost","root","mypasswd","mydatabase","SET NAMES UTF8");
maketable("SELECT * FROM customer", $fieldarray);
?>
</table >
</body>
</html>
ข้อมูลแสดง แต่หัวตารางไม่แสดงครับ
Tag : PHP, MySQL