0

In a php generated table, i use drag and drop on the rows, to change the sorting. (first image, second image....)

If i drop the 4th row as first, it will be first, but the row i dropped, it stays as 4th, the rows wont renumber them.

How can i renumber the rows, and put the index in the hidden input named sorrend_hidden?

<table class="table table-hover table-bordered list" id="table_2">
    <thead>
        <tr>
            <td style="width: 1px; text-align: center;"><input type="checkbox" class="checkAllCheckboxes" /></td>
            <td style="text-align: center;">ID</td>
            <td class="left">Kép</td>
            <td class="left">Megnevezés</td>
            <td style="text-align: center;">Sorrend</td>
            <td style="text-align: center;">Státusz</td>
            <td class="right">Műveletek</td>
        </tr>
    </thead>
    <tbody>
        <tr id="sor24">
            <td class="left"><input class="table_row_checkbox" type="checkbox" name="selectedRows[]" value="24" /></td>
            <td style="text-align: center;">24</td>
            <td class="left"><img width="200" src="images/homepage_slideshow/89-FIFA_17_B4jlEln.jpg" class="img-responsive" /></td>
            <td class="left"></td>


            <input type="hidden" value="" name="sorrend_hidden" />


            <td style="text-align: center;">2</td>
            <td style="text-align: center;">Intaktív</td>
            <td class="right">
                <a href="cms/slideshow-szerkesztes.php?id=24"><span class="btn btn-sm button"><span class="glyphicon glyphicon-pencil"></span></span></a>
                <a id="24" href="#" class="deleteLink"><span class="btn btn-sm btn-danger "><span class="glyphicon glyphicon-remove"></span></span></a>
            </td>
        </tr>
        <tr id="sor31">
            <td class="left"><input class="table_row_checkbox" type="checkbox" name="selectedRows[]" value="31" /></td>
            <td style="text-align: center;">31</td>
            <td class="left"><img width="200" src="images/homepage_slideshow/503-WatchDogs2_FarCrayPrimal_1_.jpg" class="img-responsive" /></td>
            <td class="left"></td>


            <input type="hidden" value="" name="sorrend_hidden" />


            <td style="text-align: center;">2</td>
            <td style="text-align: center;">Aktív</td>
            <td class="right">
                <a href="cms/slideshow-szerkesztes.php?id=31"><span class="btn btn-sm button"><span class="glyphicon glyphicon-pencil"></span></span></a>
                <a id="31" href="#" class="deleteLink"><span class="btn btn-sm btn-danger "><span class="glyphicon glyphicon-remove"></span></span></a>
            </td>
        </tr>
        <tr id="sor30">
            <td class="left"><input class="table_row_checkbox" type="checkbox" name="selectedRows[]" value="30" /></td>
            <td style="text-align: center;">30</td>
            <td class="left"><img width="200" src="images/homepage_slideshow/324-DeadRising4_megjelent.jpg" class="img-responsive" /></td>
            <td class="left"></td>


            <input type="hidden" value="" name="sorrend_hidden" />


            <td style="text-align: center;">3</td>
            <td style="text-align: center;">Aktív</td>
            <td class="right">
                <a href="cms/slideshow-szerkesztes.php?id=30"><span class="btn btn-sm button"><span class="glyphicon glyphicon-pencil"></span></span></a>
                <a id="30" href="#" class="deleteLink"><span class="btn btn-sm btn-danger "><span class="glyphicon glyphicon-remove"></span></span></a>
            </td>
        </tr>
        <tr id="sor32">
            <td class="left"><input class="table_row_checkbox" type="checkbox" name="selectedRows[]" value="32" /></td>
            <td style="text-align: center;">32</td>
            <td class="left"><img width="200" src="images/homepage_slideshow/492-PS-VR_.jpg" class="img-responsive" /></td>
            <td class="left"></td>


            <input type="hidden" value="" name="sorrend_hidden" />


            <td style="text-align: center;">4</td>
            <td style="text-align: center;">Aktív</td>
            <td class="right">
                <a href="cms/slideshow-szerkesztes.php?id=32"><span class="btn btn-sm button"><span class="glyphicon glyphicon-pencil"></span></span></a>
                <a id="32" href="#" class="deleteLink"><span class="btn btn-sm btn-danger "><span class="glyphicon glyphicon-remove"></span></span></a>
            </td>
        </tr>
        <tr id="sor23">
            <td class="left"><input class="table_row_checkbox" type="checkbox" name="selectedRows[]" value="23" /></td>
            <td style="text-align: center;">23</td>
            <td class="left"><img width="200" src="images/homepage_slideshow/90-konzolkiraly_sl_1467362325.jpg" class="img-responsive" /></td>
            <td class="left"></td>


            <input type="hidden" value="" name="sorrend_hidden" />


            <td style="text-align: center;">5</td>
            <td style="text-align: center;">Intaktív</td>
            <td class="right">
                <a href="cms/slideshow-szerkesztes.php?id=23"><span class="btn btn-sm button"><span class="glyphicon glyphicon-pencil"></span></span></a>
                <a id="23" href="#" class="deleteLink"><span class="btn btn-sm btn-danger "><span class="glyphicon glyphicon-remove"></span></span></a>
            </td>
        </tr>
    </tbody>
