Jump to content

Need Help With PHP Coding


eRott

Recommended Posts

Hi, I didnt really know what to name this but I hope someone can help. While I do know some stuff about PHP, I am still learning.

I have seen many website with urls such as
http://www.mydomain.com/index.php?page=contactus
I am just wondering, how would I do something like this. I am looking to create something similar whereby it specifies a certain video for example.

So I am looking for something like:
http://www.mydomain.com/videos.php?id=rocket_launch

And if someone were to go to that link, it would display that video about a rocket. So basically every single video has an id and i could do a link thing like that. So for example I could have a bunch of different ones like:
http://www.mydomain.com/videos.php?id=rocket_launch
http://www.mydomain.com/videos.php?id=exploding
http://www.mydomain.com/videos.php?id=best_card_trick

Take YouTube for example. I am looking to do something similar to that. For example, one of their URLS to a video is:

http://youtube.com/watch?v=MBwEd_g62Hk







I am thinking simple, something more like having two simple files; vid_id.php and video.php (for example). Then, in the vid_ip.php file it would contain a list of all the videos, and have id's. For example, the numbers 0-5 are the id's, and for every id it tells the source of the video file.

[code]
[0] src="http://www.mydomain.com/videos/rocket.swf"
[1] src="http://www.mydomain.com/videos/balloon.swf"
[2] src="http://www.mydomain.com/videos/cake.swf"
[3] src="http://www.mydomain.com/videos/cheese.swf"
[4] src="http://www.mydomain.com/videos/apple.swf"
[5] src="http://www.mydomain.com/videos/cat.swf"
[/code]

Then, in video.php a simple script which would allow me to simply type in http://www.mydomain.com/video.php?id=3 in the browsers address bar and it would then bring me to the page which will show the video about cheese. Would it be possible to do something simple like this?




Thank you.
Best Regards,
Link to comment
Share on other sites

@ Jenk
I am going to be honest with you... I have NO idea on earth what that means  ;D.

@ Millar
That's about all I know about this. I've seen that type of code thing elsewhere, however, I have no idea how to write it. I mean, I do I assign the ids in the first place etc... I can read and understand and modify php code, however, I am completly clueless when it comes to actually writing it. Consider me an alien who just arrived on earth who is trying to learn php  ???.


By the way, wow. You guys certainly replied fast  :o
Link to comment
Share on other sites

The best way to do this would be to use a mysql database to contain your video information.

That way you can call http://www.mydomain.com/video.php?id=3 and query the mysql server for the video and show the proper video on video.php page.

If you need help with mysql let me know and I can give a brief explanation or you can search the [url=http://www.phpfreaks.com/tutorials.php]tutorials[/url] section of this website.  There are also plenty of resources out on the web for setting up a mysql database, tables and fields.  Just google it :)
Link to comment
Share on other sites

omg. The response time is like 3 seconds. Hell, I can't even look away before someone has already replied! This forum is amazing!  ;D. Anyway, back on topic, yes please. ANY and ALL help you can provide is more then appreciated. Also, using a mySQL database, how would I go abouts adding more information to it? Would I also need some sort of form that I enter information into then submit it and it writes to the database. I am at an even greater state of confusion when it comes to MySQL databases.
Link to comment
Share on other sites

okay let's say you have a table in your database called videos. it has 2 fields: one called video_name and one called video_id. each row in your table would have the video name and an id associated with it. example:

[code]
video_name  video_id
------------  -------
rocket            1
balloon          2
cheese            3
[/code]

so let's make an example script, using this data. 

videos.php
[code]
<?php
  //connect to and select db
  $conn = mysql_connect('localhost','username','password') or die(mysql_error());
  $db = mysql_select_db('dbname',$conn) or die(mysql_error());
 
  //get a list of the info from the table to make the linkies
  $sql = "select * from videos";
  $result = mysql_query($sql, $conn) or die(mysql_error());

  // for each row fetched from the results... 
  while ($list = mysql_fetch_array($result)) {
      //make the custom linkie
      echo "<a href= 'http://www.mydomain.com/videos.php?id={$list['video_id']}'>{$list['video_name']}</a><br>";
  } // end while

  // if the user clicked on a linkie and therefore an id exists (the is_numeric is a basic security precaution) ...
  if (is_numeric($_GET['id'])) {
      // for easier var syntax handling
      $id = $_GET['id'];
     
      // select the video from the db based on its id     
      $sql = "select video_name from videos where video_id = '$id'";
      $result = mysql_query($sql, $conn) or die(mysql_error());

      // if a video was found...
      $videofound = mysql_num_rows($result);
      if ($videofound > 0) {
        $video = mysql_fetch_array($result);
        // insert standard video/swf imbedding code here, using $video as the name
      } // end if found
  } // end if there was a linkie clicked
?>
[/code]     

this is not the most efficient way of doing this. first off, it entirely depends on how you want things to look and feel on your website, and how the flow of the rest of your site is.  you will probably have your db connection in a diff file of course, and you may or may not want the code to be split up into different files, for instance.  But this example will point you in the right direction.

Link to comment
Share on other sites

Yes, that was an excellent explaination. Thank you very much. That has helped a lot, i think ;D. I had been doing some looking around and this is what I have so far:

config.php
[code]
<?php
$dbhost = 'localhost';
$dbuser = 'insert_username';
$dbpass = 'insert_password';
$dbname = 'insert_database';
?>
[/code]


opendb.php
[code]
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
[/code]



closedb.php
[code]
<?php
mysql_close($conn);
?>
[/code]



create_database.php
[code]
<?php
include 'config.php';
include 'opendb.php';

$query = 'CREATE TABLE .....( '.
        'video_id INT NOT NULL AUTO_INCREMENT, '.
        'video_name VARCHAR(50) NOT NULL, '.
        'PRIMARY KEY(video_id))';

$result = mysql_query($query);

include 'closedb.php';
?>
[/code]


Now I just have to make it all work. Although, this part of the code you had written:


[code]
      // if a video was found...
      $videofound = mysql_num_rows($result);
      if ($videofound > 0) {
        $video = mysql_fetch_array($result);
        // insert standard video/swf imbedding code here, using $video as the name
      } // end if found
  } // end if there was a linkie clicked
