1

Тема: Поможите ! Рейтинг для песен PHP (оптимизировать)

Уважаемые форумчане !

Не являясь большим докой в области РНР программирования, очень прошу помощи в подгонке, и разъяснении установки ниже приложенного кода к SAM Broadcaster 4.9.0 . Просмотрев дирректорию PHP программы, увидел, что структуры файлов расходятся координально.

Спасибо.

<?

##############################################################################
#                                                                               #
#  This script was written by Paul MacArthur: http://www.celticradio.net     #
#  You are allowed to use this script freely on your website, but it is not  #
#  to be sold or distributed to any other broadcaster, website, person or    #
#  entity. To aquire the most recent version of this script, please visit:   #
#                                                                            #
#  http://www.webcelt.com/news/archives/category/internet-radio              #
#                                                                            #
#  In addition, please do not remove the credits and website URL provided in #
#  this section. Would very much appreciate that!                            #
#                                                                            #
#  Suggestions, comments and feedback: webmaster@celticradio.net             #
#                                                                            #
# -------------------------------------------------------------------------- #
#                                                                            #
#                        PHP SAM COOKIE RATING SCRIPT                        #
#                        ----------------------------                        #
#                                                                            #
#  The purpose of this script is to allow for a cookie based rating system   #
#  that works in conjunction with the PHP template provided via the SAM      #
#  broadcaster by Spacial Audio. It has been tested with SAM V3 and above.   #
#                                                                            #
#  Listeners can rate a song once every 24 hours and only when that song is  #
#  playing on your broadcast. Some broadcasters have chosen to only allow    #
#  one rating per song, but we believe that giving our listeners the         #
#  ability to rate a song multiple times over time will create a             #
#  better broadcast by playing the songs listeners want to hear.             #
#                                                                            #
#  Not only does this script provide a rating system, but it also adds       #
#  weight to a song that is rated based on the following formula:            #
#                                                                            #
#  Rating of 5 add 5 to the weight.                                          #
#  Rating of 4 add 3 to the weight.                                          #
#  Rating of 2.5 add 0 to the weight.                                        #
#  Rating of 1 add -3 to the weight.                                         #
#  Rating of 0 add -5 to the weight.                                         #
#                                                                            #
#  Additionally, your listeners will be able to see the current rating and   #
#  weight of the song upon a successful rating via a pop-up web page.        #
#                                                                            #
#  Because of buffering issues with web radio, sometimes the song playing    #
#  will not match what shows on your website - it could be off by 5 seconds  #
#  or 20 seconds. Therefore, we have built in a check that prevents rating   #
#  a song that does not match what is currently playing on your broadcast.   #
#                                                                            #
#  INSTRUCTIONS:                                                             #
#                                                                            #
#  Place this script in with the other PHP template files provided by SAM.   #
#  View the CopyCode.txt file to easily grab the code. Copy all graphic      #
#  files to appropriate locations.                                           #
#                                                                             #
#  Add the following javascript code to your header file (header.php):       #
#                                                                             #
#  <script LANGUAGE="JavaScript" type="text/javascript">                     #
#                                                                             #
#   function ratesong(rating, songid)                                        #
#          {                                                                     #
#          var ratesong;                                                         #
#          if (ratesong) {                                                         #
#          ratesong++;                                                             #
#          }                                                                     #
#          else {                                                                 #
#          ratesong=0;                                                          #
#          }                                                                     #
#          ratesong = window.open("./ratesong.php?rating="+rating+"&checkid="   #
#        +songid, "ratesongwin", "location=no,status=no,menubar=no,scrollbars #
#         =no,resizeable=yes,height=305,width=290");                           #
#          }                                                                     #
#    </script>                                                                #
#                                                                             #
#    The following HTML snipets should be placed on the page that your        #
#    listeners will actually rate the song. I have also provided the HTML     #
#    code to display the current rating of a song using the include star.gif  #
#    graphic set.                                                             #
#                                                                             #
#    RATING FORM HTML:                                                         #
#                                                                             #
#    <? $songid = $song['songid']; ?>                                         #
#    <form name="rateform" >                                                  #
#    <select style="font-family: Verdana, Arial, Helvetica, sans-serif;       #
#    font-size: 10px; line-height: 150%; " name = "rate"                      #
#    onchange="if ((rate.value>=0) && (rate.value<6)) {ratesong(rate.value,   #
#    <?echo $songid; ?>); rate.disabled=true; }">                             #
#    <option value = "9" selected>Rate this song!                             #
#    <option value = "5">Love it!                                             #
#    <option value = "4">Like it                                                 #
#    <option value = "2.5">It's OK                                              #
#    <option value = "1">Don't Like it                                         #
#    <option value = "0">Dump It!                                             #
#    <option value = "9">Cancel                                                 #
#    </select>                                                                 #
#    </form>                                                                     #
#                                                                             #
#    DISPLAY RATING HTML:                                                     #
#                                                                             #
#    <span><b>Rating:&nbsp;</b>                                                 #
#        <?                                                                     #
#        if ($song['rating'] == 0) {                                             #
#            $rating_image = "stars-0-0.gif";                                 #
#        }                                                                     #
#                                                                             #
#        if (($song['rating'] > 0) && ($song['rating'] < 1)) {                 #
#            $rating_image = "stars-0-5.gif";                                 #
#        }                                                                     #
#                                                                                #
#        if (($song['rating'] >= 1) && ($song['rating'] < 1.5)) {             #
#            $rating_image = "stars-1-0.gif";                                 #
#        }                                                                     #
#                                                                             #
#        if (($song['rating'] >= 1.5) && ($song['rating'] < 2)) {             #
#            $rating_image = "stars-1-5.gif";                                 #
#        }                                                                     #
#                                                                             #
#        if (($song['rating'] >= 2) && ($song['rating'] < 2.5)) {             #
#            $rating_image = "stars-2-0.gif";                                 #
#        }                                                                     #
#                                                                             #
#        if (($song['rating'] >= 2.5) && ($song['rating'] < 3)) {             #
#            $rating_image = "stars-2-5.gif";                                 #
#        }                                                                     #
#                                                                             #
#        if (($song['rating'] >= 3) && ($song['rating'] < 3.5)) {             #
#            $rating_image = "stars-3-0.gif";                                 #
#        }                                                                     #
#                                                                             #
#        if (($song['rating'] >= 3.5) && ($song['rating'] < 4)) {             #
#            $rating_image = "stars-3-5.gif";                                 #
#        }                                                                     #
#                                                                             #
#        if (($song['rating'] >= 4) && ($song['rating'] < 4.5)) {             #
#            $rating_image = "stars-4-0.gif";                                 #
#        }                                                                    #
#                                                                            #
#        if (($song['rating'] >= 4.5) && ($song['rating'] < 5)) {             #
#            $rating_image = "stars-4-5.gif";                                 #
#        }                                                                    #
#                                                                            #
#        if ($song['rating'] >=5 ) {                                          #
#            $rating_image = "stars-5-0.gif";                                 #
#        }                                                                    #
#    ?>                                                                       #
#    <img src="<? echo "$rating_image"; ?>"  width="64" height="12"/>         #
#    </span>                                                                  #
#                                                                            #
#                                                                            #
#    CHANGES TO PHP TEMPLATE:                                                 #
#                                                                            #
#    The only changes required to the PHP template system provided by         #
#   Spacial Audio is the FUNCTIONS.PHP file located in common directory:     #
#                                                                            #
#    Add the following to the definitions section:                            #
#                                                                            #
#    Def($song["rating"],$song["rating"]);                                    #
#     Def($song["num_ratings"],$song["num_ratings"]);                          #
#                                                                              #
#     Included with this script are the stars.gif graphic set to display       #
#    stars on your website and also a graphic file to display "na.gif" when   #
#    there is no album cover available.                                       #
#                                                                              #
#     Please note that on CelticRadio.net we use a thumbnail version(100x100)  #
#    to display a smaller version of the album cover in the rating script.    #
#    You may need to adjust the pop-up size of the webpage to adjust to the   #
#    standard size you use for displaying album covers.                       #
#                                                                              #
#     Any questions at all on installing this script, ways to make it better   #
#     or just to say "Hello", give me a shout at webmaster@celticradio.net.    #
#                                                                            #
#                                                                            #
#                                                                            #
#                                                                              #
#                                                                            #
##############################################################################


