Back to Top

To Swap Database Row in PHP

To Swap Database Row in PHP

Today I am going to show one more demo which is for to swap database table row like in Joomla for sorting or ordering a table.Here I am going to show you how to swap the id’s of rows of the particular table.

Basically, we are doing swapping to SWAP values of two variables with using a third variable so with the use this basic concept, I have created one demo which swap database rows with PHP coding.In order to switch the position of rows, you will need to set the Order(ASC, DESC) value of each row and then you can update rows with swap code.a

Let’s have a look into coding done by me so you get the idea how swapping of rows is possible.

Also READ: To use Flickr API with PHP

First of all create one table swap rows,

After creating table create one file swaprow.php and write a query to fetch records from the particular table you want to swap its row using MySQL functions.

After select records from the database, It is a time to use records and for that you can create HTML as shown below:

Next is to create one file that is validSwapID.php and the main important thing is to create one function which swap the row.Here I have written one function row_swap which swap database:

Recommended READ:
To Unzip file and move to destination in PHP
To grab content from nested tags in PHP

Here above code, You will get previd and nextid. Using previd and nextid find out the previous and next row from swap rows Table.And after fetching the id, call the row_swap function which will update the row into the table and your data will be updated.

That’s it. Now you are ready to swap rows from the database.Have a look at the working Demo here.

View Demo

I hope you have understood the concept and it has been helpful to you.

Comments (3)

  1. What will happen with this code when your table has 1000 rows?

    Does that still works?

    1. You can change 999 value to other max value. Here I have used fix value and need to take third variable for swapping because i have swap primary key.

      any suggestion??

      1. .I’m very sure I didn’t msslpeil anything, it was just copied and pasted and only the number and category name changed. Any idea what can be the problem?Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Run-Time Validation using jQuery

Posted on 11 years ago

Bhumi