 |
ปัญหาการใช้ Datatable แบบ Server-side processing ต้องการให้ข้อมูลอยู่ในบรรทัดเดียวกัน ต้อง join ตารางยังไงคะ |
|
 |
|
|
 |
 |
|
ตาราง po

ตาราง po_detail

ปัญหาตอนนี้คือ จะทำยังไงให้ file ไปอยู่รวมกัน ในบรรทัดเดียว ตอนนี้มันแยกบรรทัดกันอยู่ค่ะ

Code (PHP)
<script type="text/javascript" language="javascript" >
$(document).ready(function() {
var dataTable = $('#employee-grid').DataTable( {
"processing": true,
"serverSide": true,
"Paginate": true,
"LengthChange": false,
"Filter": false,
"Info": false,
"ajax":{
url :"employee-grid-data.php", // json datasource
type: "post", // method , by default get
error: function(){ // error handling
$(".employee-grid-error").html("");
$("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
$("#employee-grid_processing").css("display","none");
}
},
"fnDrawCallback": function () {
//alert('test');
$(".example1").fancybox({
'maxWidth' : 800,
'maxHeight' : 600,
'fitToView' : false,
'width' : '100%',
'height' : '100%',
'autoSize' : false,
'closeClick' : false,
'openEffect' : 'none',
'closeEffect' : 'none',
'type' : 'iframe'
});
$(".example2").attr("rel","gallery").fancybox({
/*'width' : '50%',
'height' : '50%',
'autoScale' : false,
'transitionIn' : 'none',
'transitionOut' : 'none',
'type' : 'iframe',*/
////////print//////////
'afterShow' : function(){
var win=null;
var content = $('.fancybox-inner');
$('.fancybox-wrap')
// append print button
.append('<div id="fancy_print"></div>')
// use .on() in its delegated form to bind a click to the print button event for future elements
.on("click", "#fancy_print", function(){
win = window.open("width=200,height=200");
self.focus();
win.document.open();
win.document.write('<'+'html'+'><'+'head'+'><'+'style'+'>');
win.document.write('body, td { font-family: Verdana; font-size: 10pt;}');
win.document.write('<'+'/'+'style'+'><'+'/'+'head'+'><'+'body'+'>');
win.document.write(content.html());
win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
win.document.close();
win.print();
win.close();
}); // on
} // afterShow
});
}
} );
} );
</script>
<div id="templatemo_content">
<table id="employee-grid" cellpadding="0" cellspacing="0" border="0" class="display" width="100%">
<thead>
<tr>
<th width="5%">ประเภท</th>
<th width="5%">Com</th>
<th width="10%">วันที่ PO</th>
<th width="10%">PO. No.</th>
<th width="10%">QT. No.</th>
<th width="25%">บริษัท</th>
<th>ไฟล์ </th>
<th width="10%">ผู้รับผิดชอบ</th>
<th width="5%">แก้ไข </th>
</tr>
</thead>
</table>
</div>
Code (PHP)
<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "4321";
$dbname = "salecentral";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
mysqli_set_charset($conn,"utf8");
/* Database connection end */
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
$columns = array(
// datatable column index => database column name
0 =>'ptype',
1 =>'date',
2 =>'name_partner',
3 =>'po_no',
4 => 'qt_no',
5 => 'com',
6 => 'file',
7 => 'Ename'
);
// getting total number records without any search
$sql = "SELECT po.id As idPO,date, name_partner, po_no, qt_no, com,Ename,Enname,file,ptype ";
$sql.=" FROM po,partner,employee,po_detail where po.id_partner=partner.id_partner AND po.id_author=employee.Eid AND po.id=po_detail.id_po";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql = "SELECT po.id As idPO,date, name_partner, po_no, qt_no, com,Ename,Enname,file,ptype ";
$sql.=" FROM po,partner,employee,po_detail where po.id_partner=partner.id_partner AND po.id_author=employee.Eid AND po.id=po_detail.id_po AND 1=1";
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql.=" AND ( ptype LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR date LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR name_partner LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR po_no LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR qt_no LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR com LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR file LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR Ename LIKE '%".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$idPO= $row["idPO"];
$nestedData=array();
if($row['ptype']=='po') { $nestedData[] = "PO";}
if($row['ptype']=='ใบเคลม') { $nestedData[] = "ใบเคลม";}
if($row['com']=='CC') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/central.png' height='40px'/>";}
if($row['com']=='CL') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/clwheel.png' height='40px'/>";}
if($row['com']=='DS') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/dreamshop.png' height='40px'/>";}
list($Year,$month,$day) = explode("-",$row['date']);
$nestedData[] = (int)$day."/".(int)$month."/".($Year+543);
$nestedData[] = $row["po_no"];
if($row['qt_no']!= "ประวัติลูกค้า" && $row['qt_no']!= "ข้อมูลSale"){
if (strpos($row['qt_no'],'Rev'))
{ $nestedData[] = "<a class='example1' href='viewEdit$row[com].php?idx=$row[qt_no]'>$row[qt_no]</a>";}
else{ $nestedData[] = "<a class='example1' href='view$row[com].php?idx=$row[qt_no]'>$row[qt_no]</a>";}
} else { $nestedData[] = $row['qt_no'];}
$nestedData[] = $row["name_partner"];
list($name,$type) = explode(".",$row['file']);
if ($type =='pdf') { $nestedData[] = "<a class='example1' href='images/po/$row[file]'><img src='images/po/PDF-icon.png' align='left' width='60px'/></a>";}
else { $nestedData[] = "<a class='example1' href='preview.php?type=po&images=$row[file]'><img src='images/po/$row[file]' width='60px'/></a>";}
$nestedData[] = $row['Ename']." (".$row['Enname'].")";
$nestedData[] = "<a class='example1' href='checkEdit.php?idx=$idPO&po=po' target='_self'><img src='../images/edit.png' width='30' title='แก้ไข' border='0' ></a>";
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format
?>
Tag : PHP, MySQL
|
ประวัติการแก้ไข 2016-11-13 16:38:02
|
 |
 |
 |
 |
Date :
2016-11-13 16:32:32 |
By :
bsaranya |
View :
5445 |
Reply :
16 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
group by ครับ
Code (PHP)
ต่อบรรทัดที่ 49
$sql.=" group by po.id";
//เดาล้วนๆ
|
ประวัติการแก้ไข 2016-11-13 19:40:55
 |
 |
 |
 |
Date :
2016-11-13 19:40:37 |
By :
Luz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
SELECT tb1.po_no, tb2.file AS file1, tb3.file AS file2 FROM po AS tb1 JOIN po_detail AS tb2 ON (tb2.id_po = tb1.id)
LEFT JOIN po_detail AS tb3 ON (tb3.id_po = tb1.id) AND tb3.id != tb2.id GROUP BY tb1.id
ลองรันดูครับ มันได้ ไฟล์ 2 คอลัมภ์ ในแถวเดียวกันไหม
|
 |
 |
 |
 |
Date :
2016-11-14 08:08:28 |
By :
9nonameman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เดิม

ที่อยากให้เป็น คือ ไม่ว่าจะมีกี่ไฟล์ ก็ให้ไฟล์ทั้งหมด แสดงแค่บรรทัดเดียว

ตาราง po กับ po_detail po.id=po_detail=id_po
เดิมใช้วิธีนี้ค่ะ
Code (PHP)
<?
$sql ="SELECT po.id,po.date,po.po_no,po.qt_no,po.id_partner,po.com,po.id_author,employee.Ename,employee.Enname,partner.name_partner, po_detail.id_po
FROM po JOIN partner
ON po.id_partner=partner.id_partner
JOIN employee
ON po.id_author=employee.Eid
LEFT JOIN po_detail
ON po.id=po_detail.id_po
GROUP BY po.id
ORDER BY po.id desc";
$result = mysql_query($sql);
?>
<table cellpadding="0" width="100%" cellspacing="0" border="0" class="display" id="example">
<tr>
<th width="5%">Company</th>
<th width="10%">วันที่ PO</th>
<th width="10%">PO. No.</th>
<th width="10%">QO. No.</th>
<th width="25%">บริษัท</th>
<th>ไฟล์ </th>
</tr>
<? while ($row = mysql_fetch_array($result)) {
$idPO = $row['id'];
$id_partner = $row['id_partner'];
?>
<tr>
<td><? echo $row['com'];?></td>
<td><? echo $row['date'];?></td>
<td><? echo $row['po_no'];?></td>
<td><? echo $row['qt_no'];?></td>
<td><? echo $row['name_partner'];?></td>
<td>
<? $sqlFile="Select * from po_detail where id_po='$idPO'";
$qFile= mysql_query($sqlFile);
while ($rowFile = mysql_fetch_array($qFile)) {
list($name,$type) = explode(".",$rowFile['file']);
if ($type =='pdf') { echo "<a id='example8' href='images/po/$rowFile[file]'><img src='images/po/PDF-icon.png' width='80px'/></a>";}
else { echo "<a id='example3' href='images/po/$rowFile[file]'><img src='images/po/$rowFile[file]' width='80px'/></a>";}
}?>
</td>
</tr>
<? } ?>
</table>
แต่ข้อมูลที่ได้ช้ามาก เลยเปลี่ยนมาใช้แบบ Server-side processing แล้วหนูลองjoin ตารางแล้วมันไม่ได้
เลยอยากจะถามว่าต้องทำยังไงคะ
Code (PHP)
<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "4321";
$dbname = "salecentral";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
mysqli_set_charset($conn,"utf8");
/* Database connection end */
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
$columns = array(
// datatable column index => database column name
0 =>'ptype',
1 =>'date',
2 =>'name_partner',
3 =>'po_no',
4 => 'qt_no',
5 => 'com',
6 => 'file',
7 => 'Ename'
);
// getting total number records without any search
$sql = "SELECT po.id As idPO,date, name_partner, po_no, qt_no, com,Ename,Enname,file,ptype ";
$sql.=" FROM po,partner,employee,po_detail where po.id_partner=partner.id_partner AND po.id_author=employee.Eid AND po.id=po_detail.id_po";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql = "SELECT po.id As idPO,date, name_partner, po_no, qt_no, com,Ename,Enname,file,ptype ";
$sql.=" FROM po,partner,employee,po_detail where po.id_partner=partner.id_partner AND po.id_author=employee.Eid AND po.id=po_detail.id_po AND 1=1";
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql.=" AND ( ptype LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR date LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR name_partner LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR po_no LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR qt_no LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR com LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR file LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR Ename LIKE '%".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$idPO= $row["idPO"];
$nestedData=array();
if($row['ptype']=='po') { $nestedData[] = "PO";}
if($row['ptype']=='ใบเคลม') { $nestedData[] = "ใบเคลม";}
if($row['com']=='CC') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/central.png' height='40px'/>";}
if($row['com']=='CL') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/clwheel.png' height='40px'/>";}
if($row['com']=='DS') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/dreamshop.png' height='40px'/>";}
list($Year,$month,$day) = explode("-",$row['date']);
$nestedData[] = (int)$day."/".(int)$month."/".($Year+543);
$nestedData[] = $row["po_no"];
if($row['qt_no']!= "ประวัติลูกค้า" && $row['qt_no']!= "ข้อมูลSale"){
if (strpos($row['qt_no'],'Rev'))
{ $nestedData[] = "<a class='example1' href='viewEdit$row[com].php?idx=$row[qt_no]'>$row[qt_no]</a>";}
else{ $nestedData[] = "<a class='example1' href='view$row[com].php?idx=$row[qt_no]'>$row[qt_no]</a>";}
} else { $nestedData[] = $row['qt_no'];}
$nestedData[] = $row["name_partner"];
list($name,$type) = explode(".",$row['file']);
if ($type =='pdf') { $nestedData[] = "<a class='example1' href='images/po/$row[file]'><img src='images/po/PDF-icon.png' align='left' width='60px'/></a>";}
else { $nestedData[] = "<a class='example1' href='preview.php?type=po&images=$row[file]'><img src='images/po/$row[file]' width='60px'/></a>";}
$nestedData[] = $row['Ename']." (".$row['Enname'].")";
$nestedData[] = "<a class='example1' href='checkEdit.php?idx=$idPO&po=po' target='_self'><img src='../images/edit.png' width='30' title='แก้ไข' border='0' ></a>";
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format
?>
|
ประวัติการแก้ไข 2016-11-14 13:43:19
 |
 |
 |
 |
Date :
2016-11-14 13:42:29 |
By :
bsaranya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$nestedData[] index น่าจะ 6 คือ คอลัภม์ ที่เอาไว้แสดงไฟล์
- คุณก็ไป ดึง po_detail ในลูป while ด้วยไอดี po
- แล้วก็ลูป เอา ไฟล์ ที่ตาราง po_detail เก็บใส่ไว้ ตัวแปรสักตัว
- หลังจากนั้นก็นำตัวแปรนั้น ไปใส่ใน $nestedData[6]
|
ประวัติการแก้ไข 2016-11-14 15:19:17
 |
 |
 |
 |
Date :
2016-11-14 14:58:20 |
By :
9nonameman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "4321";
$dbname = "salecentral";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
mysqli_set_charset($conn,"utf8");
/* Database connection end */
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
$columns = array(
// datatable column index => database column name
0 =>'ptype',
1 =>'date',
2 =>'name_partner',
3 =>'po_no',
4 => 'qt_no',
5 => 'com',
6 => 'file',
7 => 'Ename'
);
// getting total number records without any search
$sql = "SELECT po.id As idPO,date, name_partner, po_no, qt_no, com,Ename,Enname,file,ptype ";
$sql.=" FROM po,partner,employee where po.id_partner=partner.id_partner AND po.id_author=employee.Eid ";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql = "SELECT po.id As idPO,date, name_partner, po_no, qt_no, com,Ename,Enname,file,ptype ";
$sql.=" FROM po,partner,employee where po.id_partner=partner.id_partner AND po.id_author=employee.Eid AND 1=1";
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql.=" AND ( ptype LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR date LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR name_partner LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR po_no LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR qt_no LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR com LIKE '%".$requestData['search']['value']."%' ";
/*$sql.=" OR file LIKE '%".$requestData['search']['value']."%' ";*/
$sql.=" OR Ename LIKE '%".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$idPO= $row["idPO"];
$nestedData=array();
if($row['ptype']=='po') { $nestedData[] = "PO";}
if($row['ptype']=='ใบเคลม') { $nestedData[] = "ใบเคลม";}
if($row['com']=='CC') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/central.png' height='40px'/>";}
if($row['com']=='CL') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/clwheel.png' height='40px'/>";}
if($row['com']=='DS') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/dreamshop.png' height='40px'/>";}
list($Year,$month,$day) = explode("-",$row['date']);
$nestedData[] = (int)$day."/".(int)$month."/".($Year+543);
$nestedData[] = $row["po_no"];
if($row['qt_no']!= "ประวัติลูกค้า" && $row['qt_no']!= "ข้อมูลSale"){
if (strpos($row['qt_no'],'Rev'))
{ $nestedData[] = "<a class='example1' href='viewEdit$row[com].php?idx=$row[qt_no]'>$row[qt_no]</a>";}
else{ $nestedData[] = "<a class='example1' href='view$row[com].php?idx=$row[qt_no]'>$row[qt_no]</a>";}
} else { $nestedData[] = $row['qt_no'];}
$nestedData[] = $row["name_partner"];
/*เพิ่ม*/
$sqlFile="SELECT * from po_detail where id_po='$idPO'";
$qFile= mysqli_query($conn, $sqlFile);
while ($rowFile = mysql_fetch_array($qFile)) {
$file = $rowFile['file'];
list($name,$type) = explode(".",$file);
if ($type =='pdf') { $nestedData[] = "<a class='example1' href='images/po/$file'><img src='images/po/PDF-icon.png' align='left' width='60px'/></a>";}
else { $nestedData[] = "<a class='example1' href='preview.php?type=po&images=$file'><img src='images/po/$file' width='60px'/></a>";}
}
$nestedData[] = $row['Ename']." (".$row['Enname'].")";
$nestedData[] = "<a class='example1' href='checkEdit.php?idx=$idPO&po=po' target='_self'><img src='../images/edit.png' width='30' title='แก้ไข' border='0' ></a>";
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format
?>

ยังไม่ได้อ่ะค่ะ
|
 |
 |
 |
 |
Date :
2016-11-15 10:40:23 |
By :
bsaranya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่านข้อมูลมาเก็บไว้ใน buffer (array) ก่อน ยังไม่ต้อง echo ออกมา
แล้วใช้ if else ตรวจสอบว่า ใน buffer มี record ที่มีข้อมูลที่สำคัญเหมือนกันหรือไม่
ถ้ามีเหมือนกัน ให้ add tag แสดงผลเพิ่ม เติมใน buffer record เดียวกัน
ถ้าไม่เหมือนกัน ให้ add เป็น buffer record ใหม่
เมือจัดการ buffer เรียบร้อยแล้ว ค่อยอ่านข้อมูลจาก buffer มาพิมพ์
|
 |
 |
 |
 |
Date :
2016-11-15 13:21:22 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องเตรียมข้อมูลก่อนครับ เก็บใส่ อาร์เรย์
ลองทำกับข้อมูลน้อยๆ ดูก่อนนะครับ
|
 |
 |
 |
 |
Date :
2016-11-15 16:30:21 |
By :
Luz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอมีตัวอย่างที่เข้าใจง่ายมั้ยคะ ขอบคุณค่ะ
|
 |
 |
 |
 |
Date :
2016-11-15 19:07:37 |
By :
bsaranya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 8 เขียนโดย : bsaranya เมื่อวันที่ 2016-11-15 19:07:37
รายละเอียดของการตอบ ::
ลองเอาไปปรับใช้ดูครับ ของผม query 2 ครั้ง อาจจะเร็วกว่า(มั้ง)
Code (PHP)
<?php
$sql ="SELECT po.id,po.date,po.po_no,po.qt_no,po.id_partner,po.com,po.id_author,employee.Ename,employee.Enname,partner.name_partner, po_detail.id_po
FROM po JOIN partner
ON po.id_partner=partner.id_partner
JOIN employee
ON po.id_author=employee.Eid
LEFT JOIN po_detail
ON po.id=po_detail.id_po
GROUP BY po.id
ORDER BY po.id desc";
$result1 = mysql_query($sql);
$resultArray = array();
while ($row = mysql_fetch_array($result1)) {
array_push($resultArray,$row);
}
$val = '';
foreach ($resultArray as $po) {
$val.=$po['id'].',';
}
$val = substr($val, 0,strlen($val)-1); // ตัด , ตัวสุดท้าย
$sqlFile="Select * from po_detail where id_po IN ($val)";
$result = mysql_query($sqlFile);
?>
<table cellpadding="0" width="100%" cellspacing="0" border="0" class="display" id="example">
<tr>
<th width="5%">Company</th>
<th width="10%">วันที่ PO</th>
<th width="10%">PO. No.</th>
<th width="10%">QO. No.</th>
<th width="25%">บริษัท</th>
<th>ไฟล์ </th>
</tr>
<?php foreach ($resultArray as $row) {
$idPO = $row['id'];
$id_partner = $row['id_partner'];
?>
<tr>
<td><?php echo $row['com'];?></td>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['po_no'];?></td>
<td><?php echo $row['qt_no'];?></td>
<td><?php echo $row['name_partner'];?></td>
<td>
<?php
while ($rowFile = mysql_fetch_array($result)) {
if($rowFile['id']==$idPO)
{
echo 'files';
}
}?>
</td>
</tr>
<?php } ?>
</table>
|
ประวัติการแก้ไข 2016-11-15 20:46:32
 |
 |
 |
 |
Date :
2016-11-15 20:45:38 |
By :
Luz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ติดตามอ่านมาพักนึง
1. จากที่ดู Code ปัญหาไม่ได้เป็น Server-side processing ของ DataTable ครับ ปัญหาคือ คุณต้องการแสดงค่าให้ออกแค่บรรทัดเดียว ซึ่งผมก็ไม่ค่อยถนัดการ join เท่าไร ก็เลยไม่อาจจะแนะนำอะไรอะไรได้มากครับ
แนะนำ คิดตามง่าย ๆ แค่ join หรือ คิวรี่ ให้ได้ข้อมูลตามที่ต้องการก่อน ยังไม่ต้องไปคิดถึงตัว DataTable ครับ โดย
1) ก็สร้างตาราง table tr td ทดสอบตามปกติให้ค่าที่ต้องการออกมาให้หมดเลยครับ ทำรูปแบบตามที่ต้องการให้ได้ก่อน
2) เรียกใช้งาน jquery ใส่ id ให้ Table เช่น <table id="exam01" จากนั้นเรียกการใช้ dataTable แบบปกติ $('#exam01').DataTable
3) ถ้าหากข้อมูลเยอะเรียกแบบนี้ยอมรับว่าช้า แต่ถ้าทดสอบแบบนี้ จะได้ join ตาราง หรือ แยกการคิวรี่ได้ชัดเจน
2. ตรง Code ผมเห็นมีเงื่อนไขการ Search (ซึ่งไม่รู้ว่าทำอะไร) แต่หลักการของ DataTable เมื่อส่งค่าไม่ว่าจะมาแบบไหน ให้ใช้การ Search ของตัวมันเองจะสะดวก ไม่ต้องมีเพิ่มเงื่อนไขหากไม่จำเป็น ลองพิจารณาดูครับ
3.หากได้ทดสอบตามที่แนะนำ ผมอยากให้ลองหาตัว script ของ DataTable ที่ชื่อว่า ssp.class.php มันจะเป็น class ที่ช่วยในการทำงาน
ภาษาอังกฤษเขาเขียน ว่า Helper functions for building a DataTables server-side processing SQL query
มันสะดวกดีครับ หาจาก google ก็เจอใน GitHub มี ตัวอย่างด้วยครับ
ฉนั้น เพื่อไม่ให้งง ลองทดสอบแค่การ join หรือ คิวรี่ให้ได้ตามต้องการก่อน จากนั้นค่อยไปประยุกต์ใช้กับ Server-side processing ครับ
|
 |
 |
 |
 |
