View Single Post
  #5  
Old 01-07-2016, 08:11 AM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 857
Gender: Unknown/Other
Credits: 125,738
Kyttias is on a distinguished road
Default

The only way I can think of is to have a separate table in the database that will hold information about whether a user has ever activated a code before. It'd have users as rows (obviously), but must have all the promo codes as columns. Every time you make a new limited promocode you'd have to add another column to that table. The checks would be as simple as 'has the user activated this code more than 0 times?' - and any limit could work, if you wanted to limit some things to other amounts.

The users would be automatically be added to the table only after the first time they adopt a limited pet, so you needn't add any manually. Here's vaguely where my thoughts lead me.

Create a table in the database (can run this SQL) -
Code:
CREATE TABLE IF NOT EXISTS `adopts_limitedpromos` (
  `pcid` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(30) NOT NULL,
  `examplecode1` int(11) NOT NULL DEFAULT '0',
  `examplecode2` int(11) NOT NULL DEFAULT '0',
  `examplecode3` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`pcid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
And you can use it like this -
PHP Code:
    
/* You'll want this code to run whenever a limited promocode is trying to be claimed: */
    // You will need to get these variables yourself, perhaps with a function?
    
$promocode;
    
$username;
    
    
// Example here using MySQLi instead of what's native to Mysidia.
    
include("../../inc/config.php");  
    
$db = new mysqli(DBHOSTDBUSERDBPASSDBNAME);

    
// Check the number of times a user has used a promocode.
    
$sql mysqli_query("SELECT {$promocode} FROM `adopts_limitedpromos` WHERE `username` = '{$username}'"); 
    
$result mysqli_fetch_object($sql);

    
// If the number of times the user has used this code is less than 1, procede!
    
if ($result->$promocode 1){    
        
// Updates the database for this user to increase the number of times this promocode has been claimed by one.
        
mysqli_query($db,"UPDATE `adopts_limitedpromos` SET `{$promocode}` = {$promocode} + 1 WHERE `username` = '{$username}'");
        
        if (
mysqli_affected_rows($db) < 1){
        
// If the update function did not fire, the user doesn't have a row in the table yet. This'll create one.
            
mysqli_query($db,"INSERT INTO `adopts_limitedpromos` SET `username` = '{$username}', `{$promocode}` = '1'");
        }
        
        
mysqli_close($db);
    } 
Not sure how well you follow me. This is the best way I can think of? Actually implementing it in parts of the site might require more help.
__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it.
Reply With Quote