2013-06-22 21 views
10

jQuery UI(ソート可能)を使用してテーブルの順序をPHP配列に保存しようとしています。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> 

ソートが正常に動作しますが、私はconfig.phpにあるもの配列にneworder値($_POST['neworder'])を保存する方法がわかりません。

私はconfig.phpに新しい秩序を保存するfile_put_contentsの組み合わせで(uksort()、またはuksort())PHP関数uasort()を使用しなければならないと思います。

したがって、このような何か:あなたはあなたがそれらを必要とするために、キーを取得するために(PHPで利用可能5.3+)クロージャとusort使用したいしようとしている

<?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. 

    */ 
} 
?> 
+1

データをPHPファイルに保存することは悪い考えです。それらをいくつかのjsonまたはxml形式で格納することができます。大規模/頻繁に行う必要がある場合は、データベースを使用する必要があります。 – user568109

+0

@GerritHoekstraこんにちは私はあなたを助けたいと思いますが、それほど難しくないと思いますが、なぜこの悪い方法でそれをやっているのかを知りたいのですが?それがより良くなったときに私はそれの一部を変更することはできますか?私は一般に、juiでソート可能な2つ以上の列を持つテーブルを意味し、そのソートとして、またはユーザーがボタンをクリックすると新しいオーダーが保存されますか?なぜJSONやXML形式を使って保存しないのですか?魔女のJSON_Config_reader.phpという名前のファイルがそのJSONファイルを読み込んでその配列を作成しますか? – ncm

+0

@GerritHoekstra - あなたは何を知っていますか?あなたは私にとって「素敵な試し」と言うコメントをつけることができます。 – ncm

答えて

1

$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); 
}); 

次にあなたが新しい順

$newConfig = array(); 

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

に書き換え$設定を変更することができますここからは、あなたのユースケースに最も合った方法で$ configを永続化することができます。私もPHPファイルを作成するためにそれを使用しないことを助言する、より良いアプローチは

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

ありがとう、私はこれを使用します。これは、jQueryのUIでこの変更で動作します: 'アップデート:関数(){ \t \t \t \t \t \tするvar A = $( '#のmytableはのTBODY')ソート可能な( 'のtoArray');。 \t \t \t \t \t \t var newOrdering = []; (; iはa.lengthを<; VAR I = 0 iが++)のため \t \t \t \t \t \t \t \t \t \t \t \t newOrdering [I] [I] .substring(0、[I] .indexOfを(= ' - ')); \t \t \t \t \t \t $ .post( "edit.php"、{'neworder':newOrdering}); その後\t \t \t \t \t} ' – user1480019

1

はあなたの問題をカバーしているので、多くのスレッドがあります取得するには、その後

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

を使用することであってもよい

  1. jQuery UI Sortable, then write order into a database
  2. save jquery ui-sortable positions to DB
  3. Optimal way to save order of jQueryUI sortable list with Meteor
  4. http://dev.nirmalya.net/articles/javascript/saving-ordering-of-a-sortable-list-using-jquery-ui

私はあなたがそこにあなたの答えを見つけると確信しています。すべてのデータベース

にそれを保存し、データベースから順序を取得し、ループで正しくそれを表示

$('#mylist').sortable('toArray'); 

経由順序を得ることについてです。チュートリアルを見てください。

0

$(function() { $("ul.sortable").sortable({ connectWith: '.sortable', update: function(event, ui) { /* var position = $('.sortable').sortable('serialize', { key: 'menu', connected: true });*/ $('div').empty().html($('.sortable').serial()); } }); });

+0

そのコード – waheed

+0

(関数($){ $ .fn.serial =関数(){ VARアレイ= []を追加し、 変数$ ELEM = $(この); (メニュー+ '[' + e + '] = 0); $ elem.each(function(i){ var menu = this.id; $(' li '、this).each(function(e){ array.push = '+ this.id); }); }); 戻りarray.join(' &'); }})(jQueryの); – waheed

+2

あなたの答えを編集し、コメントにコードを投稿しないでください。 –

関連する問題