require("config.php");

$db->open("SELECT songlist.*, historylist.listeners as listeners, historylist.requestID as requestID, historylist.date_played as starttime FROM historylist,songlist WHERE (historylist.songID = songlist.ID) AND (songlist.songtype='S') ORDER BY historylist.date_played DESC LIMIT 6");
$history = $db->rows();
reset($history);

$db->open("SELECT songlist.*, queuelist.requestID as requestID FROM queuelist, songlist WHERE (queuelist.songID = songlist.ID)  AND (songlist.songtype='S') AND (songlist.artist <> '') ORDER BY queuelist.sortID ASC LIMIT 4");
$queue = $db->rows();
reset($queue);

list($key, $song) = each($history);
$listeners = $song["listeners"];

PrepareSong($song);
$rating = urlencode($_GET['rating']);
$checkid = urlencode($_GET['checkid']);

if ($rating == 5) $weight_add = 5;
if ($rating == 4) $weight_add = 3;
if ($rating == 2.5) $weight_add = 0;
if ($rating == 1) $weight_add = -3;
if ($rating == 0) $weight_add = -5;

$new_weight = $song["weight"] + $weight_add;
if ($new_weight > 100) $new_weight = 100;
if ($new_weight < 0) $new_weight = 1;

if (($new_weight > 0) && ($new_weight < 10)) $rotation = "No Rotation";
if (($new_weight > 9) && ($new_weight < 20)) $rotation = "Rare Rotation";
if (($new_weight > 19) && ($new_weight < 40)) $rotation = "Light Rotation";
if (($new_weight > 39) && ($new_weight < 60)) $rotation = "Medium Rotation";
if (($new_weight > 59) && ($new_weight < 80)) $rotation = "Heavy Rotation";
if (($new_weight > 79) && ($new_weight < 101)) $rotation = "Power Hit";


