foreach($_POST['id'] as $title_id => $order) {
$sql = "UPDATE db_links SET title_id = '$title_id' WHERE id = '$order'";
mysql_query($sql) or die (mysql_error());
}
I need to update two values in database: $lingi_sort_no and $title_id. I would appreciate some help
//The UL List
/*
<ul><li><input type="hidden" name="" value="<?php echo $value1;?>" class="first"/><input type="hidden" name="" value="<?php echo $value2;?>" class="second"/><a></a></li></ul>
*/
$(function () {
$('.drag').sortable({
connectWith: '.drag',
update : function(e, ui) {
var index = 0;
var _data = []; // array of values
ul.find('> li').each(function() {
index++;
var _val1 = $(this).find('input.first').val();
var _val2 = $(this).find('input.second').val();
var _str = index+','+val1+','+val2; // save data as comma separated string
_data.push(_str);
});
//Conver array of data to string to send it to php
var data = _data.join(';'); //this will create a string like this data='index1,data1,data2;...;indexn,datan1,datan2'
//You dont need dataType json if you only spect a confirmation from php
$.ajax({
url : 'UpdateLink.php',
data : 'data='+data,
type : 'POST',
success : function(data,status) {
if(status == 'success'){
alert(data);
}
},
error : function() {
alert('There was a problem');
}
});
}
});
});
//The PHP file
<?php
$data = $_POST['data'];
//Create first array
$items = explode(';',$data); // This create the first array with avery item from the UL
foreach($items as $item){
$values = explode(',',$item); // This create the second array to separate the values
$id = $values[0]; // The order of the li item from the ul list
$title = $values[1]; //The title of the item, Here is better to use the unique id of your table
//I changed the MySQL sentence, but I think you need a unique id to refer to, instead the title
$sql = "UPDATE db_links SET order ='$id' WHERE title_id= '$title'";
mysql_query($sql) or die (mysql_error());
}
?>
$(function () {
$('.drag').sortable({
connectWith: '.drag',
update : function(e, ui) {
var index = 0;
var _data = []; // array of values
ul.find('> li').each(function() {
index++;
var _val1 = $(this).find('input.first').val();
var _val2 = $(this).find('input.second').val();
var _str = index+','+val1+','+val2; // save data as comma separated string
_data.push(_str);
});
//Conver array of data to string to send it to php
var data = _data.join(';'); //this will create a string like this data='index1,data1,data2;...;indexn,datan1,datan2'
//You dont need dataType json if you only spect a confirmation from php
$.ajax({
url : 'UpdateLink.php',
data : 'data='+data,
type : 'POST',
success : function(data,status) {
if(status == 'success'){
alert(data);
}
},
error : function() {
alert('There was a problem');
}
});
}
});
});
And finally the PHP code:
PHP Code:
<?php
$data = $_POST['data'];
//Create first array
$items = explode(';',$data); // This create the first array with avery item from the UL
foreach($items as $item){
$values = explode(',',$item); // This create the second array to separate the values
$id = $values[0]; // The order of the li item from the ul list
$title = $values[1]; //The title of the item, Here is better to use the unique id of your table
//I changed the MySQL sentence, but I think you need a unique id to refer to, instead the title
$sql = "UPDATE db_links SET order ='$id' WHERE title_id= '$title'";
mysql_query($sql) or die (mysql_error());
}
Bookmarks