0

I am doing something wrong and it's driving me nuts because I can't figure it out. Using jQuery ui sortable to sort the divs on my page. The sorting part works kinda but it does not update the database.

The only thing I can think of is the clear within page, but if I put this on the div it acts not as fluent.

PHP

<div id="page">
    <div id="listItem_'.$id.'" class="a bunch of random classes">
        <div class="handle"></div>
    </div>
    <div id="listItem_'.$id.'" class="this one has some other classes">
        <div class="handle"></div>
    </div>
    <div class="clear"></div>
    <div id="listItem_'.$id.'" class="a bunch of some other">
        <div class="handle"></div>
    </div>
</div>

Javascript

$(document).ready(function(){
    $("#page").sortable({ 
      handle : '.handle', 
      update : function () { 
        var order = $('#page').sortable('serialize');
            $(document).load("sort.php?"+order); 
        }
    });
});

sort.php

<?php
    session_start();
    require('connect.php');

    if($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_SESSION['USERNAME'])){
        $i=1;
        foreach ($_GET['listItem'] as $position => $item){
            $sql = "UPDATE table SET position = ".$i." WHERE id = ".$item;
            $res = mysql_query($sql);
            $i++;
        } 
    }
?>

2 Answers 2

1

Start debugging by placing var_dump('got here'); (or one of the variables) in your PHP script. Use the console in Firebug (which is an add-on) in Firefox to view the output. Step through until you find the spot where it's failing.

3
  • Thanks alot! it did not fix, but did not know that yet!
    – rofavadeka
    Feb 14, 2013 at 14:37
  • Thanks, it helped me find the solution to my problem. Code above works perfectly, it went wrong in my PHP where only type A blocks got an ID instead of all blocks.
    – rofavadeka
    Feb 14, 2013 at 14:57
  • No problem. Glad to help. You just added a big item to your toolset... integral when debugging AJAX and Javascript in general. var_dump() in PHP - and console.log() in Javascript - are your friends.
    – Gor
    Feb 14, 2013 at 18:31
0

should $i be replaced with $position:

$sql = "UPDATE table SET position = ".$i." WHERE id = ".$item;
$sql = "UPDATE table SET position = ".$position." WHERE id = ".$item;

in any case ... if it doesnt update the database presumably you get an error?

and, i assume your table isnt actually called 'table' otherwise you should probably backtick it..

4
  • table is called blocks, trying to make a windows 8 like thing where i can create blocks. creating, updating and removing these blocks works but i want to be able to drag them to new positions. the "a bunch of random classes" are the type, width and color of the blocks. Annyway, thanks for your effort. Unfortunately it does not work.
    – rofavadeka
    Feb 14, 2013 at 14:30
  • but what doesnt work exactly. do the rows get updated but with the wrong data? do they not get updated? if they dont get updated, that would indicate you have an error. do a mysql_error or find out the value of $res.
    – djjjuk
    Feb 14, 2013 at 14:32
  • 1
    as a side note, you really shouldnt be using mysql_* functions any more. read the big red notice here: php.net/manual/en/function.mysql-query.php
    – djjjuk
    Feb 14, 2013 at 14:34
  • Thanks for that, did not know it was deprecated.
    – rofavadeka
    Feb 14, 2013 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.