Date :
2016-11-16 10:13:59 |
By :
apisitp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ลองเอาค่า string แตกเป็น array
แต่อยากให้ไฟล์อยู่ตรงช่องค่ะ โดยขึ้นบรรทัดใหม่เรียงลงมา แต่อันนี้เหมือนมันขยับออกไปด้านข้างค่ะ ทำให้ข้อมูลฟิลด์อื่น หายไป
Code (PHP)
$array = explode(',', $row["all_file"]);
foreach(array_unique($array ) as $val){
list($name,$type) = explode(".",$val);
if ($type =='pdf') { $nestedData[] = "<a class='example1' href='images/po/$val'><img src='images/po/PDF-icon.png' align='left' width='60px'/></a>";}
else { $nestedData[] = "<a class='example1' href='preview.php?type=po&images=$val'><img src='images/po/$val' width='60px'/></a>";}
}

|
 |
 |
 |
 |
Date :
2016-11-18 10:00:29 |
By :
bsaranya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ๆคะ
จะสอบถามว่า หนูเอา ตย. มาจาก https://www.thaicreate.com/community/datatables-php-mysql-ajax-basic.html
แล้ว ในตย. หนูก็ทำ search ใน dataTable ตาม ตย. ถ้าอยากจะทำอย่างที่ Mr.กล้า บอก หนูควรทำอย่างไรคะ
2. ตรง Code ผมเห็นมีเงื่อนไขการ Search (ซึ่งไม่รู้ว่าทำอะไร) แต่หลักการของ DataTable เมื่อส่งค่าไม่ว่าจะมาแบบไหน ให้ใช้การ Search ของตัวมันเองจะสะดวก ไม่ต้องมีเพิ่มเงื่อนไขหากไม่จำเป็น ลองพิจารณาดูครับ
Code (PHP)
$sql = "SELECT po.id As idPO,date, name_partner, po_no, qt_no, com,Ename,Enname,file,ptype ";
$sql.=" FROM po,partner,employee where po.id_partner=partner.id_partner AND po.id_author=employee.Eid AND 1=1";
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql.=" AND ( ptype LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR date LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR name_partner LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR po_no LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR qt_no LIKE '%".$requestData['search']['value']."%' ";
$sql.=" OR com LIKE '%".$requestData['search']['value']."%' ";
/*$sql.=" OR file LIKE '%".$requestData['search']['value']."%' ";*/
$sql.=" OR Ename LIKE '%".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
|
ประวัติการแก้ไข 2016-11-24 16:34:35
 |
 |
 |
 |
