Search code examples
phpmysqlround-robin

Round Robin System php and mysql


I'm creating landing pages. All the leads that come in go to my database. I want to retrieve the leads from my database and send them out to 2 emails

for example like this

lead 1 -> my email
lead 2 -> my employee
lead 3 -> my email
lead 4 -> my employee
lead 5 -> my email
lead 6 -> my employee

any idea how i would start? How do you recommend i setup my database?


Solution

  • You simply need to use a randomizer (e.g. mt_rand) to generate a number between 0 (you) and 1 (your employer). If you have more employees, then just increase the maximum number.

    In your database, add a column named "assignee" or anything else, and when you store the email add also the random number. For example:

    // Number of employees, excluding you
    $employees = 1;
    
    $stmt = $dbh->prepare("INSERT INTO emails (email, assignee) VALUES (:email, :assignee)");
    $stmt->bindParam(':email', '[email protected]');
    $stmt->bindParam(':assignee', mt_rand(0, $employees));
    

    The table will then look like:

    email                | assignee
    --------------------------------
    [email protected]  |        0
    --------------------------------
    [email protected] |        1
    --------------------------------
    [email protected] |        1
    

    When you select the records, those with assignee = 1 are assigned to your employee, and those with assignee = 0 are yours.

    Note: this method is the simplest one. It doesn't return an exact sequence of 0-1, but if you have N emails, on the average 50% will be assigned to you and 50% to your employee (for the "Law Of Large Numbers"), so you'll have achieved your goal!