CodeIgniter Forums
Update Row Value If Another Row is Deleted - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Update Row Value If Another Row is Deleted (/showthread.php?tid=75385)



Update Row Value If Another Row is Deleted - GregS - 01-31-2020

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


RE: Update Row Value If Another Row is Deleted - includebeer - 02-01-2020

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/models/model.html#event-parameters


RE: Update Row Value If Another Row is Deleted - GregS - 02-02-2020

(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/models/model.html#event-parameters


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