Link to home
Start Free TrialLog in
Avatar of Refael
RefaelFlag for United States of America

asked on

IF within an IF statement inside FOREACH loop

Hello experts,

I am sorry if the example below is not so clear.
I will explain what i am trying to achieve:

1). I have a foreach loop

2)  While running the foreach loop I filter the data so it runs only on the data i need

3). The $filter is a variable set via a link (the get method) . So for example i check if the $filter is equal to "a". If it does  then i need to add another if statement (a filter to data) inside the foreach loop).

My problem is i am not sure how to close the entire if statements. For some reason i do not get any data displayed when i add this code.

Can someone help?

foreach($x as $y) {
	if ($filter == 'a') {
	  if (condition) { // adding an if statment to the foreach loop only if $filter is equal to a
	if ($filter == 'b') {	
		if (condition) { // adding an if statment to the foreach loop if $filter is equal to b
	if (condition) { // this is the default if statment in the foreach loop
	// the excuated code for the foreach////////////////////////////////////////

Open in new window

Avatar of Frosty555
Flag of Canada image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Refael


Hi Frosty555, thank you

That's the thing... the same code needs to run under each addition IF statement.

So if $filter == 'a' i am adding an if condition that it is actually a data filter (i am suing simpleXML). The IF testaments are serving as filters to the data in the foreach loop.

If I have no other choice then i will copy the entire code under each $filter == '' yet i am sure there is a way not to do that or?
Avatar of Refael


Frosty555 thank you. I think i can use xpath in simpleXML instead of the IF statement and then use the same method as in your example. Thank you and i am sorry for being douche :-)
Avatar of Refael


Thank you. Read my last comment.