</table>

Jquery:

$("#table_2").tableDnD(
    {
        onDrop: function(table, row) 
        {
            var rows = table.tBodies[0].rows;
            var RowID = row.id.substr(3);
            var RowIndex = $(row).index() + 1;

            // name = sorrend_hidden

            $.ajax({
                data: { RowID:RowID, RowIndex:RowIndex },
                type: 'POST',
                url: 'files/update_slide_index.php',
                success: function(data) 
                {
                    var result = $.trim(data);
                    $('#newsletterResult').html(result);
                    $('#newsletterModal').modal('show');
                },
                complete: function()
                {
                    setTimeout(function() 
                    {
                        location.reload();  
                    }, 3000 );  
                }
            });
        }
    });

Sql update:

<?php
include_once("../../files/connect.php");
if(isset($_POST['RowID']))
{
    $RowID = intval($_POST['RowID']);
    $RowIndex = intval($_POST['RowIndex']);




    $sql = "UPDATE slideshow SET slide_sorrend = '$RowIndex' WHERE slide_id = '$RowID'";
    /*if(mysqli_query($kapcs, $sql))
    {
        mysqli_query($kapcs, "UPDATE slideshow SET slide_sorrend = slide_sorrend + 1  WHERE slide_id != '$RowID'") or die(mysqli_error($kapcs));
    }
    else
    {
        die(mysqli_error($kapcs));
    }*/

    echo 'A kép sorrendje sikeresen megváltozott.';
}
else
{
    echo 'Nincs ID';
}
?>
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
Takács Zoltán
  • 117
  • 3
  • 13
  • Possible duplicate of [Making changes to multiple records based on change of single record with SQL](http://stackoverflow.com/questions/40623132/making-changes-to-multiple-records-based-on-change-of-single-record-with-sql) – e4c5 Jan 06 '17 at 10:32
  • How can i renumber the rows, and update the all rows with the sql? If one row chages, i have to change all the rows sort number. – Takács Zoltán Jan 06 '17 at 11:08
  • That's exactly what's explained in the linked question – e4c5 Jan 06 '17 at 11:27
  • But in that, there arent any javascript. Only the sql renumber the rows? – Takács Zoltán Jan 06 '17 at 11:29
  • The javascript is in the client side. No matter what you do the javscript cannot change your database. That is exactly so for every single webpage on earth – e4c5 Jan 06 '17 at 11:30
  • The javascript tells php to make the changes – e4c5 Jan 06 '17 at 11:30
  • And what answer can i use in the link you give me? – Takács Zoltán Jan 06 '17 at 11:31

1 Answers1

0

I had a similar issue months ago. I ended up using the sortable feature included in jQuery UI.