Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2013
    Posts
    706
    Plugin Contributions
    0

    Default division by zero

    im getting this warning in logs
    PHP Warning: Division by zero in /var/www/clients/xxx/xxx/web/includes/templates/xxx/jscript/super_data_head.php on line 281.
    line 281
    Code:
    $ratingValue = round($ratingSum / $reviewCount, 1);

  2. #2
    Join Date
    Feb 2006
    Location
    Tampa Bay, Florida
    Posts
    7,074
    Plugin Contributions
    267

    Default Re: division by zero

    if ($reviewCount != 0) {
    $ratingValue = round($ratingSum / $reviewCount, 1);
    } else {
    ... do something. maybe $ratingValue = "N/A";
    }
    That Software Guy. My Store: Zen Cart Modifications
    Available for hire - See my ad in Services
    Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.

  3. #3
    Join Date
    Jan 2013
    Posts
    706
    Plugin Contributions
    0

    Default Re: division by zero

    here is a little more of the code
    Code:
    $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            $ratingValue = round($ratingSum / $reviewCount, 1);
        }
        if ( $reviewCount > 0 ) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      }

  4. #4
    Join Date
    Jan 2013
    Posts
    706
    Plugin Contributions
    0

    Default Re: division by zero

    this did not work
    if ($reviewCount != 0) {

  5. #5
    Join Date
    Oct 2008
    Location
    Croatia
    Posts
    1,202
    Plugin Contributions
    12

    Default Re: division by zero

    Here's a working copy:
    Code:
    if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            if ($reviewCount > 0) {
                $ratingValue = round($ratingSum / $reviewCount, 1);
            } else {
                // do not bother
            }
        }
    Zen Cart Point of Sale? Sure: ZX POS - v2 released
    My site - Pro ZC Help | My portfolio | My plugins

  6. #6
    Join Date
    Jan 2013
    Posts
    706
    Plugin Contributions
    0

    Default Re: division by zero

    can u be a little bit more precise, i tried but it blanks the screen.
    how do i change this
    Code:
        $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            $ratingValue = round($ratingSum / $reviewCount, 1);
        }
        if ($reviewCount != 0) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      },
      "review" : [
      <?php for ($i = 0, $n = sizeof($reviewArray); $i<$n; $i ++) { ?>
      {
        "@type" : "Review",
        "author" : {
          "@type" : "Person",
          "name" : <?php echo json_encode(strtok($reviewArray[$i]['customerName']," ")); //steve to use only the forename, encoded, does NOT need enclosing quotation marks ?>
        },
        "reviewBody" : <?php echo json_encode($reviewArray[$i]['reviewText']); //steve added json_encode to catch quotation marks and pesky accents etc., does NOT need enclosing quotation marks ?>,
        "datePublished" : "<?php echo substr($reviewArray[$i]['dateAdded'], 0, 10); ?>",
        "reviewRating" : {
          "@type" : "Rating",
          "ratingValue" : "<?php echo $reviewArray[$i]['reviewRating']; //steve bug: was fixed at $ratingValue ?>"
          }
        }<?php if ($i+1 != $n) { ?>,<?php } ?>
      <?php } ?>
      ]
    <?php } //if no reviews, aggregateRating makes no sense ?>
    }
    </script>
            <?php } //eof Product Schema
        }//eof Schema enabled ?>
    into this
    Code:
    if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            if ($reviewCount > 0) {
                $ratingValue = round($ratingSum / $reviewCount, 1);
            } else {
                // do not bother
            }
        }
    i think cause i took away the closing ?>

  7. #7
    Join Date
    Jul 2012
    Posts
    15,007
    Plugin Contributions
    17

    Default Re: division by zero

    Quote Originally Posted by jimmie View Post
    here is a little more of the code
    Code:
    $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            $ratingValue = round($ratingSum / $reviewCount, 1);
        }
        if ( $reviewCount > 0 ) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      }
    Quote Originally Posted by jimmie View Post
    can u be a little bit more precise, i tried but it blanks the screen.
    how do i change this
    Code:
        $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            $ratingValue = round($ratingSum / $reviewCount, 1);
        }
        if ($reviewCount != 0) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      },
      "review" : [
      <?php for ($i = 0, $n = sizeof($reviewArray); $i<$n; $i ++) { ?>
      {
        "@type" : "Review",
        "author" : {
          "@type" : "Person",
          "name" : <?php echo json_encode(strtok($reviewArray[$i]['customerName']," ")); //steve to use only the forename, encoded, does NOT need enclosing quotation marks ?>
        },
        "reviewBody" : <?php echo json_encode($reviewArray[$i]['reviewText']); //steve added json_encode to catch quotation marks and pesky accents etc., does NOT need enclosing quotation marks ?>,
        "datePublished" : "<?php echo substr($reviewArray[$i]['dateAdded'], 0, 10); ?>",
        "reviewRating" : {
          "@type" : "Rating",
          "ratingValue" : "<?php echo $reviewArray[$i]['reviewRating']; //steve bug: was fixed at $ratingValue ?>"
          }
        }<?php if ($i+1 != $n) { ?>,<?php } ?>
      <?php } ?>
      ]
    <?php } //if no reviews, aggregateRating makes no sense ?>
    }
    </script>
            <?php } //eof Product Schema
        }//eof Schema enabled ?>
    into this
    Code:
    if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            if ($reviewCount > 0) {
                $ratingValue = round($ratingSum / $reviewCount, 1);
            } else {
                // do not bother
            }
        }
    i think cause i took away the closing ?>
    I have/suggest a simpler correction for this issue considering that at least the three variables have an initial assignment that helps with the rest of the processing.

    Using the first group of additional code change:
    Code:
    $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            $ratingValue = round($ratingSum / $reviewCount, 1);
        }
        if ( $reviewCount > 0 ) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      }
    To:
    Code:
    $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (!empty($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            $ratingValue = round($ratingSum / $reviewCount, 1);
        }
        if ( $reviewCount > 0 ) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      }
    The reason I suggest this is, the test of '!empty' basically says if there is data in the variable (which includes an array that has 1 or more elements). If there are no elements in the array or variable, then the divide by zero line will not be processed and the $reviewCount will be 0 and therefore continue processing as if there was no rating to evaluate. Further, even if there is data assigned to the variable, it is necessary to know if it is an array or as is available/better in more recent php versions to check if the variable is iterable or traversable. Otherwise, you can accomplish the above direction by changing:
    Code:
    $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            $ratingValue = round($ratingSum / $reviewCount, 1);
        }
        if ( $reviewCount > 0 ) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      }
    to:
    Code:
    $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            if ($reviewCount > 0) {
            $ratingValue = round($ratingSum / $reviewCount, 1);
            }
        }
        if ( $reviewCount > 0 ) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      }
    Or even better than that to:
    Code:
    $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
        }
        if ( $reviewCount > 0 ) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared 
            $ratingValue = round($ratingSum / $reviewCount, 1);
    ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      }
    Since the check for $reviewCount is already present...

    Looking at the "longer code":
    Code:
        $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            $ratingValue = round($ratingSum / $reviewCount, 1);
        }
        if ($reviewCount != 0) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      },
      "review" : [
      <?php for ($i = 0, $n = sizeof($reviewArray); $i<$n; $i ++) { ?>
      {
        "@type" : "Review",
        "author" : {
          "@type" : "Person",
          "name" : <?php echo json_encode(strtok($reviewArray[$i]['customerName']," ")); //steve to use only the forename, encoded, does NOT need enclosing quotation marks ?>
        },
        "reviewBody" : <?php echo json_encode($reviewArray[$i]['reviewText']); //steve added json_encode to catch quotation marks and pesky accents etc., does NOT need enclosing quotation marks ?>,
        "datePublished" : "<?php echo substr($reviewArray[$i]['dateAdded'], 0, 10); ?>",
        "reviewRating" : {
          "@type" : "Rating",
          "ratingValue" : "<?php echo $reviewArray[$i]['reviewRating']; //steve bug: was fixed at $ratingValue ?>"
          }
        }<?php if ($i+1 != $n) { ?>,<?php } ?>
      <?php } ?>
      ]
    <?php } //if no reviews, aggregateRating makes no sense ?>
    }
    </script>
            <?php } //eof Product Schema
        }//eof Schema enabled ?>
    Oye there are some improvements that can be made here:
    Code:
        $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (!empty($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
        }
        if ($reviewCount != 0) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared 
            $ratingValue = round($ratingSum / $reviewCount, 1);
    ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      },
      "review" : [
      <?php for ($i = 0; $i<$reviewCount; $i ++) { ?>
      {
        "@type" : "Review",
        "author" : {
          "@type" : "Person",
          "name" : <?php echo json_encode(strtok($reviewArray[$i]['customerName']," ")); //steve to use only the forename, encoded, does NOT need enclosing quotation marks ?>
        },
        "reviewBody" : <?php echo json_encode($reviewArray[$i]['reviewText']); //steve added json_encode to catch quotation marks and pesky accents etc., does NOT need enclosing quotation marks ?>,
        "datePublished" : "<?php echo substr($reviewArray[$i]['dateAdded'], 0, 10); ?>",
        "reviewRating" : {
          "@type" : "Rating",
          "ratingValue" : "<?php echo $reviewArray[$i]['reviewRating']; //steve bug: was fixed at $ratingValue ?>"
          }
        }<?php if ($i+1 != $reviewCount) { ?>,<?php } ?>
      <?php } ?>
      ]
    <?php } //if no reviews, aggregateRating makes no sense ?>
    }
    </script>
            <?php } //eof Product Schema
        }//eof Schema enabled ?>
    To capture the specific modifications only (because I can't seem to stop seeking improved code):

    Code:
        $ratingSum = 0;
        $ratingValue = 0;
        $reviewCount = 0;
        if (isset($reviewArray) && (is_array($reviewArray))){
            $reviewCount = sizeof($reviewArray);
            foreach ($reviewArray as $row) {
                $ratingSum += $row['reviewRating'];
            }
            if ($reviewCount > 0) {
            $ratingValue = round($ratingSum / $reviewCount, 1);
            } else {
              // do not bother
            }
        }
        if ($reviewCount != 0) { //do not bother if no reviews at all. Note note best/worstRating is for the max and min rating used in this review system. Default is 1 and 5 so no need to be declared ?>
      ,
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo $ratingValue; //average rating based on all reviews ?>",
        "reviewCount": "<?php echo $reviewCount; ?>"
      },
      "review" : [
      <?php for ($i = 0, $n = sizeof($reviewArray); $i<$n; $i ++) { ?>
      {
        "@type" : "Review",
        "author" : {
          "@type" : "Person",
          "name" : <?php echo json_encode(strtok($reviewArray[$i]['customerName']," ")); //steve to use only the forename, encoded, does NOT need enclosing quotation marks ?>
        },
        "reviewBody" : <?php echo json_encode($reviewArray[$i]['reviewText']); //steve added json_encode to catch quotation marks and pesky accents etc., does NOT need enclosing quotation marks ?>,
        "datePublished" : "<?php echo substr($reviewArray[$i]['dateAdded'], 0, 10); ?>",
        "reviewRating" : {
          "@type" : "Rating",
          "ratingValue" : "<?php echo $reviewArray[$i]['reviewRating']; //steve bug: was fixed at $ratingValue ?>"
          }
        }<?php if ($i+1 != $n) { ?>,<?php } ?>
      <?php } ?>
      ]
    <?php } //if no reviews, aggregateRating makes no sense ?>
    }
    </script>
            <?php } //eof Product Schema
        }//eof Schema enabled ?>
    Some other "routine" changes that have been presented in other code would be to change "sizeof" to "count" and to remove some of the extra parentheses that do not serve a true purpose such as changing: "&& (is_array($reviewArray))" to "&& is_array($reviewArray)".

    Had enough yet? :)
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...
    Upgraded to Zen Cart V1.5.3 from V1.5.1 from V1.5.0 from V1.3.9h

 

 

Similar Threads

  1. Coupon division by zero?
    By Orchard_Direct in forum Discounts/Coupons, Gift Certificates, Newsletters, Ads
    Replies: 2
    Last Post: 31 Jul 2009, 05:16 PM
  2. Division by zero error
    By simetra in forum General Questions
    Replies: 2
    Last Post: 6 Jul 2007, 08:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR