php - Which function architecture is the best for maintainability in this simple case? -


I'm adding "promo code" functionality to an online shopping cart written by another person, this is a very Mickey Mouse architecture question Is there, but I would like to hear some opinions. All my three ideas will work, but do you think that it is best for liability?

So, here is the basic table structure for the promo code:

  promocodes` (`promocode_id` INT not the NULL AUTO_INCREMENT primary key,` promocode` VARCHAR (255) NULL, `description` VARCHAR (255) zero,` discount_percentage` INT zero, `discount_dollars` decimal (6, 2) zero,` expiration_date` zero for date, `use_remaining` INT zero,` is_active` NULL default bool '1 What's clever (without getting overcomplicated):  

Check the existence with SQL, everything else in code / P>

  // Library Function Promo CodeExists ($ Promo Codestring) {// Make SQL call return $ promo codeHash; // is false, no record} function isPromoCodeUsable ($ promoCodeDetailsHash) {// Expiration date and the number of uses left and active / inactive} function usePromoCode ($ promoCodeId) {// // decreases use the left} // USAGE $ promoCodeDetailsHash = promoCodeExists ($ promoCode); {If_array ($ promoCodeDetailsHash) and isPromoCodeUsable ($ promoCodeDetailsHash)} {usePromoCode ($ promoCodeDetailsHash ['id'])} else {// Invalid Promo Code}  

Or, Validate the library function. Promo Code ($ promoCodeDetailsHash) {// Check end date and left and active usage. Number of / passive} function isPromoCodeUsable ($ promoCodeString) {// create SQL call return valid protocol ($ promoCodeDetailsHash); // or wrong if there is no record) // USAGE $ promoCodeDetailsHash = promoCodeExists ($ promoCode); {If_array (isPromoCodeUsable ($ promoCodeDetailsHash))} {usePromoCode ($ promoCodeDetailsHash ['id'])} else {// Invalid Promo Code}

Everything in SQL Check invalid as nonexistance:

  // library function getDetailsForUsablePromoCode ($ promotional code) {// use SQL where only the current valid code for the validity go back to existence Section // or if there is no false result) // USAGE $ promoCodeDetailsHash = getDetailsForUsablePromoCode ($ promoCode) if (is_array ($ promoCodeDetailsHash)) {usePromoCode ($ promoCodeDetailsHash ['id'])} Other {// error status}  

feel free to point out any other approach or walkthrough here

In my application, I make it 2 table as your first table, but only keep use_limit as integer in another table, I use , Will use one line per line.

in the promocode_usage table, I will have promocode_id in the form of a foreign key, and other required columns such as usage datat, user id, etc. To check whether the promo is still available or not, I just want to see count line promocode_usage in the table Promocode_id if the result Use_limit , the promo can be used.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -