Jump to content

RadioDJ Request Page (Random Song selecter)


Recommended Posts

Now RadioDJ has website requests built into it I thought I would share this code.

 

if anyone is using the RadioDJ demo website template for taking listener requests. I have a modified version of the request.php page which incorporates a random song selector.

 

You can see a demo of it in action here http://djgarybaldy.co.uk/requests/

 

I made this random song selector script years ago for S** and found it works just as well with the new RDJ web template.

 

The Demo template for RadioDJ can be found here http://www.radiodj.ro/download/scripts/RadioDJDemoScriptV2.2.zip

 

All you need to do is replace the code inside request.php with this piece of code.

 

/* ============================================================================= */
// EDIT BELOW
/* ============================================================================= */

$pageTitle     = "Song Requests";            //Page title
$limit         = 30;                         //How many upcoming tracks to display?
$targetpage    = $_SERVER['SCRIPT_NAME'];     //Link to this page
$reqLimit      = 10;                         //Limit number of requests per IP

//messages:
define("ERROR_TRACKID", "Please select a track in order to send the request!
Go Back");
define("ERROR_USERNAME", "Please enter your name in order to send the request!
Go Back");
define("ERROR_TRACKREQ", "The selected track is already requested.
Please try again later, or select another track!");
define("ERROR_LIMITREACHED", "Sorry, but you've reached the request limit for one day.");
define("ERROR_UNKNOWN", "Unknown error occurred! Please try again...");

define("MSG_REQSUCCESS", "Your request was successfully placed!");
define("MSG_NORESULTS", "No results to display...");

define("REQ_DESCRIPTION", "Please enter request details below");
define("REQ_NAME", "Your Name:");
define("REQ_MESSAGE", "Message (Optional):");
define("REQ_BUTTON", "Submit Your Request");

define("NAV_NEXT", "NEXT");
define("NAV_PREV", "PREVIOUS");

define("SEARCH_TXT", "Search artist or title:");
define("SEARCH_BUTTON", "Search");

define("COL_ARTIST", "Artist");
define("COL_TITLE", "Title");
define("COL_DURATION", "Duration");
define("COL_REQ", "Req");
define("ALT_REQ", "Request this track");

/* ============================================================================= */
// END EDIT
/* ============================================================================= */

require_once('serv_inc.php');
require_once('header.php');

date_default_timezone_set($def_timezone);

//============  FUNCTIONS ==============//

//build duration string from seconds
function convertTime($seconds) {
   $sec = $seconds;
   // Time conversion
   $hours = intval(intval($sec) / 3600);
   $padHours = True;
   $hms = ($padHours)
       ? str_pad($hours, 2, "0", STR_PAD_LEFT). ':'
       : $hours. ':';
   $minutes = intval(($sec / 60) % 60);
   $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';
       $seconds = intval($sec % 60);
   $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);

   return $hms;
}

function getRealIpAddr() {
   if (!empty($_SERVER['HTTP_CLIENT_IP'])){
     //check ip from share internet
     $ip=$_SERVER['HTTP_CLIENT_IP'];
   }
   elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
     //to check ip is pass from proxy
     $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
   } else {
     $ip=$_SERVER['REMOTE_ADDR'];
   }
   return $ip;
}

//============ END FUNCTIONS ==============//

$srch = "";        //search term value holder
$srchpath = ""; //search path holder
$srcquery = ""; //search query holder
$stages = 3;     //how to split the pagination
$page = 1;         //default page
$reqid = "";

if(isset($_GET['searchterm'])){
   if($_GET['searchterm'] != "") {
   
       $srch = mysql_escape_string($_GET['searchterm']);
       $srchpath = "&searchterm=$srch";
       $srcquery = "AND (`artist` LIKE '%$srch%' OR `title` LIKE '%$srch%')"; //Search artist and title
   }
}

//Get the page if it's requested
if(isset($_GET['page'])){
   $page = mysql_escape_string($_GET['page']);
}

if($page){
   $start = ($page - 1) * $limit;
}else{
   $start = 0;
}

if(isset($_POST['reqsubmit'])){

   /*
   ERROR CODES:
   0 = No error
   1 = no user name
   2 = no requested track
   3 = track already in queue
   4 = request limit reached
   */

   $reqname = mysql_escape_string($_POST['requsername']);
   $reqmsg = mysql_escape_string($_POST['reqmessage']);
   $reqsongID = mysql_escape_string($_POST['songID']);
   $reqIP = getRealIpAddr();
   
   $error = 0;
   $reccount = 0;
       
   if(!$reqname){$error = 1;}
   if(!$reqsongID){$error = 2;}
   
   if($error == 0){
   
       db_conn();
       
       //track is already requested?
       $recheck = "SELECT COUNT(*) AS num FROM `requests` WHERE `songID`='$reqsongID' AND `played`=0;";
       $total_req = mysql_fetch_array(mysql_query($recheck));
       
       if($total_req['num'] > 0){
           $error = 3;
       }
       
       @mysql_free_result($total_req);
       
       if($error == 0){
           //user has reached the request limit?
           $recheck = "SELECT COUNT(*) AS num FROM `requests` WHERE `userIP`='$reqIP' AND DATE(`requested`) = DATE(NOW());";
           $total_req = mysql_fetch_array(mysql_query($recheck));
           
           if($total_req['num'] >= $reqLimit){
               $error = 4;
               $reccount = $total_req['num'];
           }
           
           @mysql_free_result($total_req);
           db_close($opened_db);
       }
       
   }
   
   switch ($error) {
       case 0:
           db_conn();
           
           $queryx = "INSERT INTO `requests` SET `songID`='$reqsongID', `username`='$reqname', `userIP`='$reqIP', `message`='$reqmsg', `requested`=now();";
           $resultx = mysql_query($queryx);
               
           if($resultx > 0) {
               echo "" . MSG_REQSUCCESS . "
";
           } else {
               echo "" . ERROR_UNKNOWN . "
";
           }
               
           @mysql_free_result($resultx);
           db_close($opened_db);
           
           break;
       case 1:
           echo "" . ERROR_USERNAME . "
";
           break;
       case 2:
           echo "" . ERROR_TRACKID . "
";
           break;
       case 3:
           echo "" . ERROR_TRACKREQ . "
";
           break;
       case 4:
           echo "" . ERROR_LIMITREACHED . " (" . $reccount . "/" . $reqLimit . ")" . "
";
           break;
   }
   
   
   $reqid = "";
}

//Get the page if it's requested
if(isset($_GET['requestid'])){

   if($_GET['requestid'] != "") {
       
           $reqid = mysql_escape_string($_GET['requestid']);
       
           echo "\n";
           echo "    \n";
           echo "            \n";
           echo "                \n";
           echo "                    
" . REQ_DESCRIPTION . "\n";
           echo "                \n";
           echo "                \n";
           echo "                    " . REQ_NAME . "\n";
           echo "                    \n";
           echo "                \n";
           echo "                \n";
           echo "                    " . REQ_MESSAGE . "\n";
           echo "                    \n";
           echo "                \n";
           echo "                \n";
           echo "                    \n";
           echo "                \n";
           echo "            \n";
           echo "            \n";
           echo "        \n";
           echo "    \n";
   }
}

//================//

if($reqid == ""){
   db_conn();

   //Get the number of items
   $query = "SELECT COUNT(*) as num FROM `songs` WHERE `enabled`='1' $srcquery AND `song_type`=0";
   $total_pages = mysql_fetch_array(mysql_query($query));
   $total_pages = $total_pages['num'];

   //Get page data
$query1 = "SELECT `ID`, `artist`, `title`, `duration`, `date_played`, `artist_played` FROM `songs` WHERE `enabled`='1' $srcquery AND`song_type`=0 ORDER BY `artist` ASC LIMIT $start, $limit";
$result = mysql_query($query1);
   if(isset($_GET['req'])){
   $req = mysql_escape_string($_GET['req']);
}
if ($req == 'req') {
$query = "select * from songs  WHERE `enabled`='1' AND `song_type`=0 ORDER BY RAND() LIMIT 0,20";
$result = mysql_query($query);
}

       
   // Initial page num setup
   if ($page == 0){$page = 1;}
   $prev = $page - 1;
   $next = $page + 1;
   $lastpage = ceil($total_pages/$limit);
   $LastPagem1 = $lastpage - 1;

   $paginate = '';

   if($lastpage > 1) {    
       $paginate .= "";
       // Previous
       if ($page > 1){
           $paginate.= "" . NAV_NEXT . "";
       }else{
           $paginate.= "" . NAV_PREV . "";
       }
       
       // Pages
   
       if ($lastpage             for ($counter = 1; $counter                 if ($counter == $page){
                   $paginate.= "$counter";
               }else{
                   $paginate.= "$counter";
               }                    
           }
       } elseif($lastpage > 5 + ($stages * 2))    {
       
       // Beginning only hide later pages
           if($page                 for ($counter = 1; $counter                     if ($counter == $page){
                       $paginate.= "$counter";
                   }else{
                       $paginate.= "$counter";
                   }                    
               }
           
               $paginate.= "...";
               $paginate.= "$LastPagem1";
               $paginate.= "$lastpage";
               
           } elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2)){
           
               $paginate.= "1";
               $paginate.= "2";
               $paginate.= "...";
               
               for ($counter = $page - $stages; $counter                     if ($counter == $page){
                       $paginate.= "$counter";
                   }else{
                       $paginate.= "$counter";
                   }
               }
               
               $paginate.= "...";
               $paginate.= "$LastPagem1";
               $paginate.= "$lastpage";

           } else {
           
               $paginate.= "1";
               $paginate.= "2";
               $paginate.= "...";
               
               for ($counter = $lastpage - (2 + ($stages * 2)); $counter                     if ($counter == $page){
                       $paginate.= "$counter";
                   }else{
                       $paginate.= "$counter";
                   }
               }
           }
       }
                   
       // Next
       if ($page             $paginate.= "" . NAV_NEXT . "";
       }else{
           $paginate.= "" . NAV_NEXT . "";
       }    
       $paginate.= "";
   }

   //Search box
   echo '';
   echo "";
   echo SEARCH_TXT . "  ";
