I got an Warning: Division by zero in error, the page code is below while the error page code wrote in red. Any one can help me with this? Thanks! // no results found so let's add a new record for the guest if ($results == 0) $wpdb->insert( $table_votes, array( 'post_id' => "$post_id", 'user_id' => 0, 'vote' => "$vote_val", 'ip_address' => "$user_ip", 'date_stamp' => "$nowisnow" )); } else { // first try and update the existing logged in user record $data = array( "post_id" => $post_id, "vote_val" => $vote_val, "date_stamp" => $nowisnow, ); $where = array( "user_id" => $user_id, "post_id" => $post_id, "ip_address" => $user_ip ); $results = $wpdb->update($table_votes, $data, $where); // no results found so let's add a new record for the logged in user if ($results == 0) $wpdb->insert( $table_votes, array( 'post_id' => "$post_id", 'user_id' => "$user_id", 'vote' => "$vote_val", 'ip_address' => "$user_ip", 'date_stamp' => "$nowisnow" )); } // now lets get all post ids this visitor or user has voted on already // so we can set the transient values in the db // must be a guest visitor if ($user_id < 1) { $query = $wpdb->prepare("SELECT post_id FROM $table_votes WHERE user_id = 0 AND ip_address = %s", $user_ip); $myrows = $wpdb->get_col($query); // must be a registered user } else { $query = $wpdb->prepare("SELECT post_id FROM $table_votes WHERE user_id = %s", $user_id); $myrows = $wpdb->get_col($query); } $myrows = array_values($myrows); // create a unique name based off the IP so we can find it in the options table $unique_name = md5($user_ip); // first remove the existing unique transient (if any) just to be safe delete_transient('clpr_'.$unique_name); // set the unique transient with results array to expire in 30 days set_transient('clpr_'.$unique_name, $myrows, 60*60*24*30); // grab the new votes up/down for the post $row = $wpdb->get_row( $wpdb->prepare( "SELECT votes_up AS votesup, votes_down AS votesdown, votes_total AS votestotal FROM $table_votes_total WHERE post_id = %d", $post_id ) ); // calculate the total successful percentage and round to remove all decimals [COLOR="#FF0000"] $votes_percent = round($row->votesup / $row->votestotal * 100);[/COLOR] // update/create meta keys on the post so it's easy to call from the loop update_post_meta($post_id, $app_abbr. '_votes_up', $row->votesup); update_post_meta($post_id, $app_abbr. '_votes_down', $row->votesdown); update_post_meta($post_id, $app_abbr. '_votes_percent', $votes_percent); echo $votes_percent . '%'; // send back the % result so we can update the coupon % value in real-time die; // so it doesn't return an extra zero } // check if the visitor or user has already voted // called within the coupon loop function clpr_vote_check($post_id, $the_trans) { // see if the post id exists in the array meaning they already voted if (in_array($post_id, $the_trans)) return true; else return false; } // gets the transient array holding all the post ids the visitor has voted for // called before the coupon loop and used within the loop function clpr_vote_transient() { // setup the unique transient name based on md5ing their IP address $trans_name = md5(appthemes_get_ip()); // get existing transient data for the visitor or user $the_transient = get_transient('clpr_'.$trans_name); return $the_transient; } // delete all votes when the admin option has been selected function clpr_reset_votes(){ global $wpdb, $app_abbr; // empty both voting tables $wpdb->query("TRUNCATE ".$wpdb->prefix."clpr_votes_total ;"); $wpdb->query("TRUNCATE ".$wpdb->prefix."clpr_votes ;"); // now clear out all visitor transients from the options table $sql = "DELETE FROM ". $wpdb->options ." WHERE option_name LIKE '_transient_".$app_abbr."_%' OR option_name LIKE '_transient_timeout_".$app_abbr."_%'"; $wpdb->query($sql); // now clear out all vote meta data from the options table $sql = "DELETE FROM ". $wpdb->postmeta ." WHERE meta_key = '".$app_abbr."_votes_up' OR meta_key = '".$app_abbr."_votes_down' OR meta_key = '".$app_abbr."_votes_percent'"; $wpdb->query($sql); } //Display the coupon voting widget within the loop function clpr_vote_box($postID, $the_transient) { global $user_ID; $response = "<span class=\'text\'>" . __('Thanks for your response!', 'appthemes') . "</span><span class=\'checkmark\'> </span>"; ?> <div class="thumbsup-vote"> <div class="frame" id="vote_<?php the_ID(); ?>"> <?php if (clpr_vote_check($postID, $the_transient) == false) : ?> <span class="text"><?php _e('Did this coupon work for you?', 'appthemes') ?></span> <div id="loading-<?php the_ID(); ?>" class="loading"></div> <div id="ajax-<?php the_ID(); ?>"> <span class="vote thumbsup-up"> <span class="thumbsup" onClick="thumbsVote(<?php echo $postID ?>, <?php echo $user_ID; ?>, 'vote_<?php the_ID(); ?>', 1, '<?php echo $response; ?>');"></span> </span> <span class="vote thumbsup-down"> <span class="thumbsdown" onClick="thumbsVote(<?php echo $postID ?>, <?php echo $user_ID; ?>, 'vote_<?php the_ID(); ?>', 0, '<?php echo $response; ?>');"></span> </span> </div> <?php else:?> <?php clpr_votes_chart(); ?> <?php endif; ?> </div> </div> <?php } //display the coupon success % badge within the loop function clpr_vote_badge($postID, $the_transient) { global $user_ID, $app_abbr; $vpercent = round(get_post_meta($postID, $app_abbr. '_votes_percent', true)); // figure out which color badge to show based on percentage if ($vpercent >= 75) $vstyle = 'green'; elseif ($vpercent >= 40 && $vpercent < 75) $vstyle = 'orange'; else $vstyle = 'red'; ?> <span class="thumbsup-badge badge-<?php echo $vstyle ?>"><span class="percent"><?php echo $vpercent ?>%</span><span class="success"><?php _e('success', 'appthemes') ?></span></span> <?php } // display the vote results within the loop and on the admin coupon view function clpr_votes_chart(){ global $post; ?> <div class="results"> <?php // get the votes for the post $votes_up = get_post_meta($post->ID,'clpr_votes_up', true); $votes_down = get_post_meta($post->ID,'clpr_votes_down', true); Code (markup):
The error is exctly what it tells you. You try to divide by zero. Happens when there are 0 values. To fix this one thing you could do is adding a if check around the red code. Check if the returned values ( total votes ) are 0, if not calculate percent, else display $votes_percent = 0, or something like that
if($row->votestotal>0) $votes_percent = round($row->votesup / $row->votestotal * 100); else $votes_percent = 0; PHP:
thanks a million, Sky. Now, it's working. However, when set the "else $votes_percent = 0;" all votes are 0, I changed it to 100, all the votes now are 100%.
What you mean all Votes? Votes % is just to display the % of all the votes tied together. Or do you mean other pages/posts/whatever gets these % then? Try removing the "else $votes_percent = 0;" all together and lets see what happens, didnt studied your hole code, maybe some other coding take care of what we planned with the else
Hi thanks Basti, I do not know how to descript the error, however, you can find it here: couponbanks . com This error for displaying the success ratio. However, now there is another error in the template: Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/manzoon/public_html/wp-content/themes/clipper/includes/theme-voting.php on line 135 While the line 135 is if (in_array($post_id, $the_trans)) Code (markup): Anyone can help me? Thanks!