如何整合星级评分系统SYMFONY 2?

时间:2014-05-10 17:16:17

标签: php jquery ajax symfony doctrine-orm

我想为我的网站使用星级评分系统,所以有人可以评价帖子或图片,所以我使用netbeans synfony,首先这是index.php

    <link type="text/css" rel="stylesheet" href="css/style.css">
    <link type="text/css" rel="stylesheet" href="css/example.css">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

</head>

<body>
<?php  
    require_once 'config.php';
    $post_id = '1'; 
?>



    <div class="rate-ex1-cnt">
        <div id="1" class="rate-btn-1 rate-btn"></div>
        <div id="2" class="rate-btn-2 rate-btn"></div>
        <div id="3" class="rate-btn-3 rate-btn"></div>
        <div id="4" class="rate-btn-4 rate-btn"></div>
        <div id="5" class="rate-btn-5 rate-btn"></div>
    </div>



    <div class="box-result-cnt">
        <?php
            $query = mysql_query("SELECT * FROM wcd_rate"); 
            while($data = mysql_fetch_assoc($query)){
                $rate_db[] = $data;
                $sum_rates[] = $data['rate'];
            }
            if(@count($rate_db)){
                $rate_times = count($rate_db);
                $sum_rates = array_sum($sum_rates);
                $rate_value = $sum_rates/$rate_times;
                $rate_bg = (($rate_value)/5)*100;
            }else{
                $rate_times = 0;
                $rate_value = 0;
                $rate_bg = 0;
            }
        ?>
        <hr>
        <h3>The content was rated <strong><?php echo $rate_times; ?></strong> times.</h3>
        <hr>
        <h3>The rating is at <strong><?php echo $rate_value; ?></strong> .</h3>
        <hr>
        <div class="rate-result-cnt">
            <div class="rate-bg" style="width:<?php echo $rate_bg; ?>%"></div>
            <div class="rate-stars"></div>
        </div>
        <hr>

    </div><!-- /rate-result-cnt -->

</div><!-- /tuto-cnt -->



<script>
    // rating script
    $(function(){ 
        $('.rate-btn').hover(function(){
            $('.rate-btn').removeClass('rate-btn-hover');
            var therate = $(this).attr('id');
            for (var i = therate; i >= 0; i--) {
                $('.rate-btn-'+i).addClass('rate-btn-hover');
            };
        });

        $('.rate-btn').click(function(){    
            var therate = $(this).attr('id');
            var dataRate = 'act=rate&post_id=<?php echo $post_id; ?>&rate='+therate; //
            $('.rate-btn').removeClass('rate-btn-active');
            for (var i = therate; i >= 0; i--) {
                $('.rate-btn-'+i).addClass('rate-btn-active');
            };
            $.ajax({
                type : "POST",
                url : "http://localhost/rating/ajax.php",
                data: dataRate,
                success:function(){}
            });

        });
    });
</script>

我有这个文件ajax.php,它将把数据保存在数据库中:

<?php
  require_once 'config.php';

if($_POST['act'] == 'rate'){
    //search if the user(ip) has already gave a note
    $ip = $_SERVER["REMOTE_ADDR"];
    $therate = $_POST['rate'];
    $thepost = $_POST['post_id'];

    $query = mysql_query("SELECT * FROM wcd_rate where ip= '$ip'  "); 
    while($data = mysql_fetch_assoc($query)){
        $rate_db[] = $data;
    }

    if(@count($rate_db) == 0 ){
        mysql_query("INSERT INTO wcd_rate (id_post, ip, rate)VALUES('$thepost', '$ip', '$therate')");
    }else{
        mysql_query("UPDATE wcd_rate SET rate= '$therate' WHERE ip = '$ip'");
    }
} 
?>

和config.php:

<?php 
 //change the values with your own hosting setting
 $mysql_host = "localhost";
 $mysql_database = "wcd_rating";
 $mysql_user = "root";
 $mysql_password = "";

 $db = mysql_connect($mysql_host,$mysql_user,$mysql_password);
 mysql_connect($mysql_host,$mysql_user,$mysql_password);
 mysql_select_db($mysql_database);
 ?>

我想知道的是如何在我的项目synfony2中集成这个(控制器,路由器,视图),以便我可以在列表注释中保存表中Notesr的速率值?提前谢谢

1 个答案:

答案 0 :(得分:2)

在您开始使用Symfony2开发Web应用程序之前,我建议您查看官方文档。它将使您更好地理解Symfony框架的工作原理。以下链接可以帮助您:

  1. The big picture,同时阅读The ViewThe ControllerThe Architecture - 将向您展示如何整体使用symfony以及&#39;什么&#39;去哪里&#39;
  2. Symfony2 and HTTP Fundamentals - 将教你如何以$_GET$_POST$_COOKIES$_FILES以Symfony的方式获取
  3. Routing - 将教你如何在Symfony中使用路由系统
  4. Database & Doctrine - 将教您如何使用DoctrineEntities管理您的数据库。
  5. 按照我给他们的顺序从链接中阅读教程,您应该能够在几个小时内将代码转换为在Symfony2中工作。

相关问题