Im trying to run two php scripts, the first checks if a flagged game was chosen, if it was not i want the code to move on without running the second cookie check php code.
Only run the second code if the first has equal strings.
What should separate the two codes so it works as intended.

<?php
$string1 = $row['title'];
$string2 = ["game 1", "game 2", "game 3"];

// Check if any of the titles in $string2 are equal to $string1
foreach ($string2 as $title) {
    if ($title == $string1) {


//Dont run this code below if $string1 is not equal to $string2



  if (isset($_COOKIE['My Cookie'])) {
  // The cookie exists
  echo '<script>window.location.href = "https://www.mysite.com";</script>';
} else {
  // The cookie doesn't exist
  echo <<<EOF
<script type="text/javascript">

[Added this forum's [code]...[/code] tags around the code block ~ MOD]

    (a) put everything below "Dont run this code below if string1 is not equal to string2" down to the bottom of the loop in an else. Or use a continue to go immediately back to the top of the loop.
    (b) Use functions a bit more so that "do this if the strings are the same" and "do this if the strings are different" can be single lines.

      Because i'm bored, taking what Weedpacket said, I streamlined the logic a bit (using in_array() and some ternary operator fun) and moved the output details into functions. Maybe this helps? 🤷

      <?php
      
      $titles = ["game 1", "game 2", "game 3"];
      
      if(in_array($row['title'], $titles)) {
        echo (isset($_COOKIE['My Cookie'])) ? has_cookie() : no_cookie();
      } else {
        echo invalid_title();
      }
      
      function has_cookie() {
        return <<<EOF
      Whatever you want to output if the title is valid and the cookie is set
      EOF;
      }
      
      function no_cookie() {
        return <<<EOF
      Whatever you want to output if the title is valid but the cookie is not set
      EOF;
      }
      
      function invalid_title() {
        return <<<EOF
      Whatever you want to output if the title is not valid
      EOF;
      }
      

        Thanks for the help, this code has to go on line 357 of a larger php script, on there own, they ran perfect, but again the problem was trying to join the two scripts without trashing the whole php pages output, and ai keeps saying use "else if" but ai is not perfect yet.

        What i wanted to do if row title is not equal, was just move on to open the game chosen by the end user, so this line function invalid_title() {
        return <<<EOF
        Whatever you want to output if the title is not valid
        EOF; I would have to remove would that be correct?
        Also i might explain that there is a javascript prompt that will ask the users birth date for some games, they get one chance to answer correctly, if not 18 the cookie times them out of those flagged games for 30 min. and by then they will have moved on.

          Will this line, if(in_array($row['title'], $titles)) {
          echo (isset($_COOKIE['My Cookie'])) ? has_cookie() : no_cookie(); echo the cookie code only if the string are equal?

            mike-345 line 357 of a larger php script ... again the problem was trying to join the two scripts without trashing the whole php pages output

            Which is why functions were invented.

            ai keeps saying use "else if" but ai is not perfect yet.

            If it were perfect then you wouldn't be writing any of this in the first place.

              Well im happy to report the code NogDog gave me worked very nice!
              Thanks again for all your help.

                Write a Reply...