0

I am using jQuery sortable to manipulate image order and write to a DB. That functionality works well.

PHP

 echo "<div class='revisionNum'>";
            echo "<ul id='sortable_" . $count ."'>";

            while($row = mysql_fetch_array($result)) {
                $sortImageName = $row['OrgImageName']; 
                $sortPath = "../data/gallery/" . $galleryID . "/images/album/" . $sortImageName;
                echo "<li class='sortPhotos' id='item_{$row['id']}' >";
                echo '<img class="sortImage" src="'. $sortPath .'"/>';
                echo "<p>" . $sortImageName . "</p>";
                echo "</li>";
            }
            echo "</ul>";
            echo "</div>";

jQuery

//make sortable
    $(".revisionNum").each(
        function(e) {
        num = e + 1;
        $("#sortable_" + num).sortable(
            {stop:function(i) {
                serial = $("#sortable_"  + num).sortable("serialize");
                $.ajax({
                    type: "GET",
                    url: "../albumUploader/queries/sort.php",
                    data: serial
                });
            },
            opacity:1.0,
            //cursor: move
        });    
    });

MYSQL

foreach($_GET['item'] as $key=>$value) {

    mysql_query("   UPDATE galleryimage
                    SET sort = '{$key}'
                    WHERE id = '{$value}'  
                ");

} The issue is when I have multiple <div class=''revisionNum> i am only grabbing the serial = $("#sortable_" + num) of the last UL if the [.revisionNum], not the actual UL that I am sorting. Thanks for the help on this. Let me know if further clarification is needed.

1 Answer 1

2

I am not sure I fully understand your question, but I think you are looking for the following:

The variable num will change every loop you make in the each-loop. But at the end it will have the value of the last loop. Because num seems to be a global variable you can't call it in the stop function. Then it will just use the last value it had. The value of the last loop. (Explains your problem)

To solve this I recommend to change your code to:

$(".revisionNum").each(
    function(e) {
    $(this).children("ul").sortable(
        {stop:function(i) {
            num = $(this).children("ul").attr("id").replace("sortable_", "");
            serial = $(this).children("ul").sortable("serialize");
            ...

$(this) refers to the $(".revisionNum") you are looping through and it will be remembered, also in the stop function.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the reply. I need the num value as it is the unique identifier that sortable uses. Without that I lose my DB write. I'll add the sql so you see what I am doing with the sort. Should have done that sooner
I've added a new line: num = $(this).children("ul").attr("id").replace("sortable_", ""); where num is retrieved by the id of the ul. Does that solve it?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.