如何动态地为产品添加星级评级

时间:2012-05-22 06:58:53

标签: php jquery mysql ajax

我一直在尝试为我的产品添加星级评分,但我一直收到错误消息。我已经从mysql调用了产品和评级数据,但它不起作用..我的代码在下面请帮助我

/ * rating.php * /

//这会处理评级输入

<?php
include("db.php");
$users_ip = $_SERVER['REMOTE_ADDR'];
if($_REQUEST['value'])
{
$id = $_REQUEST['value'];
$r_id = $_REQUEST['$rid'];
$result = mysql_query("select users_ip from tbl_rating where users_ip='$users_ip'");
$num = mysql_num_rows($result);

if($num==0)
{
$query = "insert into tbl_rating (rating,rate_id,users_ip) values ('$id','$r_id','$users_ip')";
    mysql_query( $query);

    $result=mysql_query("select sum(rating) as rating from tbl_rating");
    $row=mysql_fetch_array($result);

    $rating=$row['rating'];

    $quer = mysql_query("select rating from tbl_rating");
    $all_result = mysql_fetch_assoc($quer);
    $rows_num = mysql_num_rows($quer);
    if($rows_num > 0){
    $get_rating = floor($rating/$rows_num);
    $rem =  5 - $get_rating;
    }?>

    <?php
    for($k=1;$k<=$get_rating;$k++){?>
    <div class="rating_enb" id="<?php echo $k?>">&nbsp;</div>
    <?php
    }?>
    <?php
    for($i=$rem;$i>=1;$i--){?>
    <div class="rating_dis" id="<?php echo $k?>">&nbsp;</div>
    <?php
    $k++;
    }?>   
    <div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating : '0')?> /5</div>
    <div class="user_message"><?php echo $rows_num?> times rated</div>
<?php
}
else
{
    echo '<div class="rating_message">You already did it !</div>';
}
}
if(@$_REQUEST['show']) // show rating again after showing message
{
$result=mysql_query("select sum(rating) as rating from tbl_rating");
$row=mysql_fetch_array($result);

$rating=$row['rating'];

$quer = mysql_query("select rating from tbl_rating");
$all_result = mysql_fetch_assoc($quer);
$rows_num = mysql_num_rows($quer);
if($rows_num > 0){
$get_rating = floor($rating/$rows_num);
$rem =  5 - $get_rating;
}?>
<?php
for($k=1;$k<=$get_rating;$k++){?>
<div class="rating_enb" id="<?php echo $k?>">&nbsp;</div>
<?php
}?>
<?php
for($i=$rem;$i>=1;$i--){?>
<div class="rating_dis" id="<?php echo $k?>">&nbsp;</div>
<?php
$k++;
}?>   
<div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating : '0')?> / 5</div>
<div class="user_message"><?php echo $rows_num?> times rated</div>
<?php
}?>

/ * index.php * /     //在这里,我希望与产品一起回应星级评级,但它不起作用

<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>

<script type="text/javascript">
// <![CDATA[   
$(document).ready(function(){   
$('#loader').hide();
$('#inner').children().click(function(){
    var a = $(this).attr("id");
    $.post("rating.php?value="+a, {
    }, function(response){
        $('#inner').fadeOut();
        $('#inner').html(unescape(response));
        $('#inner').fadeIn();
        setTimeout("hideMesg();", 2000);
    });
});   
});   

function hideMesg(){

$('.rating_message').fadeOut();
$.post("rating.php?show=1", {
}, function(response){
    $('#inner').html(unescape(response));
    $('#inner').fadeIn('slow');
});
}   
// ]]>
</script>

<?php
            $resultm=mysql_query("select * from ratesummary GROUP BY pd_id");
            $resultb=mysql_query("select sum(rating) as rating from ratesummary GROUP BY pd_id");
    $rowb=mysql_fetch_array($resultb);

$rating=$rowb['rating'];

$quer = mysql_query("select rating from ratesummary GROUP BY pd_id");
$all_result = mysql_fetch_assoc($quer);
$rows_num = mysql_num_rows($quer);

if($rows_num > 0){
$get_rating = floor($rating/$rows_num);
$rem =  5 - $get_rating;
}
else
{
$rem = 5;
}
while($rowx=mysql_fetch_array($resultm))

