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.

Share:
Share on Facebook0Share on LinkedIn0Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit0

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 *

Most Popular Posts

How to Use HTML5 Data Attributes

Posted on 2 years ago

Bhumi

How to Use PHP Sessions to Store Data

Posted on 1 year ago

Bhumi

How you can write Custom Queries in Modx

Posted on 5 years ago

Bhumi

Facade implementation in Laravel

Updated 1 month ago

Bhumi

MySQL coalesce() function

Updated 18 years ago

Bhumi

How to use JavaScript Promise API?

Updated 18 years ago

Bhumi

What are Lambdas and Closures in PHP?

Updated 18 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 3 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 4 years ago

Bhumi