Page 1 of 1

"Including" files based on mySQL database entries?

Posted: Sun Jul 18, 2010 3:39 am
by markiebass
Hi, I have a mySQL database (and table) which lists several things about some films: name, rating, genre etc. The entries under "name" correspond to .php files which have reviews in them eg thesixthsense in the table will correspond to the file /reviews/thesixthsense.php. What I am trying to do is "include" the files based on the names in the table. So, for instance, if I had The Sixth Sense, Napoleon Dynamite and Fargo listed in my table, I want to be able to "include" the reviews based on the fact that the files and entries have the same names. I hope I'm making sense here! :)

If there is a way of doing this, then it means I'll be able to have pages listing just one genre, or only the films I've rated, say, 9 or 10. I've managed to print the actual path of the reviews (see below), but am stuck here. Any help will be much appreciated, as I am still quite new to PHP. :)

Code: Select all

<?php
$con = mysql_connect("localhost","myuser","mypass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("mydb", $con);
$result = mysql_query("SELECT * FROM films");
while($row = mysql_fetch_array($result))
  {
  print "/reviews/" . $row['name'] . ".php";
  print "<br/>";
  }
mysql_close($con);
?>

Re: "Including" files based on mySQL database entries?

Posted: Sun Jul 18, 2010 4:18 am
by Gargoyle
what's wrong with

Code: Select all

require('/reviews/'.$row['name'].'.php');
?

your whole concept makes my skin crawl, though.

Re: "Including" files based on mySQL database entries?

Posted: Sun Jul 18, 2010 5:33 am
by markiebass
Thanks, I got this working (eventually :oops: ).

I know this is probably a very backwards way of...doing what I'm trying to do, but hopefully the more I learn, the more I can improve how I do things. Thanks for the help. :)

Re: "Including" files based on mySQL database entries?

Posted: Sun Jul 18, 2010 7:10 am
by oscardog
markiebass wrote:Thanks, I got this working (eventually :oops: ).

I know this is probably a very backwards way of...doing what I'm trying to do, but hopefully the more I learn, the more I can improve how I do things. Thanks for the help. :)
It would be a lot more efficient to have a table named reviews and store all the data in that table (and each film would have it's own row). Then have one page that pulls the data from the table based on a GET variable (or something similar). And to make it SEO friendly you could even rewrite the URLs.