[/code]

When you say, "insert standard video/swf embedding code here, using $video as the name" what exactly do you mean?

See, I have a game and video website, and am looking for a better way to show videos. Currently, for the videos I just have a page for the category and all the videos are there. So for example, all my funny videos, I have one page where there is a drop down list that users select a video from and click play then the videos shows in a frame. But why I want to do it with the whole id thing, is so people can access an individual video directly, instead of having to go to funny.php and selecting it from the drop down list. Here is the code of funny.php:

sorry, keeps saying the server reset the connection while trying to load the page, so just use this link:
http://www.erott.retect.com/videos/funny.txt
Link to comment
Share on other sites

by standard video/swf embedding code, i mean the normal < object / embed > html tags.
[code]
$showmovie = <<<MOVIE
<object width='550' height='400'>
<param name='movie' value='$video'>
<embed src='$video' width='550' height='400'>
</embed>
</object>
MOVIE;

echo $showmovie;
[/code]


as far as a "better" way to do it... well this whole id thing really isn't all that "better" than your current dropdown list, unless you are hardcoding your dropdown list.  In which case, you could pull the info from the db as shown above, and populate the dropdown list in the while loop, instead of making links.  In the end, your current way vs. this way kinda just boils down to preference, rather than "better"...

i suppose if you wanted to give the user the ability to bookmark a direct link instead of having to go to the page and select evertime, this new method would be better.
Link to comment
Share on other sites

Oh ok. So just add it there... gotcha. If you mean by "hardcoding your dropdown list", manually adding all the videos, yes, that is currently what I do. While I suppose I could pull of the information from a database like you said, the main purpose of this is to allow users to directly access a video. Instead of that drop down list. Basically its just like me saying to you...

"Hey I found this funny video, go here, and choose "Blah Blah" from the drop down list then click play!"

Rather, I would like to be able to do...

"Hey I found this funny video, go here!"

You know what I mean?

... Ok, that script thing I was working on worked.. who knew  ;D. Ok, so now I have a database called ".....", inside that i have a table called "....." and inside that I have 2 fields, "video_id" and "video_name". Correct so far? Now, how do i actually go abouts adding videos to the database?

Also, that code you wrote, I not too sure I follow what exactly it does. Please, let me know which option, if any is the correct understanding of what it does:

a) It searches the database, finds all/any videos and lists them all on the page
b) It searches the database, finds only one video and plays it
c) It searches the database, finds all/any videos and lists them. Then, when a link is clicked, it plays it
Link to comment
Share on other sites

Just realized something.

[code]
$showmovie = <<<MOVIE
<object width='550' height='400'>
<param name='movie' value='$video'>
<embed src='$video' width='550' height='400'>
</embed>
</object>
MOVIE;

echo $showmovie;
[/code]

This is saying it can find the value stored in '$video' but where is that value of $video stored? In your code it says that the value of $video is

[code]
$video = mysql_fetch_array($result);
[/code]