{
                echo '<div class="ratebox">
                <p class=product_name>#'.$rowx['pd_id'].'</p>
                <p class=product_name>'.$rowx['pd_name'].'</p>  </div>'       
                ;
            }

?>



<div id="container">

    <div id="outer">
        <div id="inner">
            <?php
            for($k=1;$k<=$get_rating;$k++){?>
            <div class="rating_enb" id="<?php echo $k?>">&nbsp;</div>
            <?php
            }?>
            <?php
            for($i=$rem;$i>=1;$i--){?>
            <div class="rating_dis" id="<?php echo $k?>">&nbsp;</div>
            <?php
            $k++;
            }?>   
            <div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating :   '0')?> / 5</div>
            <div class="user_message"><?php echo $rows_num?> times rated</div>
        </div>
    </div>

</div>

使用的CSS样式

    #container{
        font-family:Arial, Helvetica, sans-serif;
        width:440px;
    }
    #customForm{
        padding: 0 10px 10px;
    }
    .user_message{ margin-top:12px; color:#006600; font-family:Georgia, "Times New  Roman", Times, serif; font-size:14px; font-weight:bold;}
    #customForm label{
        display: block;
        color:#000000;
        float:left;
        width:130px;
        font-size:14px;   
        line-height: 1.4em;
    }
    #customForm input{
        width: 220px;
         float:left;
        padding: 4px;
        text-shadow: 0 -1px 1px rgba(0,0,0,0.25);
        border-bottom: 1px solid rgba(0,0,0,0.25);
        font-size: 11px;
        border: 1px solid #CCCCCC;
    }
    input#button{
        width: 244px;
         float:left;
        padding: 4px;
        text-shadow: 0 -1px 1px rgba(0,0,0,0.25);
        border-bottom: 1px solid rgba(0,0,0,0.25);
        font-size: 11px;
        border: 1px solid #CCCCCC;
    }
    #outer{ width:150px; padding:6px;
            background:#FFFFFF;
            height:18px;
            margin-top:12px;
            color:#fff;
            margin-bottom:5px;
            font-weight:bold;
            -moz-border-radius: 6px;
            font-size:12px;
            -webkit-border-radius: 6px;
            -moz-box-shadow: 0 1px 3px rgba(0,0,0,0.6);
            -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.6);
            text-shadow: 0 -1px 1px rgba(0,0,0,0.25);
            border-bottom: 1px solid rgba(0,0,0,0.25);}

    #outer .rating_enb {
            width:16px; padding:1px;
            height:18px; float:left;
            background:#CC0000;
            margin-right:4px;   
            cursor:pointer;
        }
    #outer .rating_dis {
            width:16px; padding:1px;
            height:18px; float:left;
            cursor:pointer;
            background:#CCCCCC;               
            margin-right:4px;   
        }
    #outer .rating_dis:hover, #outer .rating_enb:hover {background:#009933;    }

    #outer .rating_value{ color:#003399; padding-top:2px; font-size:14px}
    #outer .rating_message{ color:#003399; padding-top:2px; font-size:14px}

数据库架构

CREATE TABLE `tbl_product` (
`pd_id` int(10) unsigned NOT NULL auto_increment,
`pd_name` varchar(100) NOT NULL default '',
`pd_image` varchar(200) default NULL,

PRIMARY KEY(pd_id),     )ENGINE = MyISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1007;

 CREATE TABLE `tbl_rating` (
`r_id` int(10) unsigned NOT NULL auto_increment,
`pd_id` int(10) unsigned NOT NULL default '0',
`rating` int(11) NOT NULL default '0',
`users_ip` varchar(200) NOT NULL default '0',
PRIMARY KEY  (`r_id`),
KEY `pd_id` (`pd_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

使用mysql创建视图创建表ratesummary以使用tbl_rating加入tbl_product

1 个答案:

答案 0 :(得分:0)

请尝试更改您的退货,例如这些行

    <div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating : '0')?> /5</div>
    <div class="user_message"><?php echo $rows_num?> times rated</div>

应该是这样的:

    echo "<div class='rating_value'>". ((@$get_rating) ? @$get_rating : '0')/5 ."</div>".
          "<div class='user_message'>". $rows_num . "times rated</div>";