jQuery UI将排序列表保存到PHP数组

前端之家收集整理的这篇文章主要介绍了jQuery UI将排序列表保存到PHP数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图用jQuery UI(可排序)将表的顺序保存到 PHP数组中.

我已经非常简化了,但这是它的基本想法.我有一个表中嵌有可排序的列表.该表通过PHP foreach生成,涉及包含在另一个文件(config.PHP)中的多维数组.

config.PHP文件

  1. <?PHP
  2. $config = array(
  3. "mno" => array('item 5'),"abc" => array('item 1'),"ghi" => array('item 3'),"pqr" => array('item 6'),"jkl" => array('item 4'),"vwx" => array('item 8'),"def" => array('item 2'),"stu" => array('item 7'),);
  4. ?>

table(index.html):

  1. <table cellpadding="2" cellspacing="0" align="center" id="mytable">
  2. <tbody>
  1. <?PHP
  2. $i = 0;
  3. include 'config.PHP';
  4. foreach($config AS $name => $value){
  5. $item = $value[0];
  6. echo '
  7. <tr id="'.$name.'-'.$i++.'">
  8. <td>'.$item.'</td>
  9. </tr>';
  10. }
  11. ?>
  1. </tbody>
  2. </table>

scripts(index.html):

  1. <!-- Add jQuery library -->
  2. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  3. <!-- Add jQuery UI library -->
  4. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
  5. <script type="text/javascript">
  6. $(document).ready(function() {
  7. var fixHelper = function(e,ui) {
  8. ui.children().each(function() {
  9. $(this).width($(this).width());
  10. });
  11. return ui;
  12. };
  13.  
  14. $("#mytable tbody").sortable({
  15. helper: fixHelper,opacity: 0.5,scroll: false,update: function () {
  16. var data = $('#mytable tbody').sortable('serialize');
  17. $.post("edit.PHP",{'neworder': data});
  18. }
  19. }).disableSelection();
  20. });
  21. </script>

排序工作正常,但我不知道如何将新的顺序值($_POST [‘neworder’])保存到数组中是什么在config.PHP中.

我想我必须用file_put_contents的组合使用uasort()(或uksort(),uksort())的PHP函数来保存config.PHP中的新命令.

所以这样的事情

  1. <?PHP
  2. if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['neworder'])) {
  3. /*
  4. Here file_put_contents in config.PHP the new order. So:
  5. $config = array(
  6. "mno" => array('item 5'),);
  7.  
  8. Becomes:
  9. $config = array(
  10. "abc" => array('item 1'),"mno" => array('item 5'),);
  11.  
  12. After this is send by Jquery UI:
  13. neworder:abc[]=1&def[]=6&ghi[]=2&jkl[]=4&mno[]=0&pqr[]=3&stu[]=7&vwx[]=5
  14.  
  15. I've tried this:
  16. $filename = 'config.PHP';
  17.  
  18. $lines = file( $filename,FILE_IGNORE_NEW_LINES );
  19. $linenumber = 2;
  20. foreach( $_POST['neworder'] AS $name => $val){
  21. $phost = $val[0];
  22.  
  23. $lines[$linenumber] = ' "'.$name.'" => array(\'' . $phost . '\'),';
  24. $linenumber++;
  25. }
  26.  
  27. file_put_contents( $filename,implode( "\n",$lines ) );
  28.  
  29. But the '$val' is not send with Jquery only the order.
  30.  
  31. */
  32. }
  33. ?>

解决方法

您将要使用带有关闭的usort(可在PHP 5.3中获取)以按需要的顺序获取密钥.
  1. $newOrder = $_POST["neworder"];
  2. $config_keys = array_keys($config);
  3. usort($config_keys,function($a,$b) use($newOrder) {
  4. return array_search($a,$newOrder) - array_search($b,$newOrder);
  5. });

然后,您可以将$config重新写入新订单

  1. $newConfig = array();
  2.  
  3. foreach($config_keys as $key){
  4. $newConfig[$key] = $config[$key];
  5. }
  6. $config = $newConfig;
  7. unset($newConfig);

从这里你可以坚持使用任何一种方法对于你的用例最有意义的$config.我建议不要使用它创建一个PHP文件,但更好的方法可能是使用

  1. file_put_contents($cacheFile,serialize($config));

然后检索

  1. $config = unserialize(file_get_contents($cacheFile));

猜你在找的jQuery相关文章