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

标签 php jquery jquery-ui multidimensional-array foreach

我正在尝试使用 jQuery UI(可排序)将表的顺序保存到 PHP 数组。

我已经大大简化了它,但这是它的基本思想。我有一个表格,其中嵌入了一个可排序列表。该表是通过包含在另一个文件 (config.php) 中的多维数组的 PHP foreach 生成的。

config.php:

<?php
$config     = array(
    "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'),
);
?>

表(index.html):

<table cellpadding="2" cellspacing="0" align="center" id="mytable">
    <tbody>
<?php
    $i = 0;
    include 'config.php';
    foreach($config AS $name => $value){
        $item = $value[0];
        echo '
        <tr id="'.$name.'-'.$i++.'">
            <td>'.$item.'</td>
        </tr>';
    }
?>
    </tbody>
</table>

脚本(index.html):

<!-- Add jQuery library -->
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<!-- Add jQuery UI library -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var fixHelper = function(e, ui) {
            ui.children().each(function() {
                $(this).width($(this).width());
            });
            return ui;
        };

        $("#mytable tbody").sortable({
            helper: fixHelper,
            opacity: 0.5,
            scroll: false,
            update: function () {
                var data = $('#mytable tbody').sortable('serialize');
                $.post("edit.php", {'neworder': data});
            }
        }).disableSelection();
    });
</script>

排序工作正常,但我不知道如何将新订单值 ($_POST['neworder']) 保存到 config.php 中的数组中>。

我认为我必须结合使用 PHP 函数 uasort()(或 uksort()uksort())file_put_contents 将新订单保存在 config.php 中。

所以像这样:

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['neworder'])) {
    /* 
    Here file_put_contents in config.php the new order. So:
    $config     = array(
        "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'),
    );

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

    After this is send by Jquery UI:
    neworder:abc[]=1&def[]=6&ghi[]=2&jkl[]=4&mno[]=0&pqr[]=3&stu[]=7&vwx[]=5

    I've tried this:
        $filename = 'config.php';

        $lines = file( $filename , FILE_IGNORE_NEW_LINES );
        $linenumber = 2;
        foreach( $_POST['neworder'] AS $name => $val){
            $phost = $val[0];

            $lines[$linenumber] = ' "'.$name.'" => array(\'' . $phost . '\'),';
            $linenumber++;
        }

        file_put_contents( $filename , implode( "\n", $lines ) );

    But the '$val' is not send with Jquery only the order.

    */  
}
?>

最佳答案

您将要使用带闭包的 usort(在 php 5.3+ 中可用)以按照您需要的顺序获取 key 。

$newOrder = $_POST["neworder"];
$config_keys = array_keys($config);
usort($config_keys, function($a, $b) use($newOrder) {
      return array_search($a, $newOrder) - array_search($b, $newOrder);
});

然后你可以把rewrite $config改成新的命令

$newConfig = array();

foreach($config_keys as $key){
    $newConfig[$key] = $config[$key];
}
$config = $newConfig;
unset($newConfig);

从这里您可以以任何对您的用例最有意义的方法保留 $config。我建议不要使用它来创建一个 php 文件,更好的方法可能是使用

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

然后检索

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

关于php - jQuery UI 将可排序列表保存到 PHP 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17252575/

相关文章:

javascript - 显示/隐藏 2 个选择菜单的问题

jQuery 移动 ScrollView

javascript - defaultDate 选项与 setDate 方法之间的区别

javascript - jQuery UI Widget Factory 和原型(prototype)?

php - 客户模型中 Magento 中的 loadByEmail 和加载方法问题

php - 如果 Woocommerce 上的购物车中有特定商品,请更改购物车商品价格

jquery - 居中对齐图像

php - 在 Woocommerce 结账时添加总折扣券金额

php - 使用ajax加载laravel View

jquery - 使用带有选择选项的 jquery ui 选项卡传递变量