存储生成的输入字段的排名

发布于 2024-12-09 11:38:14 字数 1238 浏览 0 评论 0原文

我陷入了一些我自己无法解决的问题:(不幸的是......我需要在我的表中存储 jQuery 生成的一些输入字段的排名,让我解释一下:

当我单击按钮时,我添加一个输入字段在我的页面中,生成的每个输入字段都有一个递增的名称(name0,name1 ...)和一个递增的id(item-1,item-2等)

我使用jQuery的可排序功能,一切都很好,我拖动放弃我的田地,他们有 我序列

化所有内容,它给了我: item[]=0&item;[]=2&item;[]=1&item;[]=3 每个字段都有一个排名。我的数据库中的这个排名与关联的字段(我的表看起来像这样:“id”“排名”“id”字段自动递增)

这是我的代码:

添加字段的 JS 函数:

$(function() {

 var item = 'item-';
 var i = 0;
 var k = 0;
 var deletebtn = "<div id='deletebtn'>delete this</div>";

 $("#button_add").live("click", function(){
   $('#reorder').append("<li id="+ item + i++ +" class='list_item'>" + "<input type='text' name='title"+ k++ +"' value='' kind='title' />" + deletebtn + "</li>");
 });
}); 

TO获取序列化结果:

newRank = $("#reorder").sortable("serialize");
 $("#rank").val(newRank); 

这链接到一个输入字段以获取序列化值

<input type="hidden" id="rank" name="rank" value="" />

PHP TO $_POST 输入字段:

    $rank = $this->input->post('rank'); 

// THIS WILL GIVE ME THE RESULT : item[]=0&item;[]=2&item;[]=1&item;[]=3

是否有想法将与其 id 相对应的每个字段的排名存储在我的数据库中? !!

I’m stuck on something that I can’t fix by myself :( Unfortunately… I need to store a rank from some inputs fields generated by jQuery in my TABLE, let me explain :

When I click on a button I add an input field in my page, each input field generated have an incremented name (name0, name1 ...) and an incremented id (item-1, item-2 and so on)

I use the sortable function of jQuery and everything is okay, I drag and drop my fields and they have their order.

I serialize everything and it gives me that : item[]=0&item;[]=2&item;[]=1&item;[]=3 each field have a rank. Well now I need to store this rank in my DB WITH the field associated (my TABLE look like this : ‘id’ ‘rank’ the ‘id field is auto incremented)

Here’s my code :

THE JS FUNCTION TO ADD FIELDS :

$(function() {

 var item = 'item-';
 var i = 0;
 var k = 0;
 var deletebtn = "<div id='deletebtn'>delete this</div>";

 $("#button_add").live("click", function(){
   $('#reorder').append("<li id="+ item + i++ +" class='list_item'>" + "<input type='text' name='title"+ k++ +"' value='' kind='title' />" + deletebtn + "</li>");
 });
}); 

TO GET THE SERIALIZE RESULT :

newRank = $("#reorder").sortable("serialize");
 $("#rank").val(newRank); 

This is linked to an input field to get the serialized value

<input type="hidden" id="rank" name="rank" value="" />

THE PHP TO $_POST THE INPUT FIELD :

    $rank = $this->input->post('rank'); 

// THIS WILL GIVE ME THE RESULT : item[]=0&item;[]=2&item;[]=1&item;[]=3

Any idea to store the rank of each field corresponding to their id in my DB ? Any help would be very very appreciated !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2024-12-16 11:38:14

如果我理解正确,您当然可以从 $rank 创建一个数组并循环遍历您的数据库插入:

$ranks = explode("&", $rank); // gives an array 

foreach ($ranks as $r)
{
    // you'll have to do some string manipulation here
    $num = str_replace("item[]=", "", $r);

    // $num should now be a number - add it to your db below
    // ...
}

您的 $rank 中有一些额外的分号,但不确定是否存在是不是拼写错误。

If I'm understanding you correctly, you could of course create an array from $rank and loop through for your db inserts:

$ranks = explode("&", $rank); // gives an array 

foreach ($ranks as $r)
{
    // you'll have to do some string manipulation here
    $num = str_replace("item[]=", "", $r);

    // $num should now be a number - add it to your db below
    // ...
}

There are some extra semi-colons in your $rank, but not sure if that is a typo or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文