title: If I have a function that prints as many times as it loops, how can I set up a check that will only print once if all results = 0?
body: <p>If I have a function that prints as many times as it loops, how can I set up a check that will only print once if all sport results combined = 0?</p>
  
  <p>I have an <code>if</code> statement within a function called <code>calc</code> that states <code>if</code> the total <code>$allTotalPicks !== "0</code> then echo <code>&lt;table&gt;&lt;/table&gt;</code>. My <code>else if</code> statement is the problem and it checks to see if <code>$allTotalPicks == 0</code> then <code>echo</code> a statement saying <code>"there is no data..."</code>. The problem is it is printing the statement as many times as it is looping the function when it is called. There are 7 sports the function checks and currently the <code>else if</code> statement <code>"there is no data..."</code> prints for every sport that <code>$allTotalPicks == 0</code>. </p>
  
  <p>How can I get it to just print once and <em>ONLY</em> if <code>$allTotalPicks == 0</code> for <em>ALL</em> the sports <code>$sports $i</code> combined? If only one or two sports has <code>$allTotalPicks == 0</code> then the <code>echo</code>statement saying <code>"there is no data..."</code> should <em>NOT</em> print. All sports would need to equal zero to print the <code>else if</code> <code>echo</code> <code>"there is no data..."</code>. Is this possible? Thank you in advance.</p>
  
  <p>You can view the current final results table in action here: <a href="https://wsplays.com/results/" rel="nofollow noreferrer">https://wsplays.com/results/</a> on the right sidebar. It is the "per sport" table this code is for. Thanks!</p>
  
  <pre><code>&lt;?php
  include_once("../../../wp-load.php");
  global $wpdb;
  
  $action = $_POST['action'];
  $val = $_POST['val'];
  
  if ($action=="persport") {
  
      if ($val=="playoftheday30") {
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 30 DAY) AND CURDATE() AND playoftheday = 'YES'";
      }
  
      if ($val=="playoftheday90") {
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 90 DAY) AND CURDATE() AND playoftheday = 'YES'";
      }
  
      if ($val=="playoftheday365") {
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 365 DAY) AND CURDATE() AND playoftheday = 'YES'";
      }    
  
      if ($val=="latebreakingplay30") {
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 30 DAY) AND CURDATE() AND latebreakingplay = 'YES'";
      }
  
      if ($val=="latebreakingplay90") { 
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 90 DAY) AND CURDATE() AND latebreakingplay = 'YES'";  
      }
  
      if ($val=="latebreakingplay365") { 
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 365 DAY) AND CURDATE() AND latebreakingplay = 'YES'"; 
      }
  
      if ($val=="vipyesterday") {
          $interval = " game_date = DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND vipplays = 'YES'";
      }
  
      if ($val=="vipplays7") {  
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 7 DAY) AND CURDATE() AND vipplays = 'YES'";  
      }
  
      if ($val=="vipplays30") {  
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 30 DAY) AND CURDATE() AND vipplays = 'YES'"; 
      }
  
      if ($val=="vipplays90") {  
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 90 DAY) AND CURDATE() AND vipplays = 'YES'";
      }
  
      if ($val=="vipplays180") {
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 180 DAY) AND CURDATE() AND vipplays = 'YES'";  
      }
  
      if ($val=="vipplays365") {
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 365 DAY) AND CURDATE() AND vipplays = 'YES'";
      } 
  
      if ($val=="yesterday") {
          $interval = " game_date = DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND vipplays = 'NO'";
      }
  
      if ($val=="past7days") {
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 7 DAY) AND CURDATE() AND vipplays = 'NO'";
      }
  
      if ($val=="thisweek") {
          $interval = "YEAR(game_date) = YEAR(CURDATE()) AND  WEEKOFYEAR(game_date)=WEEKOFYEAR(CURDATE())";
      }
  
      if ($val=="thismonth") {
          $interval = " YEAR(game_date) = YEAR(CURDATE()) AND MONTH(game_date) = MONTH(CURDATE())";
      }
  
      if ($val=="past30days") {
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 30 DAY) AND CURDATE() AND vipplays = 'NO'";
      }
  
      if ($val=="thisyear") {
          $interval = "  YEAR(game_date) = YEAR(CURDATE())";
      }
  
      if ($val=="past365days") {
          $interval = " game_date  BETWEEN (CURDATE() - INTERVAL 365 DAY) AND CURDATE() AND vipplays = 'NO'";
      }
  
      if ($val=="currentseason") {
          $interval = "currentseason";
      }
  
      function calc($interval,$result_sum,$sum_yourpick,$tot_w,$sport,$sportname) {
          global $wpdb;
  
          if ($interval=="currentseason") {
              $result = "SELECT * FROM ".$wpdb-&gt;prefix."bidsys_sport_seasons WHERE sport=  '".$sport."' AND current_season = 'YES' LIMIT 1";
              $daterange = $wpdb-&gt;get_results($result);
              // Display YIP Picks only
              $interval = "game_date BETWEEN '" . $daterange[0]-&gt;start_date . "' AND  '" . $daterange[0]-&gt;end_date . "' AND vipplays = 'YES'";
          }
  
          $allRecordWin = 0;
          $allRecordLoss = 0;
          $allRecordPush = 0;
          $allRecordPCT = 0;
          $allRecordUnits = 0;
          $allTotalPicks = 0;
          $allRecordUnitsTemp = 0;
  
          $result = " SELECT pick_odds,units_play,pick_result FROM ".$wpdb-&gt;prefix."bidsys_picklist WHERE  sport = ".$sport." AND  ".$interval."  ORDER BY game_date DESC";
          $newmeta = $wpdb-&gt;get_results($result);
  
          foreach($newmeta as $data){
              $pick_odds = 0;
              $units_play = 0;
  
              if($data-&gt;pick_odds!="") $pick_odds = $data-&gt;pick_odds;
  
              if($data-&gt;units_play!="") $units_play = $data-&gt;units_play;
  
              if($data-&gt;pick_result=='WIN'){
                  if ($pick_odds&gt;=100) {
                      $allRecordUnitsTemp = ($pick_odds/100) * $units_play;
                      $allRecordUnitsTemp = Abs(Round($allRecordUnitsTemp, 2));
                      $allRecordUnits = $allRecordUnits + $allRecordUnitsTemp;
                  } 
  
                  else if ($pick_odds&lt;100 &amp;&amp; $pick_odds!=0) {
                              $allRecordUnitsTemp = (100/$pick_odds) * $units_play;
                              $allRecordUnitsTemp = Abs(Round($allRecordUnitsTemp, 2));
                              $allRecordUnits = $allRecordUnits + $allRecordUnitsTemp;
                  }
  
                  else {
                              $allRecordUnitsTemp = (100/$pick_odds) * $units_play;
                              $allRecordUnitsTemp = Abs(Round($allRecordUnitsTemp, 2));
                              $allRecordUnits = $allRecordUnits + $allRecordUnitsTemp;
                  }
  
                  $allRecordWin = $allRecordWin + 1;
                  $allTotalPicks = $allTotalPicks + 1;
  
              } // END WIN 
  
              else if ($data-&gt;pick_result=='LOSS') {
                      $allRecordUnits = $allRecordUnits - $units_play;
                      $allRecordUnits = Round($allRecordUnits, 2);
                      $allRecordLoss = $allRecordLoss + 1;
                      $allTotalPicks = $allTotalPicks + 1;
              } 
  
              else if ($data-&gt;pick_result=='PUSH') {
                      $allRecordPush = $allRecordPush + 1;
              }
          } // END foreach
  
          if ($allTotalPicks&gt;0) {
  
              $allRecordPCT = ($allRecordWin / $allTotalPicks) * 100;
              $allRecordPCT = Round($allRecordPCT, 0);
          }
  
          if ($allRecordUnits&gt;0) {
              $all = "+".$allRecordUnits;
          }
  
          else {
              $all = $allRecordUnits;
          }
  
          /** *********************************************************/
          /** *********************************************************/
  
          if ($allTotalPicks !== 0) {    
              echo"
                  &lt;table class='sidebar_result b'&gt;
                          &lt;tr class='hdr_2'&gt;
                              &lt;td colspan='5' &gt;$sportname&lt;/td&gt;
                          &lt;/tr&gt;
                              &lt;tr class='hdr_2'&gt;
                              &lt;td&gt;W&lt;/td&gt;
                              &lt;td&gt;L&lt;/td&gt;
                              &lt;td&gt;P&lt;/td&gt;
                              &lt;td&gt;PCT&lt;/td&gt;
                              &lt;td&gt;UNITS&lt;/td&gt;
                          &lt;/tr&gt;
                              &lt;tr class='hdr_3'&gt;
                              &lt;td&gt;$allRecordWin&lt;/td&gt;
                              &lt;td&gt;$allRecordLoss&lt;/td&gt;
                              &lt;td&gt;$allRecordPush&lt;/td&gt;
                              &lt;td&gt;".$allRecordPCT."%&lt;/td&gt;
                              &lt;td&gt;$all&lt;/td&gt;
                          &lt;/tr&gt;
                  &lt;/table&gt;";
          }
  </code></pre>
  
  <p>This is where the problem starts:</p>
  
  <pre><code>        else if ($allTotalPicks == 0) { 
              echo "&lt;p style='font-size: 14px; color: #F8F8F8; padding: 10px; text-align: left;'&gt;There is no data for the current selection.&lt;br&gt;Please choose another option.&lt;/p&gt;";
          }
  
          /** *********************************************************/
          /** *********************************************************/
      } // END function
  
      $sportname = "";
      $bidsys_options = get_option('bidsys_options');
          for ( $i=1; $i&lt;8; $i++ ) {
              if ( $bidsys_options-&gt; sportsstatus[$i] != "Disabled" ) {
                  $sportname =  $bidsys_options-&gt;sportsname[$i];
                  $sport = $i;   
  
           calc($interval,$result_sum,$sum_yourpick,$tot_w,$sport,$sportname);
  
              } 
          }
  } // END else persport
  ?&gt;
  </code></pre>