Phpfor loop-php Tutorial

Source: Internet
Author: User
I am a beginner in PHP about the phpfor loop concept. I recently developed a project to seek the binary tree system welfare award. After writing, the execution efficiency is too low. Please give me some advice. Image description
1. if the performance reaches, the supervisor will be responsible. Under the management system, two supervisors are managers. Under the management system, two managers are directors. 0 members, 1 supervisor, 2 managers, 3 directors.
The code I wrote is as follows:
// Welfare award starts
Unset ($ map); // destroy the map variable value
$ Map ['_ string'] = "datediff (curday,'". $ beginday. "') <= 0 ";
// Query the performance of all members
$ Hyyjlist = $ hyyj-> where ($ map)-> field ('userid, sum (leftyj1) + sum (leftyj2) as sumleft ')-> group ('userid ') -> select ();
$ GlrCountArry = array (); // key: Supervisor name + flw.l value: number of supervisors saved array
Foreach ($ hyyjlist as $ hkey => $ hvalue) {// cyclically update your supervisor benefits
If ($ hvalue ['sumleft']> = 10000 ){
$ Us = $ userListArry [$ hvalue ['userid']; // 3 Get a member by ID
If ($ us ['flstate'] = 0) {// The status is 0 Member
If ($ salary1-> where (array ('periods '=> $ periods, 'userid' => $ us ['id']) -> setField (array ('flj' => 'Z supervisor content Z') = false ){
$ Salary-> rollback ();
$ This-> error ('welfare Award update failed ');
}
For (! Empty ($ us ['gluser']), $ I = 0; $ I If (! Array_key_exists ($ us ['gluser']. "flrzg", $ glrCountArry )){
$ GlrCountArry [$ us ['gluser']. "flrzg"] = 1;
} Else {
$ GlrCountArry [$ us ['gluser']. "flrzg"] + = 1;
}
$ Us = $ userNameListArry [$ us ['gluser'];
If (empty ($ us )){
$ I = count ($ userlist );
}
}
}
}
}
$ Find ['state'] = array ('EQ ', 1); // status = 1 activated
$ Find ['_ string'] = "from_unixtime (confirmtime,' % Y-% m-% d') <= '". $ beginday. "'"; // activation date: current time
$ List2 = $ users-> where ($ find)-> select (); // query the member list
$ GlrCountArry2 = array (); // key: Manager name + flw.l value: number of people saves the number of managers array
Foreach ($ list2 as $ key => $ value) {// Get manager benefits
$ Us = $ value;
If ($ us ['flstate'] = 1 ){
For (! Empty ($ us ['gluser']), $ I = 0; $ I If ($ glrCountArry [$ us ['username']. "flrzg"]> 0) {// The member is at the supervisor level, and the number of supervisors is greater than 0. update the manager benefits of the member.
If ($ salary1-> where (array ('periods '=> $ periods, 'userid' => $ us ['id']) -> setField (array ('flj' => 'Z manager content Z') = false ){
$ Salary-> rollback ();
$ This-> error ('welfare Award update failed 2 ');
}
}
If (! Array_key_exists ($ us ['gluser']. "flw.l", $ glrCountArry2) {// enables upper-level managers of members to + 1
$ GlrCountArry2 [$ us ['gluser']. "flw.l"] = 1;
} Else {
$ GlrCountArry2 [$ us ['gluser']. "flrjl"] + = 1;
}
$ Us = $ userNameListArry [$ us ['gluser'];
If (empty ($ us )){
$ I = count ($ userlist );
}
}
} Else if ($ value ['flstate'] = 0) {// The member level is not the supervisor. the number of supervisors is greater than 1. update the manager benefits of the member.
For (! Empty ($ us ['gluser']), $ I = 0; $ I If ($ glrCountArry [$ us ['username']. "flrzg"]> 1) {// The members are supervisors. the number of supervisors is greater than 0. update the manager benefits of members.
If ($ salary1-> where (array ('periods '=> $ periods, 'userid' => $ us ['id']) -> setField (array ('flj' => 'Z manager content Z') = false ){
$ Salary-> rollback ();
$ This-> error ('welfare Award update failed 2 ');
}
}
If (! Array_key_exists ($ us ['gluser']. "flw.l", $ glrCountArry2) {// enables upper-level managers of members to + 1
$ GlrCountArry2 [$ us ['gluser']. "flw.l"] = 1;
} Else {
$ GlrCountArry2 [$ us ['gluser']. "flrjl"] + = 1;
}
$ Us = $ userNameListArry [$ us ['gluser'];
If (empty ($ us )){
$ I = count ($ userlist );
}
}

}
}
Foreach ($ list2 as $ key => $ value) {// get the director benefits
$ Us = $ value;
If ($ us ['flstate'] = 2) {// The status is 2 managers.
For (! Empty ($ us ['gluser']), $ I = 0; $ I If ($ glrCountArry2 [$ us ['username']. "flw.l"]> 0) {// The members are managers. the number of managers in the array is greater than 0. update the member's director benefits.
If ($ salary1-> where (array ('periods '=> $ periods, 'userid' => $ us ['id']) -> setField (array ('flj' => 'Z director award content Z') = false ){
$ Salary-> rollback ();
$ This-> error ('welfare Award update failed 3 ');
}
}
$ Us = $ userNameListArry [$ us ['gluser'];
If (empty ($ us )){
$ I = count ($ userlist );
}
}
} Else if ($ value ['flstate'] <2) {// The status is not a manager, as long as the number of managers in the array reaches two new member's director benefits
For (! Empty ($ us ['gluser']), $ I = 0; $ I If ($ glrCountArry2 [$ us ['username']. "flw.l"]> 1) {// The members are managers. the number of managers in the array is greater than 1 to update the member's director benefits.
If ($ salary1-> where (array ('periods '=> $ periods, 'userid' => $ us ['id']) -> setField (array ('flj' => 'Z director award content Z') = false ){
$ Salary-> rollback ();
$ This-> error ('welfare Award update failed 3 ');
}
}
$ Us = $ userNameListArry [$ us ['gluser'];
If (empty ($ us )){
$ I = count ($ userlist );
}
}
}
}
// Welfare award ended


Reply to discussion (solution)

Describe your business needs in text!
In this way, you may know where the problem with your code is?

A member recommends a new person to his name. a member can recommend two people. for example, a person is recommended for 1000 yuan. When the amount of the person he recommends reaches, he becomes the supervisor and will be awarded the supervisor award. When there are two supervisors under his name, he is the manager and wins the manager award.

What is the table structure? The update command should be used to complete the task.

The idea I wrote is to loop through all members. when his performance reaches, this member won the supervisor award. Then, the upper-layer managers will receive management Awards. Then, the member will be recycled once. when there are two supervisors under his name, he will receive the manager Award, and then his upper layers will receive the manager award.
The problem is that this dual loop operation is too inefficient. I ran 8000 pieces of data for 3 minutes. How can we optimize the ideas or methods?

Again. You need to see how your data is stored in the table.
This may be because your thinking is wrong.

For example, A has two members. it costs 1000 yuan to register members, and their upper-layer members receive registration results.
B C. They also have members below
B1 B2 C1 C2 member classification: Member-> supervisor-> manager-> director.
If B1, B2, C1, and C2 are registered as new members, B's performance is 2000, C's performance is 2000, and A's performance is 6000, which is calculated by the number of people.
The function to be done now is the membership welfare award. When the member's performance is 10000, the member will receive the supervisor award. When a member has two supervisors, the member can
Received the manager award. When there are two managers under the member name, the member will receive the director award.
For example, if B1's performance reaches, B1 receives the supervisor award and B receives the supervisor award. Under A, two supervisors B and B1 won the Manager Award. And so on...
I have a problem with the code I wrote. I hope to get a new idea. thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.