Question

I want to use a star rating system for my website so someone can rate a post or a picture,so i use netbeans synfony,first this is the 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>

and i have this file ajax.php which will save the data in database :

<?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'");
    }
} 
?>

and the 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);
 ?>

What i want to know is how can i integrate this(controller,router,view) in my project synfony2 so that i can save the value of the rate in my table Notesr in the column Note ? THanks in Advance

Was it helpful?

Solution

Before you start developing your web applications using Symfony2, I would advice you to look through the official documentation. It will give you a better understanding of how the Symfony framework works. The following links should get you going:

  1. The big picture, read The View, The Controller, The Architecture as well - will show you how to work with symfony overall and 'what' goes 'where'.
  2. Symfony2 and HTTP Fundamentals - will teach you how to get $_GET,$_POST,$_COOKIES,$_FILES the Symfony way
  3. Routing - will teach you how the routing system works in Symfony
  4. Database & Doctrine - will teach you how to manage your database by using Doctrine and Entities.

Read the tutorials from the links in the order I gave them to you and you should be able to transform your code to work in Symfony2 within a couple of hours.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top