|
 |
|
PHP : รอกวนขอคำเเนะนำถ้าต้องการนำข้อมูลใน DB มาเเสดงเป็น Checkbox โดย Group ตามเเผนกให้เเสดงผลตามรูปจะมีวิธีเขียนอย่างไรครับ |
|
 |
|
|
 |
 |
|
แบบนี้ใช่ไหมครับ ลองดูแบบ loop 2 ชั้นนะครับ อาจจะตอบโจทย์ท่านไม่ได้ แต่ลองเอาไปปรับแก้ดูครับ

ผมทำตารางง่ายๆให้ดูเป็นตัวอย่าง 3 ตารางดังนี้ นะครับ
1.ตาราง department เก็บฝ่ายหรือหน่วยงานของคุณนั่นแหละ

Code (SQL)
-- ----------------------------
-- Table structure for department
-- ----------------------------
DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
`dep_id` varchar(4) NOT NULL,
`dep_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`dep_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of department
-- ----------------------------
INSERT INTO `department` VALUES ('1001', 'หน่วยงานที่ 1');
INSERT INTO `department` VALUES ('1002', 'หน่วยงานที่ 2');
INSERT INTO `department` VALUES ('1003', 'หน่วยงานที่ 3');
INSERT INTO `department` VALUES ('1004', 'หน่วยงานที่ 4');
INSERT INTO `department` VALUES ('1005', 'หน่วยงานที่ 5');
INSERT INTO `department` VALUES ('1006', 'หน่วยงานที่ 6');
2.ตาราง staff เก็บรายชื่อพนักงาน

