Jump to content


Photo

Need Help With PHP Coding


  • Please log in to reply
15 replies to this topic

#1 eRott

eRott
  • Members
  • PipPipPip
  • Advanced Member
  • 206 posts
  • LocationToronto, ON

Posted 01 September 2006 - 04:47 PM

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....?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....d=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....d=rocket_launch
http://www.mydomain....hp?id=exploding
http://www.mydomain....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:









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.

[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"

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,

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 01 September 2006 - 04:49 PM

have read up on mod_rewrite :)

#3 Millar

Millar
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts
  • LocationSouthampton, UK.

Posted 01 September 2006 - 04:49 PM

http://www.mydomain....videos.php?id=* Information will be stored in the $_GET['id'] variable.

e.g.

http://www.mydomain....eos.php?id=Cats $_GET['id'] would be "Cats"

#4 eRott

eRott
  • Members
  • PipPipPip
  • Advanced Member
  • 206 posts
  • LocationToronto, ON

Posted 01 September 2006 - 04:56 PM

@ 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

#5 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 01 September 2006 - 04:57 PM

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 tutorials 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 :)
What is the CoronaMatrix?

#6 eRott

eRott
  • Members
  • PipPipPip
  • Advanced Member
  • 206 posts
  • LocationToronto, ON

Posted 01 September 2006 - 05:02 PM

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.

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 01 September 2006 - 05:52 PM

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:

video_name  video_id
------------  -------
rocket            1
balloon           2
cheese            3

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

videos.php
<?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
?>
     

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.


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 01 September 2006 - 06:12 PM

Heh... I couldn't have explained it any better than Crayon :).


What is the CoronaMatrix?

#9 eRott

eRott
  • Members
  • PipPipPip
  • Advanced Member
  • 206 posts
  • LocationToronto, ON

Posted 01 September 2006 - 06:24 PM

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
<?php
$dbhost = 'localhost';
$dbuser = 'insert_username';
$dbpass = 'insert_password';
$dbname = 'insert_database';
?> 


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



closedb.php
<?php
mysql_close($conn);
?>



create_database.php
<?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';
?>


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


      // 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

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.ret...ideos/funny.txt

#10 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 01 September 2006 - 06:40 PM

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

echo $showmovie;


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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#11 eRott

eRott
  • Members
  • PipPipPip
  • Advanced Member
  • 206 posts
  • LocationToronto, ON

Posted 01 September 2006 - 07:12 PM

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

#12 eRott

eRott
  • Members
  • PipPipPip
  • Advanced Member
  • 206 posts
  • LocationToronto, ON

Posted 01 September 2006 - 07:21 PM

Just realized something.

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

echo $showmovie;

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

$video = mysql_fetch_array($result);

which is just the results of searching the database. But there is no actual value, (eg: ) for the variable $video.

i am so lost.

#13 eRott

eRott
  • Members
  • PipPipPip
  • Advanced Member
  • 206 posts
  • LocationToronto, ON

Posted 01 September 2006 - 09:02 PM

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.ret...lib/videos.php", the page displays the video name "Terrance" and when I click on that, it brings me to "http://www.erott.ret...ideos.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:

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.ret...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.ret...ideos.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.

#14 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 01 September 2006 - 09:31 PM

oops. my bad. it should look like this:

<?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
?>

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#15 eRott

eRott
  • Members
  • PipPipPip
  • Advanced Member
  • 206 posts
  • LocationToronto, ON

Posted 01 September 2006 - 09:51 PM

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: } 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?

Edit: If you go here (it works): http://www.erott.ret.../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.

#16 eRott

eRott
  • Members
  • PipPipPip
  • Advanced Member
  • 206 posts
  • LocationToronto, ON

Posted 01 September 2006 - 11:15 PM

Edit: If you go here (it works): http://www.erott.ret.../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.


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.

Edit: 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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users