Hello and thank You for Your help in advance 🙂
I got some experience in php because this is my hobby and I have used the basics of php before.
My current problem is:
I've a MySQL database with two columns (id, number)
I've got a complex rule how I would like to count the occurrance of the same numbers. I've got 2 type of numbers in the database (nr 1 and nr 2)
I would like to use "While" to sweep through the database.
The rule is the following:
I need to count if a line is followed with the same number
For example:
row 1: 1
row 2: 2
row 3: 1
row 4: 1
row 5: 2
row 6: 1
row 7: 1
In this case I have twice the nr 1 followed with an other nr 1.
So I need to sweep through the database and count these occurrances.
This was the easy part of the rule...
I insert these numbers into the database one by one and after the insertion the script should check the last 150 row if it has reached 8 occurrances or not.
This was the second easy part of the rule...
I have figured out how to do that and it works fine, but I've got an other rule which I have to fullfill.
The rule says: We need to count how many times a line is followed with the same line at least with one same line but if the script finds that after one occurrance like this the number 1 appears just once and it is not followed with an other nr 1 than the counter needs to be set to zero and start the counting from the begining. So:
For example:
row 1: 1
row 2: 2
row 3: 1
row 4: 1
row 5: 2
row 6: 1
row 7: 2
row 8: 1
row 9: 1
row 10: 2
You can see that the nr 1 appeared twice in these rows together but because there was a single nr 1 after the first occurrance and the counter was set to zero after that and the end the counter needs to show 1.
Ok, and one other thing, it just needs to work like this for the first 8 occurrances, after that it does not have to be set to zero.
Why I need this whole thing is because I would like to be aided with this script to decide what to do. I will need to insert hundreds of thousands of numbers like this one by one and after each insertion the script should check if it has reached 8 occurrances without any single number between them and if yes after that I will need to do something.
Here is what I've got now:
while ($row = DB::fetch_row()) {
if ($row['number'] == 1){
if ($previous_was_1 == 1){
if ($previous_was_double1 == 1){
$previous_was_double1 = 1;
}else{
$double_1 = $double_1 + 1; //THIS IS THE COUNTER
$previous_was_double1 = 1;
}
}else{
$previous_was_double1 = 0;
}
$previous_was_1 = 1;
}
//I AM AFFRAID THAT THE PROBLEM STARTS FROM SOMEWHERE HERE 🙁
$current_number = $row['number'];
if ($previous_number != 0){
if ($previous_previous_number != 0){
if (($double_1 < 8) && ($double_1 >= 1)){
if ($current_number == 2){
if ($previous_number == 1){
if ($previous_previous_number == 2){
$double_1 = 0;
}
}
}
}
$previous_previous_number = $previous_number ;
$previous_number = $current_number;
}elseif($previous_previous_number == 0){
$previous_previous_number = $previous_number;
$previous_number = $current_number;
}
}elseif ($previous_number == 0){
$previous_number = $current_number;
}
}
So could anyone help me with this problem?
Thank You for You time anyway 🙂