echo "";
echo '
';
echo '';
echo '';

   if($total_pages > 0){

       //Add the pagination
       echo "$paginate
";

       //Results table
       echo '</pre><table class="main_table" border="0" cellspacing="0" cellpadding="5">';
       echo " " . "\n";
       echo "   #\n";
       echo "   " . COL_ARTIST . "\n";
       echo "   " . COL_TITLE . "\n";
       echo "   " . COL_DURATION . "\n";
       echo "   " . COL_REQ . "\n";
       echo " " . "\n";

       $cnt = 1+($limit*$page)-$limit; //Results counter

       //Add results to the table
       while($row = mysql_fetch_assoc($result)) {
           echo " " . "\n";
           echo "  $cnt.\n";
           echo "  " . $row['artist'] . "\n";
           echo "  " . $row['title'] . "\n";
           echo "  " . convertTime($row['duration']) . "\n";

           if(track_can_play($row['date_played'], $row['artist_played']) == true) {
               echo "  
               \"images/add.png\"\n";
           }else{
               echo "  \"images/delete.png\"\n";
           }
           
           echo " " . "\n";
           $cnt++;
       }
       
       @mysql_free_result($result);
       db_close($opened_db);
?>

</table><br><br><br><br><br>       //Add the bottom pagination<br>       echo '<div align="center">' . $paginate . '</div>';<br>   }else{<br>       echo "<div class='\"errordiv\"'>" . MSG_NORESULTS . "</div>";<br>   }<br>}<br><br

 

If all goes to plan your request page should now have a random song selector on it.

 

I'm ttrying to get it working with the RDJ Wordpress plugin although that is proving a bit more tricky to implement.

 

I hope someone finds this useful....

Edited by djgary72

My Blog https://djgarybaldy.blogspot.com

User of RadioDJ FREE radio playout software since 2010.

How to Install RadioDJ: https://djgarybaldy.blogspot.com/2020/08/how-to-install-radiodj-free-radio.html

RadioDJ is my most FAVOURITE piece of software EVER

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...