which is just the results of searching the database. But there is no actual value, (eg: http://www.mydomain.com/videos/cheese.swf) for the variable $video.

i am so lost.
Link to comment
Share on other sites

Ok, after some working, and editing, and reading I have finally got it all figured out, except for one small thing. It is not displaying any video when I click the name. Ok, so I open up "http://www.erott.retect.com/lib/videos.php", the page displays the video name "Terrance" and when I click on that, it brings me to "http://www.erott.retect.com/lib/videos.php?id=1" Seems to be working fine. Except now it just lists the videos again, it doesnt play the video.

I've done some more work, I have created a simple form that allows me to enter the video name and video source into the database. It works fine.

So in my table I now have 3 fields:
video_id
video_name
video_src

I used the form, and added a video, now it shows this:
video_id: 1
video_name: Terrance
video_src: http://www.erott.retect.com/videos/violent/Terrance.swf

Seems to be working fine. So, I am at this point stuck. I am looking to be able to click on that video, and the video being to play, (on a new page though). By that, I mean just a page with the video on it, not all the links.

So "http://www.erott.retect.com/lib/videos.php" would list all the videos, then if you were to click on a video, take for example the "Terrance" video, it would bring you to "http://www.erott.retect.com/lib/videos.php?id=1" which is a page that JUST shows the video.

Thank you for all your help so far. I really appreciate it even though im sure it seems as hard as trying to teach an apple how to write  ;D.
Link to comment
Share on other sites

oops. my bad. it should look like this:

[code]
<?php
.
.
.

      // select the video info from the db based on its id     
      $sql = "select * from videos where video_id = '$id'";
      $result = mysql_query($sql, $conn) or die(mysql_error());

    // if a video was found...
      $videofound = mysql_num_rows($result);
      if ($videofound > 0) {
        // make an array of the row.
        $videoinfo = mysql_fetch_array($result);
        // make a specific variable for the element in the array.
        // you can simply use the array with the element name, but
        // i do it like this for clarity in code syntax.
        $video = $videoinfo['video_name'];
       
        // make a variable with the movie object html code
        $showmovie = <<<MOVIE
            <object width='550' height='400'>
              <param name='movie' value='$video'>
              <embed src='$video' width='550' height='400'></embed>
            </object>
MOVIE;
// note: ^ MOVIE; must NOT be indented!
            // echo the movie
            echo $showmovie;
      } // end if found
?>
[/code]

as far as the "source" is concerned: if you look at your fields, you are holding redundant data by doing that.  you should either a) have the video_name with the video's name all by itself, and the video_src with only the path (but no filename) or else b) get rid of the video_src field altogether and just have the full path name included in the video_name field.  choosing option a) will make your database the most flexible, but if you're going for most flexible, then what you should do is store the relative path of the file, not the full url. 

as far as retrieving and using it, simply change the sql query from "select video_name..." to "select * ..." as shown above.  then you could grab it the same way as i changed the rest of the code above, after the fetch_array statement.  the fetch_array fetches the info from the database and puts it in an array. you can access the info by specifying the field's name, in the array element tags

['video_name']  ['video_src']

in the code above, i set $video to one of those:

$video = $videoinfo['video_name'];

you could simply do the same thing for the source:

$source = $videoinfo['video_src'];

and then use $source in your param and embed tags, same as your $video.
Link to comment
Share on other sites

OOHH, i get it. When you say 'video_name' you mean the name of the video file, as in terrance.swf or something. When I say 'video_name' I am referring to the title of the video, and 'video_src' is terrance.swf.

Oh, one thing, you forgot one: [b] } [/b] at the end of the code. As well, is there a way to make it show up on a new page, not the same one?

[b]Edit:[/b] If you go here (it works): http://www.erott.retect.com/lib/videos.php you will notice everything is working fine, however when you click on the video, the video begins to play however the link still shows. Which is why I want it to open in a new page if possible. Same window, but new page.

Same as html if you use "a href".. It will take you to a new page in the same window compared to if you use the [target="_blank"] which will open up a new window.
Link to comment
Share on other sites

[quote]
Edit: If you go here (it works): http://www.erott.retect.com/lib/videos.php you will notice everything is working fine, however when you click on the video, the video begins to play however the link still shows. Which is why I want it to open in a new page if possible. Same window, but new page.

Same as html if you use "a href".. It will take you to a new page in the same window compared to if you use the [target="_blank"] which will open up a new window.
[/quote]

I am looking to have it in a different page without the links because i want to do something that looks like:

__________________________________________
| Videos >> Funny >> (Name of Video)              |
| Author: (authors name)                                |
| Description: (some description here)                |
|                                                                |
|  ______________________________________  |
|  |                                                          |  |
|  |                                                          |  |
|  |                VIDEO SHOWS HERE              |  |
|  |                                                          |  |
|  |                                                          |  |
|  |____________________________________|  |
|              some disclaimer stuff here                |
-------------------------------------------------

And if the links with ALL those video's are showing, it will look a little messy.

[b]Edit:[/b] Got it to work. Rather quite simple actually. Thank you for all the help. Really appreciate it.

Oh, in case you are wondering, I simply split the code into 2 different files; video.php and videos.php; and had it show in videos.php

Thank you again.
Best Regards,
eRott
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.