|
 |
|
สอบถามเรื่องการสลับแถวข้อมูล แล้ว update ลงฐานข้อมูลหน่อยครับ |
|
 |
|
|
 |
 |
|
ข้อมูลให้มาไม่ครบ ก็สร้างเป็นตัวอย่าง ให้พิจารณา แล้วกัน
ประยุกต์เอาตาม database driver ที่ใช้ เด้อครับ
Code (PHP)
// javascript jquery
$(document).delegate('tr', 'click', function(e){
let tr = this, o = $(e.target);
if( o.hasClass('arrow-up'){
ar = { id: '?' , op: 'up' }
$.ajax({ url: '/update.php', dataType: 'json', data: ar, success: r=>{
if( r.success ){
$(tr).insertBefore( $(tr).prev(); )
}else alert( r.error );
}});
}else if( o.hasClass( 'arrow-dn') {
ar = { id: '?' , op: 'dn' }
$.ajax({ url: '/update.php', dataType: 'json', type:'post', data: ar, success: r=>{
if( r.success ){
$(tr).insertAfter( $(tr).next(); )
}else alert( r.error );
}});
}
}
/* หมายเหตุ
เครื่องหมายคำถาม หาที่มาเอาเอง
*/
Code (PHP)
// update.php ตัวรับ
// ต้องมี field กำหนด priority view อันไหนก่อนหลัง ตัวอย่างนี้ ใช้ field ชื่อ step
<?php
$con = parser_database_connect( $host, $db, $user, $psw ); // ปรับเอาตาม database ที่ใช้งาน
$rq = (object)$_POST;
$ro = $con->query( 'select * from table_name where id = ? ', $rq->id)->row(); // ปรับการคิวรี่ ตาม สภาพแวดล้อม ของ driver sql ที่ใช้
$er=''; // รับ ข้อมูล error
switch( $rq->op ){
case 'up':
$ro_step = $con->query( 'select * from table_name where step < ? order by step desc limit 1 ', $ro->step )->row()??(object)['id'=>false];
// ปรับการคิวรี่ ตาม สภาพแวดล้อม ของ driver sql ที่ใช้
if( ! $ro_step->id ){ // กรณี เป็น อันดับแรก ค้นหาไ่เจอ step ก่อนหน้า
$er = 'เป็นรายการ แรกอยู่แล้ว';
}
break;
case 'dn':
$ro_step = $con->query( 'select * from table_name where step > ? order by step asc limit 1 ', $ro->step )->row();
// ปรับการคิวรี่ ตาม สภาพแวดล้อม ของ driver sql ที่ใช้
if( ! $ro_step->id ){ // กรณี เป็น อันดับแรก ค้นหาไ่เจอ step ก่อนหน้า
$er = 'เป็นรายการ สุดท้ายอยู่แล้ว';
}
break;
}
if( strlen($er)<1 ){
$con->query( 'update table_name set step = ? where id = ? ', $ro_step->step, $ro->id );
// ปรับการคิวรี่ ตาม สภาพแวดล้อม ของ driver sql ที่ใช้
$con->query( 'update table_name set step = ? where id = ? ', $ro->step, $ro_step->id );
// ปรับการคิวรี่ ตาม สภาพแวดล้อม ของ driver sql ที่ใช้
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode((object)['success'=>$er ? 0 : 1, 'error'=>$er]);
|
ประวัติการแก้ไข 2024-12-06 14:20:13 2024-12-06 14:26:19
 |
 |
 |
 |
Date :
2024-12-06 14:18:45 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|