Andrew Davis
Andrew Davis

Reputation: 58

Output Webpage (Variable URL) with Database values, using PHP Template

I did some searching around, but I'm not very experienced in PHP, so I don't know the correct Terms/Words I am looking for. However, I do have a good idea of what I want to do.

  1. I have a website with many images, each image has a database entry (MySQL).
  2. I would like each image to have its own landing page, based on its database values. (Title, Category, Tags, etc...)
  3. I don't want to make a separate .php file on my server for each image. (Eg: pinkflowers.php, redroses.php, etc...)
  4. There should be 1 PHP template file, that outputs the webpage for ALL images, based on the URL (variable) that the user visits.

So if someone visits "mysite.com/pinkflowers.php", the page should be output with the variables of pinkflowers from my database.

However, the file pinkflowers.php doesn't actually exist, only template.php exists, which would be the "blueprint" for all the images in my database.

  1. I would like the .php to be removed from the URL in the browser.

    "mysite.com/pinkflowers.php" => "mysite.com/pinkflowers"

    I already have code that does this with my existing pages (below); I'm not sure if it will also work with these "imaginary" pages.

    (.htaccess)    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
  2. If the user tries to visit a page that has no related database entry (eg: "mysite.com/430jfif0ij"), they should be re-directed to the homepage.

I'm not expecting anyone to give me ALL the answers, but please at least guide me in the right direction to begin making such a template. Thanks!

Upvotes: 0

Views: 337

Answers (2)

Andrew Davis
Andrew Davis

Reputation: 58

I was able to solve my own question eventually, in the mean time that no Answers were given.

I followed this Tutorial here: http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

My .htaccess contained:

RewriteEngine on    
Options +FollowSymLinks

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  

RewriteRule ^category1/(.*)$ ./template.php?image=$1
RewriteRule ^category2/(.*)$ ./template.php?image=$1
RewriteRule ^category3/(.*)$ ./template.php?image=$1
etc...

category# - being the "folders" that my images are categorized by.

I did this on the template.php to extract the Variable from the URL:

$result = mysql_query('SELECT * FROM image_list WHERE image_name="' . mysql_real_escape_string($_GET['image']) . '"');

To redirect the visitor to the Homepage if they enter an invalid URL/ID, I used this:

if (mysql_num_rows($result)==0) {
echo "<script>window.location = 'http://mysite.com/'</script>";
}

So the combination of all these methods achieved exactly what I was looking for.

Upvotes: 2

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174987

Let your .htaccess be something like:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ template.php/$1

Then (on template.php) use $_SERVER["path_info"] to determine the path the user has entered (i.e. /pinkflowers) and query the database accordingly.

The reason I like $_SERVER["path_info"] is because it doesn't use (or pollute) the GET variable space.

Upvotes: 0

Related Questions