Date :
2016-11-24 16:19:32 |
By :
bsaranya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อย่างที่บอกผมไม่เก่งเรื่อง คิวรี่ ยิ่งถ้าจะ join ข้อมูลนี่ไม่ไหวครับ งานประจำผมไม่ได้มีตารางอะไรมากมาย...
ปัญหาของคุณให้ลองตัดความคิดเดิมทิ้ง ไปอ่านความเห็นเก่า ที่ผมพยายามอธิบาย แล้วลองทดสอบตามดูครับ
ถ้าจะแกะ DataTable ที่ผมทำไว้ http://www.edu.buu.ac.th/educarservice/pages/?menu=tables
ให้โหลด code จาก Go to : Script การขอใช้รถยนต์ สร้างจาก FullCalendar2,SB-Admin2,bootstrap-editable,fancybox-popup,ภาษาไทย
แต่กลัวไปแกะแล้วจะงง
อันนี้เป็นตัวอย่างcodeแบบง่าย
ในส่วนของไฟล์ server_side.php ส่วนสำคัญต้องเรียก require( 'ssp.class.php' );
ไฟล์ ssp.class.php ให้หาจากต้นฉบับนะครับ ที่ผมทำระบบรถไว้ ผมไม่แน่ใจว่าได้แก้อะไรไปหรือเปล่า
ส่วนการเชื่อมต่อฐานข้อมูลอันนี้ก็ตามปกติ ไม่มีอะไรซับซ้อน
server_side.php
$table = 'book_in';
$primaryKey = 'id';
$columns = array(
array('db' => 'real_date','dt' => 0),
array('db' => 'type_book','dt' => 1),
array('db' => 'book_number','dt' => 2),
array('db' => 'name','dt' => 3),
array('db' => 'replace','dt' => 4),
array('db' => 'to_book','dt' => 5),
array('db' => 'subject_book','dt' => 6),
array('db' => 'name_record','dt' => 7),
array('db' => 'remark_book','dt' => 8),
array('db' => 'id','dt' => 9),
);
$sql_details = array(
'user' => $username,
'pass' => $password,
'db' => $database,
'host' => $hostname
);
require( 'ssp.class.php' ); //สำคัญ
$where = " `book_number` LIKE '%2016%'";
echo json_encode(
SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns, $where)
);
ไฟล์แสดงข้อมูลก็เรียกใช้ไฟล์ css ไฟล์ js ต่างๆ ให้ครบ ตามเว็บหลักที่เขาสอนไว้ครับ
ไฟล์ show.php
<table class="table" id="bookin" >
<thead>
<tr>
<th>Date</th>
<th>Type</th>
<th>No.</th>
<th>From</th>
<th>Replace</th>
<th>To</th>
<th>Title</th>
<th>By</th>
<th>Remark</th>
<th>ref.</th>
</tr>
</thead>
<tbody>
//ข้อมูลมันจะถูกเรียกมาแสดงตรงนี้เอง
</tbody>
</table>
<script>
$(document).ready(function() {
$('#bookin').DataTable( {
"processing": true,
"serverSide": true,
"lengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],
"order": [[ 2, "desc" ]],
"ajax": {
url: 'server_side.php', //ถ้า path อยู่ level เท่ากันก็เรียกได้เลย
dataType: 'json'
}
});
});
</script>
อันนี้คือ ตย.แบบง่ายๆ
ส่วนจะแสดงรูป หรือ อะไรเพิ่มเติมมันจะมี 'formatter' ผมไม่มั่นใจเรียกว่าอะไร
ตย.รูปแบบ
array(
'db' => 'book_number',
'dt' => 2,
'formatter' => function( $d, $row ) {
$nobook = substr($d, 7);
return $nobook;
}
),
array(
'db' => 'name',
'dt' => 3,
'formatter' => function( $d, $row ) {
if($this->session->userdata('userLogData')['iduser'] == $row[10]) :
$name = '
<a href="#" id="name_' . $row[9] . '" data-url="' . site_url('bookin/update_bookin') . '" data-pk="'.$row[9].'" data-name="name" data-value="'.$row[3].'">'.$row[3].'</a>
<script>
$(document).ready(function() {
$("#name_'.$row[9].'").editable({
type: "textarea",
title: "แก้ไข : ขื่อผู้ลงนาม",
success :function(data) {
if(data==1){
window.location.href = "'. site_url('bookin/search/'.substr($row[2],0,4).'').'";
}else{
alert("Error : Update Name.");
}
}
});
});
</script>
';
else:
$name = $row[3];
endif;
return $name;
}
),
สุดท้ายตามที่คุณต้องการผมแนะนำให้ศึกษาเพิ่มเติมจาก ตัวนี้
https://datatables.net/examples/api/multi_filter.html
ลองไล่ที่ผมแนะนำ ทำการทดลองที่ละขั้น จากนั้นค่อยมาเก็บรายละเอียด
ถ้าพื้นฐานมีอยู่แล้ว ไม่ยากที่จะศึกษาครับ
ผมพยายามที่จะไม่โพสต์ code ของ DataTable Server side ครับ มันจะยาว และดูงงบ้าง
แต่ถ้าได้ทดลองทำ มันจะเข้าใจ
ท้ายจริงๆละ กลับไปอ่าน ความเห็นเก่า แล้วทำตามได้อะไรบ้าง
|
ประวัติการแก้ไข 2016-11-24 17:39:17 2016-11-24 17:41:03 2016-11-24 17:45:17
 |
 |
 |
 |
Date :
2016-11-24 17:38:05 |
By :
apisitp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ของหนูไม่มีปัญหาเรื่อง join แล้วค่ะ ข้อมูลโชว์ปกติ กดดูหน้าอื่นก็ปกติ
แต่พอกด search แล้ว มันเพี้ยนไปค่ะ ก็เลยมาถามต่อเรื่อง การเอาเงื่อนไขการ Search ออก
|
 |
 |
 |
 |
Date :
2016-11-24 17:54:08 |
By :
bsaranya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ก็ตามที่แนะนำไปครับ 
|
 |
 |
 |
 |
Date :
2016-11-24 18:39:18 |
By :
apisitp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|