Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Playing random URLs of videos
Old 05-29-2010, 11:07 PM Playing random URLs of videos
Skilled Talker

Posts: 53
Trades: 0
Ok so im trying to play videos (stored in a folder on my server) at random, I have the URLs of the videos in a mysql database. I'm a newbie with PHP and have been doing alot of research on how I could go about doing this but still cant get this to work. This is the script I've been working on thus far.
PHP Code:
<?php 
mysql_connect 
("localhost""username""password") or die ("failed to connect to server");
mysql_select_db("database");
$result mysql_query("SELECT url FROM vidsdb ORDER BY RAND() LIMIT 1");
$row mysql_fetch_row($result);
$url $row[0];
$title $row[1];
echo 
"<a href=\"$url\">$title</a>";
?>
If anyone has any ideas as to how I should go about with this, please do let me know.

Last edited by chrishirst; 05-30-2010 at 05:20 AM..
worldy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-30-2010, 05:23 AM Re: Playing random URLs of videos
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
The "ORDER BY" has to be a column in the table and you can only arrange it in ascending (ASC) or descening (DESC) order.

return the results as an array then select a random index from the array to display.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-30-2010, 07:16 AM Re: Playing random URLs of videos
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
False. "order by rand()" is a valid construct, though it is incredibly inefficient. The problem here is that topicstarter selects only one field but assigns two. The fix should probably be "select url,title from ...".
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 05-30-2010, 07:22 AM Re: Playing random URLs of videos
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I take it that's a MySQL "special" then

Well that's todays new thing learnt!! Cheers.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-30-2010, 01:56 PM Re: Playing random URLs of videos
Skilled Talker

Posts: 53
Trades: 0
Thanks for the input guys but all the technical terms have me confused, I'm very new to php and mysql. I've been learning the basics like retreiving data from tables but anything beyond that is currently beyond my knowledge.
worldy is offline
Reply With Quote
View Public Profile
 
Old 05-31-2010, 02:01 PM Re: Playing random URLs of videos
Marik's Avatar
Skilled Talker

Posts: 99
Trades: 0
Ok,

Step 1:

I don't know what player you are using to play the vidoes, in the script I wrote below I am using JW Player. It plays files that are in the following formats:

FLV, MP4, MP3

If your videos are in a different format use a free converter like WINFF to convert your videos to any of the above formats (except MP3 that's only for streaming audio).

So go ahead and download JW player from the above link, unzip the file and upload the folder called "mediaplayer" to the root of your server.


Step 2:

In the database you should only have the url path of the video files. For example:

http://www.mysite.com/path/to/videofile

That's it, nothing else - just the absolute url path to the file. In the script below I am assuming this is how your database structure looks:

Code:
id | video_title | video_path
----------------------------
1  | Title One  | http://www.mysite.com/path/to/videofile1

2  | Title Two  | http://www.mysite.com/path/to/videofile2

etc..
I am also assuming your database is called "videos" and the table inside that database is called "my_videos".

If you want to throw away that database you were using and just go with the example one I made then create a new database called "videos", go inside it and just copy and paste the below code in the sql tab of PHPmyadmin or type it in mysql monitor (if that's what you are using):


Code:
CREATE TABLE IF NOT EXISTS `my_videos` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `video_title` varchar(1000) NOT NULL,
  `video_path` varchar(1000) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
Next enter your video path urls under the "video_path" column, you can also add video titles under the "video_title" column, you don't have to enter anything under the "id" column - that will populate automatically.

Step 3:

Here's the PHP script to play videos at random inside JW player:

PHP Code:

<?php

    
// connect to db
    
mysql_connect("localhost""username""password") or die ("Could not establish database connection.");
    
mysql_select_db("videos") or die ("Could not select database.");

    
// grab a random video to play
    
$query mysql_query("SELECT * FROM my_videos ORDER BY rand() LIMIT 1");
    while (
$row mysql_fetch_assoc($query)) {
        
$videoTitle $row['video_title'];
        
$video $row['video_path'];
    }

?>

<?php echo "Now Playing: $videoTitle <hr />"?>

<script type="text/javascript" src="mediaplayer/swfobject.js"></script>
<div id="1">If you can read this either you have JavaScript turned off or you do not have <a href="http://get.adobe.com/flashplayer/">Flash Player</a> installed.</div>
<script type="text/javascript">
var so = new SWFObject('mediaplayer/player.swf','mpl','610','330','9');
so.addParam('allowscriptaccess','always');
so.addParam('allowfullscreen','true');
so.addParam('flashvars','&file=<?php echo $video?>');
so.write('1');
</script>
Notes:

I know you are streaming your own videos, but if you want you can also stream youtube videos inside JW player. Just enter the youtube url of the video you want to stream like you did with the video file paths in the database. As long as that video doesn't have embedding disabled it will work like any other stream. Just remember to use youtube urls like these:

http://www.youtube.com/watch?v=x4c_wI6kQyE

and not like:

http://www.youtube.com/watch?v=x4c_w...ture=topvideos

- Good luck

Last edited by Marik; 05-31-2010 at 02:19 PM..
Marik is offline
Reply With Quote
View Public Profile
 
Old 06-02-2010, 05:06 PM Re: Playing random URLs of videos
Skilled Talker

Posts: 53
Trades: 0
Wow thank you so much Marik. It works!!
worldy is offline
Reply With Quote
View Public Profile
 
Old 06-03-2010, 06:45 PM Re: Playing random URLs of videos
Junior Talker

Posts: 4
Trades: 0
you can also do this using PHP's rand function combined with a switch statement
__________________

Please login or register to view this content. Registration is FREE
goodboy2 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Playing random URLs of videos
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.34438 seconds with 12 queries