CodeIgniter Forums

Full Version: Update Row Value If Another Row is Deleted
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.

The app I am building has a ranking system. Nothing fancy, just 1st, 2nd, 3rd. Is there any possible way using CodeIgniter, I could update values in the other rows based on the row that is deleted?

So, If for example,  I delete 1st place from the table, 2nd would become 1st and 3rd would become 2nd.

I have started trying to build a formula in PHP using the rows Rank Value and the number of rows in the table, but it is proving quite challenging and I was just wondering if there is a simpler way using a CodeIgniter feature.  I did try a Google search but I could not see any results that mention anything.

Thank you for any help. Smile
After you delete a row, you can update the other rows where the rank is higher. You just need to know what rang was deleted. Something like this:

Code:
UPDATE SOMETABLE SET RANK = RANK + 1 WHERE RANK > ?

You can do this in your code, or with a database trigger, or if you use CI4, with a model event (before delete or after delete): https://codeigniter4.github.io/userguide...parameters
(02-01-2020, 03:00 PM)includebeer Wrote: [ -> ]After you delete a row, you can update the other rows where the rank is higher. You just need to know what rang was deleted. Something like this:

Code:
UPDATE SOMETABLE SET RANK = RANK + 1 WHERE RANK > ?

You can do this in your code, or with a database trigger, or if you use CI4, with a model event (before delete or after delete): https://codeigniter4.github.io/userguide...parameters


This works perfectly!, Thank you so much for the help!