Meddelande

Minska
No announcement yet.

Multiple 5-Star Rating Systems - fel

Minska
X
 
  • Filter
  • Klockan
  • Show
Clear All
new posts

  • Multiple 5-Star Rating Systems - fel

    Har laddat ner denna (downloadlänken funkar inte):
    http://wcetdesigns.com/tutorials/dem...r-systems.html

    Men den vill inte fungera. Jag kan se allt och när jag kör musen över kommer stjärnorna att markeras och klickar jag så skickas det värdet till $dec_rating men inget sparas. Någon som vet varför?

    index.php
    Kod:
    <html>
    <head> 
    <script src="http://wcetdesigns.com/assets/javascript/jquery.js"></script>
    <script> 
    $(function(){
       $(".star").live("mouseover", function(){ //SELECTING A STAR
         var id = $(this).attr("x"); //ID OF THE CONTENT BEING RATED
         $(".r"+id).hide(); //HIDES THE CURRENT RATING WHEN MOUSE IS OVER A STAR
         var d = $(this).attr("id"); //GETS THE NUMBER OF THE STAR
         //HIGHLIGHTS EVERY STAR BEHIND IT
         for(i=(d-1); i>=0; i--){
           $(".transparent .s"+id+":eq("+i+")").css({"opacity":"1.0"});
         }
       }).live("click", function(){ //RATING PROCESS
         var id = $(this).attr("x"); //ID OF THE CONTENT BEING RATED
         var rating = $(this).attr("id"); //GETS THE NUMBER OF THE STAR
         var data = 'id='+id+'&rating='+rating; //ID OF THE CONTENT
    
         $.ajax({
           type: "POST",
           data: data,
           url: "rate.php", //CALLBACK FILE
           success: function(e){
             $(".ajax"+id).html(e); //DISPLAYS THE NEW RATING IN HTML
           }
         });
       }).live("mouseout", function(){ //WHEN MOUSE IS NOT OVER THE RATING
         var id = $(this).attr("x"); //ID OF THE CONTENT BEING RATED
         $(".r"+id).show(); //SHOWS THE CURRENT RATING
         $(".transparent .s"+id).css({"opacity":"0.25"}); //TRANSPARENTS THE BASE
       });
    });
    </script>
    <style>
    /*HOLD THE RATING AND THE BASE TOGETHER*/ .r { position: relative; } /*MUST BE ABSOLUTE TO STACKED*/
    .rating, .transparent { position: absolute; } .rating { z-index: 1; } .star { background: url("http://wcetdesigns.com/images/icons/star.png"); cursor: pointer; float: left !important; /*KEEPS THE STAR NEXT TO EACH OTHER*/ height: 20px; width: 20px; } .transparent .star { opacity: .25; /*BASE STARS ARE TRANSPARENT UNTIL MOUSEOVER*/ } .rating .star { opacity: 1.0; /*CURRENT RATING IS VISIBLE UNTIL MOUSEOVER*/ } .votes { float: left; /*KEEPS THE NUMBER OF VOTES NEXT TO THE RATING & BASE*/ } 
    </style>
    </head>
    <body> 
    <?php
    
    include("settings.php"); //FILE THAT CONNECTS TO YOUR DATABASE
    
    $x = array(); //ARRAY TO COUNT STARS FOR EACH PIECE OF CONTENT
    $stars = array(); //ARRAY TO SEPARATE THE TRANSPARENT 5-STAR BASE BETWEEN EACH PIECE OF CONTENT
    
    $z = mysql_query("SELECT * FROM $content_table");
    while($y=mysql_fetch_array($z)){
        $id = $y["id"]; //ID OF THE CONTENT
        $content = $y["content"]; //CONTENT ITSELF
        
        $q = mysql_query("SELECT * FROM $ratings_table WHERE id='$id'");
        $n = mysql_num_rows($q);
        if($n==1){
            $v = 'vote';
        } else {
            $v = 'votes';
        }
        
        //ADDS ALL THE STAR FOR EACH PIECE OF CONTENT
        while($r=mysql_fetch_array($q)){
            $rr = $r["rating"];
            $x[$id] += $rr;
        }
        $rr = 0; //RESETS AS IT GOES TO THE NEXT PIECE OF CONTENT
        $a = $x[$id]; //THE TOTAL NUMBER OF STARS
        
        //IF THERE ARE RATINGS...
        if($n){
            $rating = $a/$n; //GETS THE AVERAGE RATING (UNROUNDED)
        }
        
        //GETS THE RATING ON THE 5-STAR SCALE (ROUNDED TO THE NEAREST 10TH)
        $dec_rating = round($rating, 1);
        
        //LOOPS THE WHOLE NUMBER OF STARS THAT THE CONTENT HAS BEEN RATED
        for($i=1; $i<=floor($rating); $i++){
            $stars[$id] .= '<div class="star s'.$id.'" x="'.$id.'" id="'.$i.'"></div>';
        }
        
        //ALL CONTENT & ITS STARS SHOWN IN HTML
        echo '<div style="border-bottom: 1px #32baed solid;"><br>'.$content.'<br><br>
        <div class="ajax'.$id.'">';
        include("current-rating.php"); //FILE WILL THE CURRENT RATING, BASE, AND VOTES
        echo '</div><br>
        </div>';
    }
    
    ?> 
    </body>
    </html>
    current-rating.php
    Kod:
    <?php
    
    $q = mysql_query("SELECT * FROM $ratings_table WHERE id='$id' AND ip='$ip'");
    $usern = mysql_num_rows($q);
    
    $y = ''; //BLANK UNLESS USER HAS RATED CONTENT
    if($usern){
        $r = mysql_fetch_assoc($q);
        if($r["rating"]){
            $y = 'You rated this a <b>'.$r["rating"].'</b>';
        }
    }
    
    //THE CURRENT RATING & THE TRANSPARENT BASE RIGHT USER TO SUBMIT A NEW RATING
    echo '<div class="rating r'.$id.'">'.$stars[$id].'</div>
    <div class="transparent">
    <div class="star s'.$id.'" x="'.$id.'" id="1"></div>
    <div class="star s'.$id.'" x="'.$id.'" id="2"></div>
    <div class="star s'.$id.'" x="'.$id.'" id="3"></div>
    <div class="star s'.$id.'" x="'.$id.'" id="4"></div>
    <div class="star s'.$id.'" x="'.$id.'" id="5"></div>
    <div class="votes">('.$dec_rating.'/5, '.$n.' '.$v.') '.$y.'</div>
    </div>';
    
    ?>
    rate.php
    Kod:
    <?php
    
    include("settings.php"); //FILE THAT CONNECTS TO YOUR DATABASE
    
    $rating = $_POST["rating"];
    $id = $_POST["id"];
    $ip = $_SERVER["REMOTE_ADDR"];
    $date = date("l, F j, Y"). " at " .date("h:i:s A");
    $timestamp = time();
    
    //CHECKS TO SEE IF USER HAS ALREADY RATED THE CONTENT
    $q = mysql_query("SELECT * FROM $ratings_table WHERE id='$id' AND ip='$ip'");
    $nn = mysql_num_rows($q);
    $r = mysql_fetch_assoc($q);
    
    //CHECKS IF THE CONTENT EXISTS
    $q = mysql_query("SELECT * FROM $ratings_table WHERE id='$id'");
    $content_exists = mysql_num_rows($q);
    
    if($content_exists){
        if($nn){
            //CHECKS TO SEE IF THE RATING IS THE SAME AS BEFORE
            if($rating!=$r["rating"]){
                //IF SO, THE RATING WILL BE UPDATED
                mysql_query("UPDATE $ratings_table SET rating='$rating' WHERE id='$id' AND ip='$ip'");
            }
        } else {
            //ADDS THE NEW RATING, IF USER HAS NOT ALREADY RATING
            $range = range(1, 5);
            if(in_array($rating, $range)){
                mysql_query("INSERT INTO $ratings_table VALUES('', '$id', '$rating', '$ip', '$date', '$timestamp')");
            }
        }
    }
    
    $q = mysql_query("SELECT * FROM $ratings_table WHERE id='$id'");
    $n = mysql_num_rows($q);
    if($n==1){
        $v = 'vote';
    } else {
        $v = 'votes';
    }
        
    //ADDS ALL THE STAR FOR THE CONTENT BEING RATED
    while($s=mysql_fetch_array($q)){
        $rr = $s["rating"];
        $x += $rr;
    }
    $a = $x;
    
    //IF THERE ARE RATINGS...
    if($n){
        $rating = $a/$n; //GETS THE AVERAGE RATING (UNROUNDED)
    }
    
    //GETS THE RATING ON THE 5-STAR SCALE (ROUNDED TO THE NEAREST 10TH)
    $dec_rating = round($rating, 1);
    
    //LOOPS THE WHOLE NUMBER OF STARS THAT THE CONTENT HAS BEEN RATED
    for($i=1; $i<=floor($rating); $i++){
        $stars[$id] .= '<div class="star s'.$id.'" x="'.$id.'" id="'.$i.'"></div>';
    }
    
    include("current-rating.php"); //FILE WITH THE CURRENT RATING, BASE, AND VOTES (REFRESHES AFTER THE USER RATES CONTENT)
    
    ?>
    settings.php
    Kod:
    <?php
    
    //CONNECTS TO YOUR DATABASE (MODIFY TO YOUR OWN SETTINGS)
    $c = mysql_connect("server", "username", "password");
    $db = mysql_select_db("your_database", $c);
    
    //TABLES FOR THE CONTENT & THE RATINGS (MODIFY IF TABLE NAMES ARE DIFFERENT)
    $content_table = 'content';
    $ratings_table = 'ratings';
    
    $ip = $_SERVER['REMOTE_ADDR'];
    
    ?>
    databas.sql
    Kod:
    CREATE TABLE `content` (
       `id` INT NOT NULL ,
       `content` TEXT NOT NULL
    ) ENGINE = MYISAM ;
    CREATE TABLE `ratings` (
       `i` INT NOT NULL PRIMARY KEY AUTO_INCREMENT , 
       `id` INT NOT NULL ,
       `rating` INT NOT NULL ,
       `ip` VARCHAR ( 50 ) NOT NULL ,
       `dateposted` VARCHAR ( 100 ) NOT NULL ,
       `timestamp` INT NOT NULL
    ) ENGINE = MYISAM ;
    INSERT INTO content VALUES ('1', 'Demo'), ('2', 'wcetdesigns.com');
    Jag bara ÄR ...... hur ska man annars vara?!?

  • #2
    Såg felet till slut. Finns en ifsats i rate.php som jag inte vet varför den finns:

    Den kollar om det finns något i databasen och om INGEn röstat på den valda posten så kommer den ju aldrig köra den. Så dolde den.

    Kod:
    //if($content_exists){
        if($nn){
            //CHECKS TO SEE IF THE RATING IS THE SAME AS BEFORE
            if($rating!=$r["rating"]){
                //IF SO, THE RATING WILL BE UPDATED
                mysql_query("UPDATE $ratings_table SET rating='$rating' WHERE id='$id' AND ip='$ip'");
            }
        } else {
            //ADDS THE NEW RATING, IF USER HAS NOT ALREADY RATING
            $range = range(1, 5);
            if(in_array($rating, $range)){
                mysql_query("INSERT INTO $ratings_table VALUES('', '$id', '$rating', '$ip', '$date', '$timestamp')");
            }
        }
    //}
    Jag bara ÄR ...... hur ska man annars vara?!?

    Kommentera

    Working...
    X