Advanced PHP foreach loop..nested ifelse

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I m having a bit of a brain-fart here...lol

I was using a forloop() initially for this.. but then the conditions got a bit more advanced on things I needed to check for, as well as the output based on found/not found..

My problem is ensuring that on consecutive loops.. the else on the (no access) potion only gets echo d once... but of course on every loop it will check from the top and if not a match output the no-access text.. (on every iteration).. where it should only output once.

Originally there was only a few if() statement/checks in the foreach() loop.. where a simple break; took care of things fine...

but these if() s turned into if/else.. which means the else will get triggered on the next loop .. how can this be prevented?

$arrwebinars = ("Name1", "Name3");

foreach($arrwebinars as $webinar) {

   /* Webinar 1 */
   if($webinar ==  Name1 ) {    
        if($web01_title !=   ) {
            echo "available";                   
        } else {
            echo "not available";
        }
  } else {
    echo "no access";
  } 

  /* Webinar 2 */
  if ($webinar ==  Name2 ) {    
     if ($web02_title !=   ) {
        echo "available";                   
     } else {
        echo "not available";
     }
  } else {
     echo "no access";
  }

  /* Webinar 3 */
  if ($webinar ==  Name3 ) {    
    if($web03_title !=   ) {
        echo "available";                   
    } else {
        echo "not available";
    }
  } else {
    echo "no access";
  }
}

is there some other sort of control I can use to ensure the main if/else only gets executed once?

Sorry , I am having a hard time trying to describe this one, hopefully the code explains it all (and the problem)

thanks.


update: Current State: reverted back to foreach() loop.

User is only authorized for 2 vids (#5 & #6 we ll say..can be any of the 6 in reality)

Upon first loop/iteration.. vids 1-4 output (no access for you!) (because they dont, only #5 & #6 as stated above)

no. 5 outputs the embed code fine (access)

no. 6 says No access for you .. (even though they do/should have access)

Upon the scond iteration..vids 1-4 are duplicated, and again say "No access for you" (outside of it being a duplication..this is correct).. however, vid #5 NOW says "no access for you" (first loop it outputted fine, second loop it is looking for a new match while not what I want.. in theory this is correct.. but should have a duplicate)

Vid #6 is NOW outputting the embed code (where as on first loop it did not)..

The user has only purchased access to 2 vids.. so the loop only happens twice.

but I get 12 echo s

No matter what I should only get 6 echo s with 1 out put per video (available/not available -or- no access)

I cant seem to wrap my head around this today.. :(

Goal I want achieve:

6 outputs TOTAL

each output (echo) should ONLY have: available or not available printed (this is the nested conditional check for the blank title) -or- no access

there are 6 video to check against. the user can have purchased access to 1 or up to all 6 vids

first check is: is the webinar name in their purchased array found in the total available 6 vids available

second tier conditional check: if YES (match found == access)...then check to see if the title is missing (if there output access [video embed code]... if not there output text not available yet )

(going back to first conditional check) if there is NO ACCESS (match not found).. output no access text.

This works if I wanted duplicates on the stage/page.. (but I dont!) LOL..

I need to be limited to ONLY 6 outputs,.. as there are only a total of 6 vids available.

I DO NOT WANT:

on loop 1 it outputs access for vid#1 and #2-#6 are no access.. on loop 2 it re-states all of these outputs, has vid#1 no-access now, vid#2 has access and vids #3-#6 are no access...

etc.etc.. and the cycle continues. I m getting lost on this one!..

thanks!

Answers

If you wish to stop considering that "row" when you encounter a "no access" then you could simply continue; after each no-access. or do you wish to check the others even if you encounter "no access" on the first, just to fail silently in that case?

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/25330448/advanced-php-foreach-loop-nested-if-else

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils