分拣不当?

时间:2017-10-06 14:52:04

标签: php mysql ajax

我在MySql表page_order中有一个单元格。当我使用php表单添加新行时,没有任何内容写入此单元格。 我在数据库中使用了带有记录的jQuery sortable,当我们移动项目时,则更新此单元格page_order

以下是我的代码的一部分

 <?php
                        include 'dbs.php';
                        $db = new DB();
                        $users = $db->getRows('artists',array('order_by'=>'page_order ASC'));

                        if(!empty($users)): $count = 0; foreach($users as $user): $count++;
                    ?>
                <?php $idd = $_GET['id']; ?>
               <?php if(($user['interline'] == NULL) && $user['event_id'] == $idd){ ?>


    <div class="action-box" id="<?php echo $user['id']; ?>">
            <div class="action-box-count">
                 <div class="count-number"><?php echo $count; ?></div>
                 <div class="count-time">
                     <div class="count-time-top">15:37 <span>21</span></div>
                     <div class="count-time-bottom">15:50 <span>29</span></div>
                 </div>
             </div>
           <div class="action-box-info">
              <p class="user-name"><?php echo $user['fullname']; ?></p>
              <p class="user-info"><?php echo $user['artist1']; ?></p>
              <p class="user-info"><?php echo $user['artist2']; ?></p>
            </div>
          </div>

     <?php } ?>
     <?php if(($user['interline'] != NULL) && $user['event_id'] == $idd){ ?>



    <div class="action-box action-end" id="<?php echo $user['id']; ?>">
              <div class="action-box-count">
                 <div class="count-number"><?php echo $count; ?></div>
             </div>  
            <div class="action-box-info">
              <div class="text-center"><?php echo $user['interline']; ?></div>
            </div>
         </div>
      <?php } ?>


  <?php endforeach; else: ?>
                    <tr><td colspan="5">Not found......</td></tr>
                    <?php endif; ?>

   <a href="javascript:void(0);" id="addLink" onclick="javascript:$('#openForm').toggle();"><div class="action-box action-add"></div></a>
   <?php  
$idint = $_GET[id];
echo "<a class='action-box action-add-small' href=\"interline.php?idi=".$idint."\"><div class='text-center'><span class='icon'>Interline</span></div> </a>";

?>


   <input type="hidden" name="page_order_list" id="page_order_list" />
</div>
</div>

然后我在处理时使用ajax在文件中发送请求:

for($i=0; $i<count($_POST["page_id_array"]); $i++)
{
 $query = "
 UPDATE artists 
 SET page_order = '".$i."' 
 WHERE id = '".$_POST["page_id_array"][$i]."'";
 mysqli_query($connect, $query);
}

的Ajax

$(document).ready(function(){
 $( "#order_list" ).sortable({
  placeholder : "ui-state-highlight",
  update  : function(event, ui)
  {
   var page_id_array = new Array();
   $('#order_list div').each(function(){
    page_id_array.push($(this).attr("id"));
   });
   $.ajax({
    url:"order.php",
    method:"POST",
    data:{page_id_array:page_id_array},
    success:function(data)
    {
     alert(data);
    }
   });
  }
 });


});

问题是: 1.写入db的错误步骤,即不是1,2,3,4,5,但如下:10,22,27,30,33 2.第二个问题是,当我检查<?php if(($user['interline'] != NULL)输出另一个字符串时 - 排序和数字通常表现得很糟糕。

请告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您看到的差距来自这一部分:

stdlib.h

您正在$('#order_list div').each(function(){ page_id_array.push($(this).attr("id")); }); 内选择所有div。这将为您的数组填充所有没有ID的div的空值...由于您在索引中导致间隙,这会导致带有间隙的有序列表。 (不管怎样它真的很重要,因为它们只是为了订购......)

#order_list

在这里,仅仅定位动作框(似乎持有ID)将确保您迭代正确的div。