简体   繁体   中英

php or jquery code to update drag and drop sortable according to category

I have the following table:

news(id,cat_id,headline,sub_top_priority)

I create drag and drop sortable list.

It change the sub_top_priority column order in database.

The code works fine and also updating the order in database.

But this will affect and change the order of all row in database.

But i need this to change order of sub_top_priority column according to category.

If category is different then it does not change order.

plz suggest me...how to apply condition in update query.

row display code

<div style="border:1px solid #000; float:left; width:400px; padding:5px 4px 5px 4px; height:225px">
                <div id="contentLeft1">
                <ul>            
                <?php                  
                foreach($sub_top_select as $sub_top)
                {                       
                ?>
                <li id="recordsArray1_<?php echo $sub_top['id']; ?>"><?php echo $sub_top['headline']; ?></li>
                <?php } ?>
                </ul>
                </div>
            </div>


<!--SCRIPT TO UPDATE SUB-TOP SECTION UP-DOWN OR DRAG AND DROP OPERATION IN DATABASE.-->
<script type="text/javascript">
$(document).ready(function(){                          
    $(function() {
        $("#contentLeft1 ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
            var order = $(this).sortable("serialize") + '&change=updatesubtop'; 
            $.post("add_status_news.php", order, function(theResponse){
                $("#contentRight").html(theResponse);
            });                                                              
        }                                 
        });
    });

}); 
</script>

page name add_status_news.php

php code to update order in database.

<?php
if(isset($_POST['change']))
    {   
        $change = mysql_real_escape_string($_POST['change']); 
        $updateRecordsArray1 = $_POST['recordsArray1'];
        if ($change == "updatesubtop")
        {           
            $listingCounter1 = 1;
            foreach ($updateRecordsArray1 as $recordIDValue1)
            {       
                $query1 = "UPDATE news SET sub_top_priority = " . $listingCounter1 . " WHERE id = " . $recordIDValue1;              
                mysql_query($query1) or die($query1."<br/><br/>".mysql_error());
                $listingCounter1 = $listingCounter1 + 1;
            }           

        }
    }
?>

This isn't a complete answer, but I want to take advantage of the answer window's formatting options:

Instead of UPDATE, consider the following:

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id CHAR(1) NOT NULL PRIMARY KEY, sort_order INT NOT NULL);

INSERT INTO my_table (id,sort_order) VALUES ('A',1),('B',2),('C',3),('D',4),('E',5);

SELECT * FROM my_table;
+----+------------+
| id | sort_order |
+----+------------+
|  A |          1 |
|  B |          2 |
|  C |          3 |
|  D |          4 |
|  E |          5 |
+----+------------+

INSERT INTO my_table (id,sort_order) VALUES ('A',5),('B',4),('C',3),('D',2),('E',1) ON DUPLICATE KEY UPDATE sort_order = VALUES(sort_order);

SELECT * FROM my_table;
+----+------------+
| id | sort_order |
+----+------------+
|  A |          5 |
|  B |          4 |
|  C |          3 |
|  D |          2 |
|  E |          1 |
+----+------------+

This has the advantage that the query can be built inside the loop and then executed outside the loop. On large lists, I suspect that this will be faster than endless round trips to the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM