Help with PHP?
@xmanofsteel69 (458)
Canada
January 23, 2007 10:25am CST
Hey everyone!
I'm trying to create a site but I'm having big trouble. I don't want to write a huge code for HTML as I'm planning to add a search function (pretty much meaning I need an array). The only programming language that I know that I can do this with is PHP. I'm trying my best to learn it but it's giving me quite the hard time...
So my problem is, if anybody wants to help. I'm trying to put a list of movies I have available to purchase online. I'm planning to add all the different genre's for each movie so if you decide to look up "Action", all the movies with the genre "Action" is selected. Does anybody know how to set something like this up, or can someone point me in the right direction? I'm trying to learn from a book and a couple online videos right now, but it is quite hard. Thank you very much!
1 person likes this
6 responses
@swaroop_sv2003 (531)
• India
24 Jan 07
This problem is solved by using a SQL query rather than using PHP statements. That doesn't mean you need not use PHP statements . It is necessary but it is the mysql (i hope you are using mySql) connection part of PHP. Ok so you need to create a database lets name it 'movies'. Then we need to create a table in it called 'movie_list'. The fields in the tables are
1) Id int(11)
2) Name varchar(250)
3) Genre varchar(100)
So our database is setup. I hope you understood all these things. Next is the PHP and sql part. use the following code to get a list of all movies whose genre is "Action".
$connection= mysql_connect('localhost','root','password');
the above code would create a connection to your mysql server. The first argument is your hostname, second is the mysql user name and the last one is the password for the mysql server. We save this connection settings to a variable called $connection
mysql_select_db('movies',$connection);
this line creates a connection to the database movies on server ($connection) that we had made the connection in the first step.
$query= "SELECT* FROM movie_list WHERE Genre= 'Action'";$result= mysql_query($query);
The first step above is a standard SQL query that gets all the list of movies from a table called movie_list where the Genre is Action. This SQL is as such not exicuted it is only saved as a string to a variable called $query. The second step is where we execute the SQL query and the result is stored to the variable $result.
while ($row=mysql_fetch_object($result))
{ echo "$row-Id, $row-Name";}
This code fetches the results from the $results as an object. This is put in a while loop so that the loop is executed till the last object and then exits the loop. Inside the loop we print the Id and the Name of the movies that we got after th query.
So all the above said code is given below
$connection= mysql_connect('localhost','root','password');
mysql_select_db('movies',$connection);
$query= "SELECT* FROM movie_list WHERE Genre= 'Action'";
$result= mysql_query($query);while ($row=mysql_fetch_object($result))
{
echo "$row-Id, $row-Name";
}
I think that was a good introduction for you to PHP and mysql.I haven't tested this code, but it should work. If you are getting any error, post it here and I would help you. Let me tell that PHP is very easy to learn available and not that difficult as you think. By the the way dont for get to add the opening and closing PHP tags for the above code. I was not able to do that since it wont be printed here in mylot.
1 person likes this
@xmanofsteel69 (458)
• Canada
24 Jan 07
Nice response! But what about if I want multiple genre's? Not just one? I'm assuming it will have to be a 'loop' where it changes the position of each genre...possibly with a 'switch' as well... I'm just starting to learn PHP, but I did learn C++ a while ago and I've heard they're much the same. Thank you all very much for your responses!
@swaroop_sv2003 (531)
• India
25 Jan 07
Thanks for making this the best response. If you want to make the 'Genre' dynamic then just store the genre type to a variable say $genre and use this variable with the SQL query. For example "SELECT* FROM movie_list WHERE Genre= '$genre'";. I hope thats what you wanted to do.
@swaroop_sv2003 (531)
• India
25 Jan 07
Yes PHP got many similarities with C and C++. So if you know any of these languages then you wont find it much difficult to learn PHP. Just make sure that you got a good knowledge of Internet, server, client and related subjects. All the best.
1 person likes this
@xmanofsteel69 (458)
• Canada
30 Jan 07
Because I want to learn PHP and this is a good way to do it. I'm looking on forums and everything, but it's difficult sometimes. But yes, and I don't think it'll be a "store" anymore. It'll be more of just a list.
@iwyrobi (282)
• Indonesia
24 Jan 07
this is called a catalog online.
you need a category table and a video table. category to save the video genre. and video table is to save the video detail. this two table must be link using a key.
then u need a PHP algorithm to do this. i already make this site. but for furniture site. if you interest you can contact me.
thank you
@xmanofsteel69 (458)
• Canada
24 Jan 07
Yeah, I know. I was really hoping to just be able to learn it all on my own, though. I thought it would be an awesome accomplishment.
@stonehr (818)
• Croatia (Hrvatska)
23 Jan 07
So You need to have categories and under each categoies titles with description...
You can use for it files or mysql.
So You must know how to read from input, for example.. index.php?cat=action , so when you recognize that cat variable is action You 'll display all titles with action.
$cat = $_GET["cat"]; # so $cat will contain 'action', so then You neeed to connect to mysql or to open file....
and read entries and display it...
1 person likes this
@xmanofsteel69 (458)
• Canada
27 Jan 07
The only place I can see anything is within the making a website area...and it says you have to pay. I also rather learn it than just have someone else do it for me. I was just curious if people could help me along the way.