วันเสาร์ที่ 23 ตุลาคม พ.ศ. 2553

display records multi-columns algorithm by num


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="th" xml:lang="th">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>records multi-columns template</title>
<style type="text/css">
/*<![CDATA[*/
.cell {background-color:#CCCC00;border:solid green 2px;height:100px;width:100px;}
/*]]>*/
</style>
</script>
</head>
<body>

ตารางหลาย column แบบ dynamic มีการปิด tag ที่ถูกต้องแสดงได้ใน dw และผ่าน xhtml valdation
<?php
//ไม่ได้ใช้ database นะครับ แต่สามารถนำไปประยุกต์ใช้กับ database ได้ทันที
//Hint: เปลี่ยน array_pop เป็น mysql_fetch_assoc, $num_rows เปลี่ยนเป็น mysql_num_rows

//ส่วน configuration
$num_rows = 30; //จำลองแถวใน database
$nc = 5; //จำนวน column

//ส่วนสร้างข้อมูลจำลอง ถ้าใช้กับ database จะไม่มีส่วนนี้แต่จะเปลี่ยนเป็นการใช้คำสั่ง mysql_query ฯลฯ
$row = array('ID'=>'1','name'=>'test'); //ข้อมูลจำลอง
$rows = array_fill(0,$num_rows,$row); //สร้างข้อมูลจำลอง จำนวน $num_rows แถว
?>
<?php if ($num_rows > 0):?>
<table border="0">
<?php $i=0; while($r = array_pop($rows)): if($i%$nc==0):?><tr><?php endif;?>
<td>
<div class="cell"><?php echo $r['name'];?><!-- แสดงข้อมูลใน div --></div>
</td>
<?php if($i%$nc==$nc-1):?></tr><?php endif;$i++;endwhile;$i--;if($i%$nc!=$nc-1)echo'</tr>';?>
</table>
<?php else:?>
no any records.
<?php endif;?>

</body>
</html>

ไม่มีความคิดเห็น:

แสดงความคิดเห็น