3

Possible Duplicate:
How can I execute a block of code exactly once in PHP?

I have a code that basically checks the current category assigned to the post, and retrieve a code according to it. If there are more then 1 categories assigned to the post, it's rendering both the codes or the same one twice..
I wondered if there's an option to make the code to work only once.

<?php 
foreach((get_the_category()) as $category){ 
  if($category->cat_ID == '4'){ 
    echo "....TEXT...."; 
  } else { 
    echo "....TEXT2....";
  }
}
?>

Thanks!

5
  • 4
    duplicate, duplicate, duplicate. Use search functionality before posting.
    – eykanal
    Jul 19, 2011 at 13:36
  • 3
    NONE of those duplicate answers answer this question even remotely.
    – Phil
    Jul 19, 2011 at 13:39
  • @eykanal: The only similarities between this question and those supposed duplicates are the titles.
    – ADW
    Jul 19, 2011 at 13:40
  • perheps run the foreach first, save the result in case it is twice in a variable, then display the the variable whatever if is twice or once, just display one time of course after the foreach Jul 19, 2011 at 13:42
  • @ADW - you're right, mea culpa. There was an identical question to this just a few days ago and it just set me off, and a quick search showed the other "dupes", but these aren't the one I was thinking of.
    – eykanal
    Jul 19, 2011 at 13:44

4 Answers 4

5
$first = true;    
foreach((get_the_category()) as $category) { 
        if($category->cat_ID == '4') { 
           echo "....TEXT....";
           $first = false;
        } else { 
           echo "....TEXT2....";}
           $first = false;
        }

        if(!$first) break;
    }
5
  • This code will execute unnecessarily multiple times and therefore spend more resources...
    – Goran
    Jul 19, 2011 at 13:40
  • Okay, I see what you mean, let me think of a better solution.
    – Phil
    Jul 19, 2011 at 13:43
  • 1
    No need, just take a look at my answer ;)
    – Goran
    Jul 19, 2011 at 13:44
  • haha, just outa curiosity, how about now?
    – Phil
    Jul 19, 2011 at 13:45
  • If statement and $first variable are unnecessary...
    – Goran
    Jun 16, 2014 at 8:26
2

Try this:

<?php 
foreach((get_the_category()) as $category){ 
  if($category->cat_ID == '4'){ 
    echo "....TEXT...."; 
  } else { 
    echo "....TEXT2....";
  }
  break;
}

?>

Break is used to get out of loop regardless of the condition. Please refer to this article for manual.

2
  • This will not work because it will break out even if the category isn't 4 :D
    – Phil
    Jul 19, 2011 at 13:43
  • No, because the loop will execute only once...
    – Goran
    Jul 19, 2011 at 13:45
1

Add a variable to the inside of the loop that flags the code as run. Then check for that variable before running:

<?php 
   if($first_run) {
      foreach((get_the_category()) as $category){ 
         if($category->cat_ID == '4'){ 
               echo "....TEXT...."; 
         } 
         else { 
               echo "....TEXT2....";
         }
        }
      $first_run = TRUE;
      }
        ?>

This will only work if you have the code called from a function or if you are including it multiple times in the same script. If you want it to only run once per session (across several pages), you should use a SESSION variable.

0

complementing my comment above.

<?php 
      foreach((get_the_category()) as $category){ 
         if($category->cat_ID == '4'){ 
               $varCateg = "....TEXT...."; 
         } 
         else { 
               $varCateg = "....TEXT...."; 
         }
        }

// diplay varCateg here
echo $varCateg; 

?>

doing this will not show up the categ twice.

Not the answer you're looking for? Browse other questions tagged or ask your own question.