Code (SQL)
-- ----------------------------
-- Table structure for staff
-- ----------------------------
DROP TABLE IF EXISTS `staff`;
CREATE TABLE `staff` (
`staff_id` varchar(4) NOT NULL,
`staff_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`staff_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of staff
-- ----------------------------
INSERT INTO `staff` VALUES ('S001', 'นาย A');
INSERT INTO `staff` VALUES ('S002', 'นาย B');
INSERT INTO `staff` VALUES ('S003', 'นาย C');
INSERT INTO `staff` VALUES ('S004', 'นาย D');
INSERT INTO `staff` VALUES ('S005', 'นาย E');
INSERT INTO `staff` VALUES ('S006', 'นาย F');
INSERT INTO `staff` VALUES ('S007', 'นาย G');
INSERT INTO `staff` VALUES ('S008', 'นาย H');
INSERT INTO `staff` VALUES ('S009', 'นาย I');
INSERT INTO `staff` VALUES ('S010', 'นาย J');
INSERT INTO `staff` VALUES ('S011', 'นาย K');
INSERT INTO `staff` VALUES ('S012', 'นาย L');
INSERT INTO `staff` VALUES ('S013', 'นาย M');
INSERT INTO `staff` VALUES ('S014', 'นาย N');
INSERT INTO `staff` VALUES ('S015', 'นาย O');
INSERT INTO `staff` VALUES ('S016', 'นาย P');
INSERT INTO `staff` VALUES ('S017', 'นาย Q');
INSERT INTO `staff` VALUES ('S018', 'นาย R');
INSERT INTO `staff` VALUES ('S019', 'นาย S');
INSERT INTO `staff` VALUES ('S020', 'นาย T');
INSERT INTO `staff` VALUES ('S021', 'นาย U');
INSERT INTO `staff` VALUES ('S022', 'นาย V');
INSERT INTO `staff` VALUES ('S023', 'นาย W');
INSERT INTO `staff` VALUES ('S024', 'นาย X');
INSERT INTO `staff` VALUES ('S025', 'นาย Y');
INSERT INTO `staff` VALUES ('S026', 'นาย Z');
3.ตาราง staff_dep ใช้เก็บว่าพนักงานคนไหนอยู่หน่วยงานไหนบ้าง (ถ้าใส่ฟิวด์ dep_id ไปเลยในตาราง staff จะทำให้พนักงาน 1 คนอยู่ได้ 1 หน่วยงานเท่านั้น จึงเกิดตารางนี้ขึ้นมาครับ)

-- ----------------------------
-- Table structure for staff_dep
-- ----------------------------
DROP TABLE IF EXISTS `staff_dep`;
CREATE TABLE `staff_dep` (
`staff_id` varchar(4) NOT NULL,
`dep_id` varchar(4) NOT NULL,
PRIMARY KEY (`staff_id`,`dep_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of staff_dep
-- ----------------------------
INSERT INTO `staff_dep` VALUES ('S001', '1004');
INSERT INTO `staff_dep` VALUES ('S002', '1002');
INSERT INTO `staff_dep` VALUES ('S003', '1006');
INSERT INTO `staff_dep` VALUES ('S004', '1003');
INSERT INTO `staff_dep` VALUES ('S005', '1006');
INSERT INTO `staff_dep` VALUES ('S006', '1005');
INSERT INTO `staff_dep` VALUES ('S007', '1001');
INSERT INTO `staff_dep` VALUES ('S008', '1005');
INSERT INTO `staff_dep` VALUES ('S009', '1005');
INSERT INTO `staff_dep` VALUES ('S010', '1006');
INSERT INTO `staff_dep` VALUES ('S011', '1003');
INSERT INTO `staff_dep` VALUES ('S012', '1002');
INSERT INTO `staff_dep` VALUES ('S013', '1004');
INSERT INTO `staff_dep` VALUES ('S014', '1002');
INSERT INTO `staff_dep` VALUES ('S015', '1006');
INSERT INTO `staff_dep` VALUES ('S016', '1003');
INSERT INTO `staff_dep` VALUES ('S017', '1001');
INSERT INTO `staff_dep` VALUES ('S018', '1005');
INSERT INTO `staff_dep` VALUES ('S019', '1003');
INSERT INTO `staff_dep` VALUES ('S020', '1002');
INSERT INTO `staff_dep` VALUES ('S021', '1001');
INSERT INTO `staff_dep` VALUES ('S022', '1004');
INSERT INTO `staff_dep` VALUES ('S023', '1004');
INSERT INTO `staff_dep` VALUES ('S024', '1002');
INSERT INTO `staff_dep` VALUES ('S025', '1001');
INSERT INTO `staff_dep` VALUES ('S026', '1004');
PHP Code นะครับ
Code (PHP)
<?php
require_once("conn.php");
//############## อย่าลืมประกาศตัวแปรที่ใช้เชื่อมต่อของคุณมาเองนะครับในที่นี้ใช้ตัวแปรชื่อว่า $conn ###############
$html=""; // เอาไว้เก็บ html string ที่ต่อๆกันแล้วไป echo ทีเดียว
$html .='<html>';
$html .='<head>';
$html .='<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
$html .='<title>Loop to controls</title>';
$html .='</head>';
$html .='<body>';
$html .='<form method="post" action="">';
$html .='<TABLE border="1">';
$html .='<tr>';
$html .='<td align="center"><b>รหัสหน่วยงาน</b></td>';
$html .='<td align="center"><b>ชื่อหน่วยงาน</b></td>';
$html .='<td align="center"><b>พนักงานในหน่วยงาน</b></td>';
$html .='</tr>';
$sql_dep ="SELECT dep_id,dep_name FROM department ORDER BY dep_id;";
$query_dep = mysql_query($sql_dep,$conn) or die(mysql_error());
while ($dep_row = mysql_fetch_array($query_dep)) {
$html .='<tr>';
$html .='<td>'.$dep_row[dep_id].'</td>';
$html .='<td>'.$dep_row[dep_name].'</td>';
$html .='<td width="400px;">';
//############Loop Staff of Department Here###############
$sql_staff=" SELECT staff.staff_id, staff.staff_name ";
$sql_staff.=" FROM staff_dep INNER JOIN staff ON staff.staff_id = staff_dep.staff_id ";
$sql_staff.=" WHERE staff_dep.dep_id='".$dep_row[dep_id]."'; ";
$query_staff = mysql_query($sql_staff,$conn) or die(mysql_error());
while ($staff_row = mysql_fetch_array($query_staff)) {
$html .='<input type="checkbox" id="'.$staff_row[staff_id].'" value="'.$staff_row[staff_id].'">'.$staff_row[staff_name];
}
//############End Loop Staff of Department Here###########
$html .='</td>';
$html .='</tr>';
}
$html .='</TABLE>';
$html .='</form>';
$html .='</body>';
$html .='</html>';
echo $html; //response html stream
?>
|
 |
 |
 |
 |
Date :
2014-09-26 16:03:45 |
By :
โปรแกรมมั่ว |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลูปซ้อนลูปไปเลยครับ ไม่ยุ่งยาก แค่ 2 ลูปได้ผลลัพท์
ภาพโฟลล์ชาร์จในเวอร์ชั่นแบบกันเอง  

|
 |
 |
 |
 |
Date :
2014-09-26 16:46:52 |
By :
meannerss |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
mysql นะ
Code (SQL)
select dep,group_concat(concat('<input type="checkbox" name="',emp_id,'" >',emp_name)) as checkbox from employee group by dep
Code (PHP)
while($rs = $db.......){
echo $rs['dep'].$rs['checkbox']
}
ms
ใช้
for xml path ลองหาดูครับ
|
ประวัติการแก้ไข 2014-09-26 17:10:23 2014-09-26 17:12:14
 |
 |
 |
 |
Date :
2014-09-26 17:05:27 |
By :
gaowteen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้เเล้วครับ ขอบคุณทุก Comment..
|
 |
 |
 |
 |
Date :
2014-09-27 09:08:00 |
By :
junior_dev |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|