Foreach loop reads array in reverse

Hello,

There is a array $array = array("9","10","14","17");

I’m using foreach in two places on the page

foreach 1:
But this loop reads the array from the end, i.e. it starts at input value 17
Searches for entry 14 if there is no entry 17
Why does this foreach read the array in reverse?
What did I do wrong here?
Note: there is another database query code between two foreach loops but not using any loops

foreach($array AS $input){
// Tap off
// Here is the database query code
$tap_off->execute([$input]);
$result = $tap_off->rowCount();
// Stop loop if product
if (isset($result) && $result == 1) {break;}
// Search for next input value if product does not exist
if (isset($result) && $result == 0) {continue 1;}
}

foreach 2:
This loop searches the array from the left i.e. 9 input options
This loop is working correctly

foreach($array AS $input){
// Splitter
// Here is the database query code
$splitter->execute([$input]);
$result = $splitter->rowCount();
// Stop loop if product
if (isset($result) && $result == 1) {break;}
// Search for next input value if product does not exist
if (isset($result) && $result == 0) {continue 1;}
}

at the bottom of the page there are while() loop codes to list the products
Apart from these, do not have any other cycle

What does this execute() function do? Perhaps it is skipping positions?

That’s a PDOStatement execute() method call.

It cannot. It’s likely that the result you are seeing is being misinterpreted by you or the sql query is not matching what you think it should. It would take having the sql query, an sql dump of the data that the query is operating on, and what result you are getting, that leads you to believe this is not working, in order to help with the problem.

Pdhr, are you that blank? Of course that CAN be a PDO call, but, it is using data from his array and it does not appear to be PDO data. What is inside the $input variable. What does the execute command do? More info…

I’m definitely making a mistake, but I can’t find it right now. i keep searching

Input information to be searched from database

 WHERE rf_if_group_splitter.product_stock=? AND if_rf_input_output.if_rf_inputoutput=? GROUP BY brands.brand ");
$splitter->execute(['1',$input])

This is how I use

Do you actually have a table named if_rf_input_output and a field column named if_rf_inputoutput ?
Are those correct? And, have you verified that the data inside the $input variable is correct?

I am confused as you mention ->execute([$input]) and then ->execute([‘1’,$input]) …
You can NOT execute with two different numbers of array data. They need to be the same count.

I’m using JOIN, no problem here
I found the problem
The value returns 1 because there is only one product in the splitter table
if (isset($result) && $result == 1) {break;} It works when there is 1 value

However, since there are 4 products in the tap off table, it returns 4, not 1
So it goes all the way to the end of the array

I changed it like this

if (isset($result) && $result > 0) {break;}
if (isset($result) && $result < 1) {continue 1;}

This way the problem is solved
thank you all so much

It can be amateurish. :grinning: :grinning: :grinning:

Sponsor our Newsletter | Privacy Policy | Terms of Service