简体   繁体   中英

PHP: iterate through array and assign a customer to a user, in turn

Purpose

Every time an operator manually adds a Customer to the program, he/she assigns an area to the customer as well.

There are many users and every one has one or more areas assigned.

Once the operator enters a customer and assigns an area to it, a user should be assigned to that customer, but not always the same user, it has to be a different one every time (based on a area_customer table) and this has to be in turn (1,2,3,1,2,3,1,2...).

So if 3 users are assigned to an area, the created customer is first assigned to the first user, later on a new customer is entered and is assigned to the second user, a third customer is entered and is assigned to the third user. At this point all starts again. If a fourth customer is created, the first user is assigned to this, an so forth.

Details

I have 4 MySQL tables:

  • Users
  • Areas
  • Area_Users
  • Customers

Users table has a unique id user_id and other fields for each user.

Areas table has a unique id area_id and a name field.

Area_Users table has the area_id and user_id only, this is where the assignment of areas has been made, so every user has assigned to himself one or more areas.

Customer table has a unique id customer_id , has an area_id (that relates to the Areas table) and has a user_assigned_id (related to Users table)

What I need to accomplish:

Every time a customer is added to the program, the responsible manually inserts all of the data on that customer, INCLUDING THE AREA the customer is assigned to.

At this point a row is inserted in the MySQL table Customer , but the user_assigned_id should be added programmatically (and not manually by the person).

The choice of which user_id to place in the user_assigned_id needs to be calculated from the Areas table. It should be a different person every time in turn.

This is the Areas table:

|  area_id  |    name   |
-------------------------    
|     1     |    Z1     |
|     2     |    Z2     |

So let's say this is the Area_Users table:

|  area_id  |  user_id  |
-------------------------    
|     1     |     4     |
|     1     |     6     |
|     2     |     1     |
|     2     |     2     |
|     2     |     3     |
|     2     |     5     |
|     2     |     6     |

So, the area #1 is assigned to user 4 and 6, while area #2 is assigned to 1,2,3,5 and 6. (Note: User 6 has 2 area assigned)

At this point the person creates a record for area #2, this has to be assigned to user 1, the next record that will be created goes to user #2, than #3 then #5 and then #6. Than again from #1.

Same for area #1, the first goes to user #4, then #6 then #4, then #6, and so forth...

My idea is to first find the Area data:

//Codeigniter 
$areas_data = $this->db->get_where('areas', ['area_id' => 1]);

Then find how many users are in those area:

$tot_users_area = $areas_data->num_rows();

Then I can iterate through them, but how do I know what was the last assignment? And, in the case of Area #1, after user #4 comes #6 (or could be #124) so how do I go onto the next one?

If I understand correctly, and the customer_id field in the Customers table is auto-incremented, you'd need to check the Customers table for the "last assignment", as you put it.

After you get all the $areas_data:

$areas_data = $this->db->get_where('areas', ['area_id' => 1]);

Loop through each (user_id, area) pair in the Customers table and see what exists. When you find a pair that does not exist, add it:

foreach($areasdata as $key => $areaDatum) {
    $result = $this->db->get_where('customers', array('area_id' => $areaDatum['area_id'], 'user_assigned_id' => $areaDatum['user_id']));
    if(!result) {
        $data = array('area_id' => $areaDatum['area_id'], 'user_assigned_id' = > $areaDatum['user_id']);
        $this->db->insert('customers', $data);
    }
}

It's just a simple demonstration with arrays without using codeigniter and database..

<?php
$customers=[1,2,3,4,5,6,7];
$users=[1,2,3,4];
$usercustomers = [];
$userslen=count($users);


foreach($customers as $customer)
{
    if($customer > $userslen){

        $usercustomers = array('user'     => $customer%$userslen,
                               'customer' => $customer);
        var_dump($usercustomers);
        echo "<br>";

    }
    foreach($users as $user)
    {

        if($customer%$user==0 && $customer==$user){

            $usercustomers = array('user'     => $user,
                                   'customer' => $customer);

            var_dump($usercustomers);
            echo "<br>";
         }

    }

}



?>

At this point you may consider using an ORM package(Which provides reusuble solutions for situations like that)..Because there is a many to one relationship between users and customers..Using an ORM there is an easy solution.. http://www.krueckeberg.org/notes/relationships.html .. Also search the keywords "on cascade delete" and "on cascade insert" ..It will be more easy using an ORM and cascading.."There is no need to reinvent the wheel"..

There is a many to many relationship between areas and users..Changing your database design may help you,and strongly recomended..In your current design what if you need to get all areas?There is not a primary key in the table areas..Also what if you need to get track assignments using date?In this solution you can add a date field into AssignedInto table and keep track assignemts etc.AssignedInto table provides the relation between the Users and Areas..

https://en.wikipedia.org/wiki/Many-to-many_(data_model)

http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561

在此处输入图片说明

An ORM package called doctrine..

http://www.doctrine-project.org/

What you are actually looking for is ROUND ROBIN METHOD . You have to circle the users while assigning a customer.

WHAT YOU CAN DO

Whenever you are assinging the customer, you have to make another insert in a temporary table .

This temporary table tracks which was the last user (userid) that was assigned and the area of that customer, and when you will be assinging the customer once again you will first consult this temporary table to check which user was last assigned and just increment that userid and assign the customer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM