0

What i want to achieve is display list of cpt with ratings icons in right side i am looping through all posts but ratings only applying into first post.

I had already tried to loop Jquery code as i am looping on custom post type but still not working.

Displaying wp cpt.

 <?php

if(is_single()) {

global $post;

$issue_ids = get_post_meta( $post->ID, '_issue_ids', true );
if(!empty($issue_ids)){
//print_r(array_values($issue_ids));
//echo $post->ID;
$args = array(
'post_type' => 'political-issue',
'post__in' => $issue_ids
);
$postslist = get_posts( $args); 

//print_r($postslist);
?>



<div class='issues_container'> 

<?php 
$num_rate = 1;
?>
<?php foreach ( $postslist as $post ) : setup_postdata( $post );  ?>


<div class="issues_list">
<p> Issue  <?php echo $num_rate; ?> : </p>
<div class="title_of_issue">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div><!-- title_of_issue -->

<div class="description_of_issue">
<?php echo wp_trim_words( get_the_content(),50,'...') ?>
</div><!-- title_of_issue -->

<!-- Adding Rating System for user only -->

<div class="user_reviews">

<?php 
global $wpdb;
$tablename = $wpdb->prefix . "rating_calculator";

//global $post;
$postID = get_the_id();
//echo $postID;
$frs_finalize = 1;
$ip=0;
$id=0;
$getIp = $wpdb->get_row( "SELECT * FROM $tablename WHERE post_id = $postID and frs_finalize=$frs_finalize" );
if(!empty($getIp)){
$ip = $getIp->frs_ipaddr;
$id = $getIp->post_id;
$limit = $getIp->frs_now;
}
if(get_client_ip_env() == $ip && $postID == $id ){

echo  "<div class='row_rwe finalized'><div class='rating_".$num_rate."' style='pointer-events: none;' title='Already Rated Post.' alt='Already Rated Post.' data-rate-value=".$limit."><p id='message'>".get_option('review_finalize_message')."</div></div>";

}else {
echo "<div class='row_rwe'><div class='rating_".$num_rate."'></div></div>";
}



echo "<form method='post' action=''> 
<input type='hidden' id='post_id' value='".$postID."'/>
<input type='hidden' id='ipaddress' value='".get_client_ip_env()."'/>
</form>";
$getarows = $wpdb->get_results( "SELECT * FROM $tablename WHERE post_id = $postID" );
$num = count( $getarows );


if(!empty($getarows)){
$woke = 0;
foreach ( $getarows as $getarow ) 
{ 
$woke += $getarow->rating_count;
}
//echo $woke;
$total = number_format($woke/$num,0);

echo "<span id='woke'> ".$total."% Woke </span>";

if($total>0 && $total <=25){
echo "<img src='".get_option('eye-0to25')."'  class='img-eye-0to25' > ";
}

if($total>26 && $total<=75){
echo "<img src='".get_option('eye-26to75')."' class='img-eye-26to75' > ";
}

if($total>76 && $total<=100){
echo "<img src='".get_option('eye-76to100')."' class='img-eye-76to100' > ";
}


}else {
echo "<span id='woke'> 0% Woke </span>";
echo "<img src='".get_option('eye-0to25')."'  class='img-eye-0to25 percentageIm' > ";
}


echo "<div id='finalize'><form method='post' action=''><input type='hidden' value='1' id='idtrue'> <input type='button' id='submitFinalReview' value='Submit'/> </div>";

echo"<style> #finalize input[type='button']{background-color:".get_option('frs_color_picker')."; font-size:".get_option('frs_button_finalize_text_size')."px;}
</style> ";

?>

</div><!-- user_reviews -->


</div><!-- issues_list -->


<?php 
//$num++;
$num_rate++;
endforeach; 
wp_reset_postdata();
?>

</div><!-- issues_container -->

<?php  
}

}  ?>

Jquery Code

for (var j = 0; j < 10; j++) {

    jQuery(".rating_" + j).on("change", function (ev, data) {
        jQuery('.im2').removeClass("actionImage");
        var post_id = jQuery("#post_id").val();
        var ipaddress = jQuery("#ipaddress").val();
        var limit = (data.to);
        //console.log(limit); 
        var stars = jQuery('.rating_1' + j + ' .rate-base-layer .im2');

        var num = 0;
        var i;
        for (i = 0; i < limit; i++) {
            jQuery(stars[i]).addClass("actionImage");
            //jQuery('.rating_1').css("pointer-events","none");
            num++;
        }
        var wokeVal = num * 20;
        var data = {
            'action': 'frs_calculation_of_woke',
            'woke_value': wokeVal,
            'post_id': post_id,
            'ipaddress': ipaddress,
            'frs_now': limit

        };
        // We can also pass the url value separately from ajaxurl for front end AJAX implementations
        jQuery.post(ajax_object.ajax_url, data, function (response) {


            jQuery('#woke').html(response.message);

            jQuery('#submitFinalReview').show();

Jquery code applying to just first post only .

enter image description here

Zaheer Abbas
  • 145
  • 3
  • 13
  • There are possibly more than that mistake, but it's a bad idea to reuse the name `i` for 2 vars that don't have the same purpose (one to set handlers and another inside the handler function). But even with that, it won't be enough, because the handler function will keep reference to only the last value for the outer `i`. See [this post] for solutions where each handler "remembers" the value for `i` – Kaddath Feb 18 '19 at 13:27
  • yes you are right i repeating variable i multiple times so removed but same result. – Zaheer Abbas Feb 18 '19 at 13:44
  • sorry about the link forgotten in previous comment, it was [this post](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – Kaddath Feb 18 '19 at 14:05

0 Answers0