GithubHelp home page GithubHelp logo

fernandobasso / crud-custom-position Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 124 KB

A CRUD that allows the user to manually set the position in which posts are to be listed.

PHP 95.27% CSS 4.73%

crud-custom-position's Introduction

Crud With Custom Position

This is a solution to a situation I had at work. The basic idea is that the user wants to define the position an entry on the database should appear on the web page by providing a number.

Add New Entry

In this case, if the new position is set to zero, we assume the user wants to insert at the end. We then get the number or rows and set the position to number of rows + 1.

Lower to Upper Position

If user updates position 5 to 1 (it goes from a lower position to a higher position), we first update the one that current has position 1 to 2, and also increment the other positions stopping at the item that has the position 1 less than the one we are changing. Finally we update the item that the user is explicitly changing by its id.

// Updates from $newpos up to before $oldpos.
$sql = "UPDATE post SET
  pos = pos + 1
  WHERE post.pos >= $newpos
  AND post.pos < $oldpos;";


// Change the pos the explicitly repositioned post itself.
  $sql = "UPDATE post SET
  pos = {$newpos}
  WHERE post.id = {$pk};";

Upper to Lower Position

Update from current item pos + 1 up to the one in the position that the changed one will occupy. If user changes item in position 4 to position 1, decrement from position 2 to the one that current is in position 4. Then, update the item that the user is explicitly changing by its id.

// Updates from $pos + 1 to the one that will be changed explicitly.
$sql = "UPDATE post SET
  pos = pos - 1
  WHERE post.pos = $newpos + 1
  AND post.pos <= $oldpos;

// Change the position of the explicitly repositioned post itself.
$sql = "UPDATE post SET
  pos = $newpos
  WHERE id = {$pk};";

Remove Entry

Simply decrement the position of all the entries whose position are greater than the position of the element being removed.

crud-custom-position's People

Contributors

fernandobasso avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.