A Rob A Rob - 2 months ago 8
PHP Question

fix this rating issue adding rating on wrong post

enter image description here
enter image description here
i am having trouble finding the issue,the post with skull shown has 6 reviews given by buyer and 5 stars rating , how ever other post owned by this authur is receiving the same rating and starss ? , the stars and rating should show up only on the post it was given to.

here is small code that gets this stars up.
this is the line of code that shows stars in those boxes

if(!function_exists('PricerrTheme_my_account_reviews_area_function'))
{
function PricerrTheme_my_account_reviews_area_function()
{
global $current_user;
get_currentuserinfo();
$uid = $current_user->ID;

//-------------------------------------



global $wpdb,$wp_rewrite,$wp_query;
$third_page = $wp_query->query_vars['third_page'];

$third_page = $_GET['pg'];
if(empty($_GET['pg'])) $third_page = 'home';

?>

<div id="content" class="directly-frame account-cnt">




<!-- page content here -->
<div class="box_title3"><?php _e("My Ratings",'PricerrTheme'); ?></div>



<div class="my_box3"><div class="shopping_menu_dv">
<?php

$using_perm = PricerrTheme_using_permalinks();

if($using_perm) $rev_pg_lnk = get_permalink(get_option('PricerrTheme_my_account_reviews_page_id')). "/?";
else $rev_pg_lnk = get_bloginfo('siteurl'). "/?page_id=". get_option('PricerrTheme_my_account_reviews_page_id'). "&";


?>
<ul id="shopping_menu">
<li><a <?php echo ($third_page == "home" ? 'class="actiove"' : ""); ?> href="<?php echo $rev_pg_lnk; ?>"><?php _e("Ratings to Award","PricerrTheme"); ?></a></li>
<li><a <?php echo ($third_page == "waiting" ? 'class="actiove"' : ""); ?> href="<?php echo $rev_pg_lnk; ?>pg=waiting"><?php _e("Pending Ratings","PricerrTheme"); ?></a></li>
<li><a <?php echo ($third_page == "my_rev" ? 'class="actiove"' : ""); ?> href="<?php echo $rev_pg_lnk; ?>pg=my_rev"><?php _e("My Ratings","PricerrTheme"); ?></a></li>

</ul>

</div></div>





<?php

if($third_page == "home"):

?>

<div class="my_box3">
<div class="padd10">
<script>

jQuery(document).ready(function() {

jQuery('.dd-submit-rating').click(function() {

var id = jQuery(this).attr('rel');
var uprating = jQuery("#rating_me-" + id + " :selected").val();
var reason = jQuery("#reason-" + id).val();
if(reason.length < 10) { alert("<?php _e('Please input a longer description for your rating','PricerrTheme'); ?>"); return false; }

jQuery.ajax({
type: "POST",
url: "<?php echo get_bloginfo('siteurl'); ?>/",
data: "rate_me=1&ids="+id+"&uprating="+uprating+"&reason="+reason,
success: function(msg){

jQuery("#post-" + id).hide('slow');

}
});

return false;
});

//-------------------------



});


</script>
<?php

global $wpdb;
$query = "select distinct *, ratings.id ratid from ".$wpdb->prefix."job_ratings ratings, ".$wpdb->prefix."job_orders orders where
ratings.awarded='0' AND orders.id=ratings.orderid AND orders.uid='$uid'";
$r = $wpdb->get_results($query);

if(count($r) > 0)
{


foreach($r as $row)
{
$post = $row->pid;
$post = get_post($post);
$user = get_userdata($row->touser);

?>

<div class="post" id="post-<?php echo $row->ratid; ?>">
<div class="padd10_only">
<div class="image_holder3">
<a href="<?php the_permalink(); ?>"><img width="65" height="50"
src="<?php echo PricerrTheme_get_first_post_image($row->pid,65,50); ?>" /></a>
</div>

<div class="title_holder3" >
<h2><a href="<?php echo get_permalink($row->pid); ?>"><?php echo PricerrTheme_wrap_the_title($post->post_title,$row->pid); ?></a></h2>

<div class="c111">Rate:</div>
<div class="c111"><textarea id="reason-<?php echo $row->ratid; ?>" rows="2" cols="35"></textarea></div>

<div class="c111"> <select name="rating_me" id="rating_me-<?php echo $row->ratid; ?>">

<option value="5">5</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select>

</div>
<div class="c111 ck999">
<a href="#" rel="<?php echo $row->ratid; ?>" class="dd-submit-rating"><?php _e('Submit Rating Now','PricerrTheme') ?></a>

</div>


</div>



</div>
</div>


<?php

}

}
else
{
_e("There are no reviews to be awarded.","PricerrTheme");
}
?>


</div></div>

<?php elseif($third_page == "waiting"): ?>
<div class="my_box3">
<div class="padd10">

<?php

global $wpdb;
$query = "select distinct * from ".$wpdb->prefix."job_ratings ratings, ".$wpdb->prefix."job_orders orders,
".$wpdb->prefix."posts posts where posts.ID=orders.pid AND
ratings.awarded='0' AND orders.id=ratings.orderid AND posts.post_author='$uid'";
$r = $wpdb->get_results($query);

if(count($r) > 0)
{


foreach($r as $row)
{
$post = $row->pid;
$post = get_post($post);
$user = get_userdata($row->uid);

?>

<div class="post" id="post-<?php echo $row->ratid; ?>">
<div class="padd10_only">
<div class="image_holder3">
<a href="<?php the_permalink(); ?>"><img width="65" height="50"
src="<?php echo PricerrTheme_get_first_post_image($row->pid,65,50); ?>" /></a>
</div>

<div class="title_holder3" >
<h2><a href="<?php echo get_permalink($row->pid); ?>"><?php echo PricerrTheme_wrap_the_title($post->post_title,$row->pid); ?></a></h2>
<?php echo sprintf(__('Waiting from: %s','PricerrTheme'), $user->user_login ); ?>




</div>



</div>
</div>


<?php

}

}
else
{
_e("You have no pending reviews.","PricerrTheme");
}
?>


</div>

</div>
<?php elseif($third_page == "my_rev"): ?>

<div class="my_box3">
<div class="padd10">

<?php

global $wpdb;
$query = "select distinct *, ratings.id ratid from ".$wpdb->prefix."job_ratings ratings, ".$wpdb->prefix."job_orders orders,
".$wpdb->prefix."posts posts where posts.ID=orders.pid AND
ratings.awarded='1' AND orders.id=ratings.orderid AND posts.post_author='$uid'";
$r = $wpdb->get_results($query);

if(count($r) > 0)
{


foreach($r as $row)
{
$post = $row->pid;
$post = get_post($post);
$user = get_userdata($row->touser);

?>

<div class="post" id="post-<?php echo $row->ratid; ?>">
<div class="padd10_only">
<div class="image_holder3">
<a href="<?php the_permalink(); ?>"><img width="65" height="50"
src="<?php echo PricerrTheme_get_first_post_image($row->pid,65,50); ?>" /></a>
</div>

<div class="title_holder3" >
<h2><a href="<?php echo get_permalink($row->pid); ?>"><?php echo PricerrTheme_wrap_the_title($post->post_title, $row->pid); ?></a></h2>

<div class="c111"><b><?php _e("Rated","PricerrTheme"); ?>: </b></div>

<div class="c111"><?php

echo PricerrTheme_show_stars_our_of_number($row->grade);

?>
</div>
<div class="clear10" style="float:left"></div>
<div class="c111"><b><?php _e("Description","PricerrTheme"); ?>: </b></div>

<div class="c111"><?php echo stripslashes($row->reason); ?>
</div>


</div>



</div>
</div>


<?php

}

}
else
{
_e("You have no reviews.","PricerrTheme");
}
?>


</div></div>
<?php endif; ?>
<!-- page content here -->
</div>





<?php

PricerrTheme_get_users_links();

} }

