我只是想問一下,如何從列表順序中的所有id值和數據速率值合併這兩個數組? ul裏面太多了,所以我需要把它們全部存入數組中。如何從數據屬性和id使用javascript添加數組並將它們結合在數組php中?
這是我在javascript代碼:
var h = [];
$("ul.reorder-photos-list li").each(function() { h.push($(this).attr('id').substr(9)); });
var x = [];
$("ul.reorder-photos-list li").each(function() { x.push($(this).attr('data-rate').substr(9)); });
$.ajax({
type: "POST",
url: "order_update.php",
data: {
ids: " " + h + "",
rate: " " + x + ""
},
success: function(html)
{
window.location.reload();
/*$("#reorder-helper").html("Reorder Completed - Image reorder have been successfully completed. Please reload the page for testing the reorder.").removeClass('light_box').addClass('notice notice_success');
$('.reorder_link').html('reorder photos');
$('.reorder_link').attr("id","");*/
}
});
,這是我的html代碼:
<li id="image_li_<?php echo $row['id']; ?>" class="ui-sortable-handle">
<input id="rate" type="text" value="<?= $row['rate']?>" data-rate="<?php echo $row['rate']; ?>" >
,然後在我的order_update.php這是我的代碼。
$idArray = explode(",",$_POST['ids']);
$rateArray = explode(",",$_POST['rate']);
$ids = array();
foreach ($idArray as $id) {
$ids[] = $id;
}
$rates = array();
foreach ($rateArray as $rate) {
$rates[] = $rate;
}
$n = 0;
$orderArray = array();
while($n <= count($idArray))
{
$orderArray[] = array("id" => $ids[$n], "data" => $rates[$n]);
$n++;
}
,這是從orderArray
我插入查詢function updateOrder($orderArray){
$count = 1;
foreach ($orderArray as $array){
$update = mysqli_query($this->connect,"UPDATE `test` SET `order` = $count, `rate`=$array[rate] WHERE id = $array[id]");
$count ++;
}
return true;
}
希望某人幫助我。 :)提前致謝!
請簡化您的代碼。 – Oleander
h是一個不是以逗號分隔的字符串的數組,對吧?與x相同。那麼,ids:「+ h +」,rate:「」+ x +「」'會被打印出來嗎? – developerwjk
實際上,我從逗號分解它,並使其數組。我想結合rateArray到多維數組的id數組中。 @developerwjk – xerwudjohn