How to divide two values from a data table ensuring that it is not divided by zero?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BaseballGraphs
    New Member
    • Sep 2010
    • 75

    How to divide two values from a data table ensuring that it is not divided by zero?

    Hello,

    I am trying to divide one value from my data table with an associated ID by another value from my data table with a different ID for the same day that the value was added to the data table.

    I update my data table daily with several values that have different IDs that label the value type.

    Certain of the values I add to my data table also have a value of 0. Therefore, if I want to divide one value by another, I have to ensure that the rows that contain a value of 0 are not counted.

    After I divide all of the values into a new value, I then need to store that value in my data table with a new ID so that I can differentiate it.

    I have some code that I've been working on but am getting tricked up at this point and would appreciate any further assistance.

    Code:
    <?
    
      function getValue ( $iPlayer, $iStat ) {
        $sql = sprintf( "SELECT value FROM data WHERE player_id = '%d' and statistic_id = '%d' and day = CURdate()", $iPlayer, $iStat);
    	$result = mysql_query ( $sql );
      }
    
      function getListofPlayers ( ) {
        $sql = 'SELECT player_id FROM data WHERE day = CURdate()';
    	$result = mysql_query ( $sql );
      }
    
      function storeValue ( $iPlayer, $iStat, $value ) {
        $sql = sprintf( "INSERT INTO data SET player_id = '%d', statistic_id = '%d', value = '%s', day = NOW()", $iPlayer,  $iStat,  $value );
    	$result = mysql_query ( $sql );
      }
    
      $aPlayer = getListOfPlayers();
      foreach ( $aPlayer as $oPlayer ) {
    
       $stat_id_1 = 9;
       $stat_id_2 = 4;
       $stat_id_new = 39;
    
        $a = getValue( $oPlayer->id, $stat_id_1 );
        $b = getValue( $oPlayer->id, $stat_id_2 );
        $c = $a / $b;
    
       storeValue ( $oPlayer->id, $stat_id_new, $c );
    
      }
    
    ?>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Check if the value is 0?

    Code:
    if variable equals 0
      do not divide
    else
      do divide
    endif

    Comment

    Working...