Answer

Try to get the query using both author and post Id (Or just postId): Because the post's are independent to their ID's and they are unique but post author is just related to post but not independent because author(uid) can have multiple posts but post will have single & unique post ID

I am assuming the post Id is also in the same table for now. And giving you example with both postId, author(uid)

example

echo pricerrtheme_show_rating_star_user($post->post_author, $post_id);

Same way modify the function and mysql query

Function :

function pricerrtheme_show_rating_star_user($uid, $post_id)  { ..... }

And Query

$s = "select count(grade) cnt, sum(grade) smm from ".$wpdb->prefix."job_ratings where uid='$uid' and awarded='1' and pid='$post_id'";

Final Code

function pricerrtheme_show_rating_star_user($uid, $post_id) {
$concat = '';
$nr_ratings = 0;

 global $wpdb;
 $s = "select count(grade) cnt, sum(grade) smm from ".$wpdb->prefix."job_ratings where uid='$uid' and awarded='1' and pid='$post_id'";
$r = $wpdb->get_results($s);



 if(count($r) > 0)
   {
    $nr_ratings = $r[0]->cnt;
    $sum        = $r[0]->smm;

    if($nr_ratings > 0)
     {

        if($sum > 0)
        $sdd = ceil($sum/$nr_ratings);
        else $sdd = 1;

        for($i=1;$i<=$sdd;$i++)
        {
            $concat .= ' <img src="'.get_bloginfo('template_url').'/images/star_full.png" width="15" />';
        }

        for($i=$sdd+1;$i<=5;$i++)
        {
            $concat .= ' <img src="'.get_bloginfo('template_url').'/images/star_empty.png" width="15" />';  
          }
       }
       else
        {
        $concat = '';

         for($i=1;$i<=5;$i++)
           {
            $concat .= ' <img src="'.get_bloginfo('template_url').'/images/star_empty.png" width="15" />';
          }
      }
  }
   else
     {
       $concat = '';

         for($i=1;$i<=5;$i++)
        {
        $concat .= ' <img src="'.get_bloginfo('template_url').'/images/star_empty.png" width="15" />';
    }

  }

  return $concat." (".$nr_ratings.")";
 }

Hope this helps