if (isset($_COOKIE[$song['songID']])) {
    $message = "<b>ERROR:</b> Sorry, only one rating<br>per song every 24 hours!";
    }
    else {
    if ($song['songID'] == $checkid) {
    $message = "Song successfuly weighted & rated!";
    if ($song['num_ratings'] == 0) {
        $query = "update songlist set rating = $rating, num_ratings = 1, total_rating = $rating, weight = $new_weight WHERE ID = ".$song['songID']."";
        $result = mysql_query($query);
        $song['num_ratings'] = 1;
        $song['rating'] = $rating;
        $rating = 0;
        setcookie($song['songID'], $song['title'], time()+86400); }
    else {
      $num_ratings = $song['num_ratings'];
      $num_ratings = $num_ratings + 1;
      $total_rating = $song['total_rating'] + $rating;
          $new_rating = $total_rating/$num_ratings;
        $song['num_ratings'] = $num_ratings;
        $query = "update songlist set rating = $new_rating, num_ratings = $num_ratings, total_rating = $total_rating, weight = $new_weight WHERE ID = ".$song['songID']."";
        $result = mysql_query($query);
        $song['rating'] = round($new_rating, 1);
        $new_rating = 0;
        setcookie($song['songID'], $song['title'], time()+86400);
    }
    } else {
        $song['title'] = "N/A";
        $song['artist'] = "N/A";
        $song['rating'] = 0;
        $song['picture_tb'] = "";
        $song['num_ratings'] = 0;
        $message = "<b>ERROR:</b> Rating failed. Next song is loading...";
        }
    }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Song Rating - <? echo $song['title']; ?></title>
<link rel="stylesheet" type="text/css" href="<?echo "$styleSheet"; ?>"/>
</head>
<body style="background-color:#e4e8dd">

<div id ='small' align='center'>
<img src='songrating.gif' />
<br>
<b>Artist:</b>&nbsp;<? echo $song['artist']; ?><br>
<b>Song:</b> <? echo $song['title']; ?><br><br>
<?


    if ($song['rating'] == 0) {
        $rating_image = "stars-0-0.gif";
    }

    if (($song['rating'] > 0) && ($song['rating'] < 1)) {
        $rating_image = "stars-0-5.gif";
    }

    if (($song['rating'] >= 1) && ($song['rating'] < 1.5)) {
        $rating_image = "stars-1-0.gif";
    }

    if (($song['rating'] >= 1.5) && ($song['rating'] < 2)) {
        $rating_image = "stars-1-5.gif";
    }

    if (($song['rating'] >= 2) && ($song['rating'] < 2.5)) {
        $rating_image = "stars-2-0.gif";
    }

    if (($song['rating'] >= 2.5) && ($song['rating'] < 3)) {
        $rating_image = "stars-2-5.gif";
    }

    if (($song['rating'] >= 3) && ($song['rating'] < 3.5)) {
        $rating_image = "stars-3-0.gif";
    }

    if (($song['rating'] >= 3.5) && ($song['rating'] < 4)) {
        $rating_image = "stars-3-5.gif";
    }

    if (($song['rating'] >= 4) && ($song['rating'] < 4.5)) {
        $rating_image = "stars-4-0.gif";
    }

    if (($song['rating'] >= 4.5) && ($song['rating'] < 5)) {
        $rating_image = "stars-4-5.gif";
    }

    if ($song['rating'] >=5 ) {
        $rating_image = "stars-5-0.gif";
    }

?>
<img src="<? if ($song['picture']) {echo $song['picture'];} else { echo "na.gif";} ?>" align="center" /><br>
<img src="<? echo "$images"; ?><? echo "$rating_image"; ?>" align="center" />
<br>
<? if ($song['num_ratings'] == 0) {echo "Not Rated Yet";} ?><br>
<b>Current Rating:</b> <? echo $song['rating']; ?>/5&nbsp;&nbsp;&nbsp;<b># of Ratings:</b> <? echo $song['num_ratings']; ?>
<br>
<b>Broadcast Weight:</b> <? echo $new_weight."% - ".$rotation; ?><br><br>
<? echo $message; ?>
</div>
</body>
</html>

Отредактировано wwc (22-08-2013 13:15:50)